陷入无限的while循环 在C++中,处理错误输入(比如当程序要求一个整数但你输入一个字符)时,它应该能够做一些事情,然后循环重复输入。
当需要整数时输入字符,我的循环将无限重复,反之亦然:陷入无限的while循环 在C++中,处理错误输入(比如当程序要求一个整数但你输入一个字符)时,它应该能够做一些事情,然后循环重复输入。,c++,while-loop,int,iostream,infinite-loop,C++,While Loop,Int,Iostream,Infinite Loop,当需要整数时输入字符,我的循环将无限重复,反之亦然: #include<iostream> int main() { while (cout << "Enter a number" && !(cin >> num)) { cin.sync(); cin.clear(); cout << "Invalid input; please re-enter.\n";
#include<iostream>
int main()
{
while (cout << "Enter a number" && !(cin >> num))
{
cin.sync();
cin.clear();
cout << "Invalid input; please re-enter.\n";
}
}
#包括
int main()
{
while(cout>num))
{
cin.sync();
cin.clear();
不能使用cin.ignore()
而不是cin.sync()
并查看原因
while(cout>num)){
cin.clear();//清除错误
cin.ignore(100'\n');//忽略字符直到下一个'\n'(或最多100个字符)
cout您可以将输入读取为字符串,将其转换为整数并进行验证
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
string input;
do {
cout << "Enter a number: ";
cin >> input;
number = atoi(input.c_str()); // atoi returns 0 on error..
if (number != 0 || input.compare("0") == 0) // but maybe the user really entered "0".
break;
cout << endl;
cout << "Invalid input; please re-enter.\n";
} while (1);
cout << "You entered " << number << endl;
return 0;
}
…虽然这可能不是最优雅的解决方案。如果cin>>num
失败,则需要从流中删除无效输入。我建议使用ignore
而不是sync
来完成此操作。原因是sync
不能保证在所有实现中删除剩余的输入标准图书馆
#include <iostream>
#include <limits>
int main()
{
int num;
while (cout << "Enter a number" && !(cin >> num))
{
cin.clear(); // clear the error
// Remove input up to a new line
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Invalid input; please re-enter.\n";
}
}
表达式(text>>std::skipws>>ch).fail()
将跳过数字后出现的所有空格,然后尝试读取单个字符。如果没有可用字符,则读取将失败,表明用户只输入了一个数字而没有输入其他内容。如果我们尝试使用cin
执行此操作,则即使输入有效,也会等待用户输入更多文本。您应该我真的没有写那种代码,很难理解发生了什么以及错误在哪里。提示:@Djon我发现代码很清楚,错误很容易发现。到底是什么让你感到困扰?我很久没有见过cin和cout,我觉得它很混乱,但我很欣赏人们可能会发现它很清楚。然而,如果问题是,OP似乎是新的,这可能不是最好的学习方法,因为所有内容都集中在一行。@jrok我不喜欢默认值(1,EOF)。给初学者举一个具体的例子(100)比例如std::numeric\u limits::max()
更明确。事实上,我应该添加一个指向文档的链接(完成)。如果用户输入的字符超过100个,会发生什么情况?如果我将“233dsjfeu4”作为输入,因为它将其读取为233,然后给出错误,那么它就不起作用了,但是对于“342365465235436436”或“ddsfdfdggv”@amolakhandel,它可以正常工作。我已经更新了我的答案,以包括验证,确保只输入一个数字。除了whit以外的任何尾随字符ESPACE将表示失败。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
string input;
do {
cout << "Enter a number: ";
cin >> input;
number = atoi(input.c_str()); // atoi returns 0 on error..
if (number != 0 || input.compare("0") == 0) // but maybe the user really entered "0".
break;
cout << endl;
cout << "Invalid input; please re-enter.\n";
} while (1);
cout << "You entered " << number << endl;
return 0;
}
% ./foo
Enter a number: abc
Invalid input; please re-enter.
Enter a number: 2
You entered 2
% ./foo
Enter a number: 0
You entered 0
#include <iostream>
#include <limits>
int main()
{
int num;
while (cout << "Enter a number" && !(cin >> num))
{
cin.clear(); // clear the error
// Remove input up to a new line
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "Invalid input; please re-enter.\n";
}
}
#include <iostream>
#include <sstream>
int main()
{
int num;
for(;;)
{
std::cout << "Enter a number: " << std::flush;
std::string line;
std::getline(std::cin, line); // Read a line from console
std::stringstream text(line); // store in a string stream
char ch = 0;
if(!(text >> num).fail() && (text >> std::skipws >> ch).fail())
{
break;
}
std::cout << "Invalid input; please re-enter.\n";
}
return 0;
}