仅获取整数作为输入,代码未按预期工作
Get only integers as input, code not working as expected
我试图只允许输入整数
应该拒绝:
- 5小时
- 3.4
- 3.gh
- 3.0
- htr
应该接受:
- -5
- 0
- 78
当前代码
int getIntInput() {
int userInput;
while (true) {
std::cout << "> ";
std::cin >> userInput;
std::cout << std::flush;
if (std::cin.fail()) {
std::string cinBuffer;
std::cin.clear();
std::getline(std::cin, cinBuffer);
continue;
}
break;
}
return userInput;
}
更新代码
问题:
接受除 "htr" 之外的所有拒绝(无数字)
int getIntInput() {
std::string rawInput;
int parsedinput;
while (true) {
std::cout << "> ";
std::getline(std::cin, rawInput);
std::cout << std::flush;
try {
parsedinput = std::stoi(rawInput);
} catch (std::invalid_argument & e) {
continue;
} catch (std::out_of_range & e) {
continue;
}
break;
}
return parsedinput;
}
完成代码
只接受带有可选参数的整数,这将允许
接受或拒绝的负数。
int getIntInput(bool allowNegatives = true) {
bool validIntInput;
std::string rawInput;
int parsedinput;
while (true) {
validIntInput = true;
// Grabs the entire input line
std::cout << "> ";
std::getline(std::cin, rawInput);
std::cout << std::flush;
for (int i = 0; i < rawInput.length(); i++) {
// Checks to see if all digits are a number as well as to see if the number is a negative number
if (!isdigit(rawInput[i]) && !(allowNegatives && i == 0 && rawInput[i] == '-')) {
validIntInput = false;
break;
}
}
if (!validIntInput) {
continue;
} else {
try {
// Try parse the string to an int
parsedinput = std::stoi(rawInput);
// Catch all possible exceptions, another input will be required
} catch (...) {
continue;
}
// If the code reaches here then the string has been parsed to an int
break;
}
}
return parsedinput;}
cin 在必须读取 int 时的工作方式是,它开始解析整数并在找到非 int 字符时停止,将解析的 int 存储在变量中。这就是为什么在浮点数上它停在点上,而在输入“5g”中它将停在 g 上。
如果您只想输入整数,您可以做的是 read the whole line 然后检查字符串中的每个字符是否都是数字,使用以下代码:
bool onlyNums = true;
for (int i=0;i<rawInput.size();i++) {
if (!isdigit(rawInput[i]))
onlyNums = false;
}
if (!onlyNums)
continue;
(您必须为上述代码包含 ctype.h 库)
如果您不介意开销,我会使用 cin.getline()
从 cin 获取输入并将其保存为字符串。然后遍历字符串并在每个字符上调用 isdigit
。您可以使用 str.erase
函数丢弃不是数字的字符。
您需要为 isdigit()
#include cctype。
注意:根据字符串的长度,这至少需要 O(N) 的运行时间。
template<class T>
T findInStreamLineWith(std::istream &input) {
std::string line;
while (std::getline(input, line)) {
std::istringstream stream(line);
T x;
if (stream >> x >> std::ws && stream.eof()) {
return x;
}
// reenter value
// cout << "Enter value again: ";
}
throw std::invalid_argument("can't find value in stream");
}
…
auto x = findInStreamLineWith<int>(std::cin);
我试图只允许输入整数
应该拒绝:
- 5小时
- 3.4
- 3.gh
- 3.0
- htr
应该接受:
- -5
- 0
- 78
当前代码
int getIntInput() {
int userInput;
while (true) {
std::cout << "> ";
std::cin >> userInput;
std::cout << std::flush;
if (std::cin.fail()) {
std::string cinBuffer;
std::cin.clear();
std::getline(std::cin, cinBuffer);
continue;
}
break;
}
return userInput;
}
更新代码
问题:
接受除 "htr" 之外的所有拒绝(无数字)
int getIntInput() { std::string rawInput; int parsedinput; while (true) { std::cout << "> "; std::getline(std::cin, rawInput); std::cout << std::flush; try { parsedinput = std::stoi(rawInput); } catch (std::invalid_argument & e) { continue; } catch (std::out_of_range & e) { continue; } break; } return parsedinput; }
完成代码
只接受带有可选参数的整数,这将允许 接受或拒绝的负数。
int getIntInput(bool allowNegatives = true) { bool validIntInput; std::string rawInput; int parsedinput; while (true) { validIntInput = true; // Grabs the entire input line std::cout << "> "; std::getline(std::cin, rawInput); std::cout << std::flush; for (int i = 0; i < rawInput.length(); i++) { // Checks to see if all digits are a number as well as to see if the number is a negative number if (!isdigit(rawInput[i]) && !(allowNegatives && i == 0 && rawInput[i] == '-')) { validIntInput = false; break; } } if (!validIntInput) { continue; } else { try { // Try parse the string to an int parsedinput = std::stoi(rawInput); // Catch all possible exceptions, another input will be required } catch (...) { continue; } // If the code reaches here then the string has been parsed to an int break; } } return parsedinput;}
cin 在必须读取 int 时的工作方式是,它开始解析整数并在找到非 int 字符时停止,将解析的 int 存储在变量中。这就是为什么在浮点数上它停在点上,而在输入“5g”中它将停在 g 上。
如果您只想输入整数,您可以做的是 read the whole line 然后检查字符串中的每个字符是否都是数字,使用以下代码:
bool onlyNums = true;
for (int i=0;i<rawInput.size();i++) {
if (!isdigit(rawInput[i]))
onlyNums = false;
}
if (!onlyNums)
continue;
(您必须为上述代码包含 ctype.h 库)
如果您不介意开销,我会使用 cin.getline()
从 cin 获取输入并将其保存为字符串。然后遍历字符串并在每个字符上调用 isdigit
。您可以使用 str.erase
函数丢弃不是数字的字符。
您需要为 isdigit()
#include cctype。
注意:根据字符串的长度,这至少需要 O(N) 的运行时间。
template<class T>
T findInStreamLineWith(std::istream &input) {
std::string line;
while (std::getline(input, line)) {
std::istringstream stream(line);
T x;
if (stream >> x >> std::ws && stream.eof()) {
return x;
}
// reenter value
// cout << "Enter value again: ";
}
throw std::invalid_argument("can't find value in stream");
}
…
auto x = findInStreamLineWith<int>(std::cin);