while循环意味着验证用户输入不断循环
While loop meant to verify user input constantly loops
我正在尝试验证用户输入,但我已经尝试了两个编译器,但我遇到了以下两种情况之一。要么它会:
-不断循环错误消息而不要求用户输入
或者
-等待用户输入,如果输入不正确,会不断循环报错。
代码如下:
cout << "Input number of the equation you want to use (1,2,3): " ;
cin >> userInput;
cout << endl;
while (userInput <= 0 || userInput >= 4)
{
cout << "Please enter a correct input (1,2,3): " ;
cin >> userInput;
cout << endl;
}
if (userInput == 1)
{
userInput 声明为整数。是否有更简单的方法来验证用户输入,或者是否需要 while 循环?我对编码还是很陌生。
我会添加额外的检查以确保如果用户输入非整数输入,则在尝试下一次读取之前清除流。
cout << "Input number of the equation you want to use (1,2,3): " ;
cin >> userInput;
cout << endl;
while (userInput <= 0 || userInput >= 4)
{
if ( !cin.good() )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
cout << "Please enter a correct input (1,2,3): " ;
cin >> userInput;
cout << endl;
}
我建议改用 do 循环,这样重复的行就会更少
int userInput = 0;
do
{
cout << "Input number of the equation you want to use (1,2,3): " ;
cin >> userInput;
cout << endl;
if ( !cin.good() )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
} while (userInput <= 0 || userInput >= 4);
如果您要执行任何错误检查,您不希望 cin >> int。如果用户输入非整数,您将陷入难以恢复的境地。
而是将 cin 转换为字符串,执行任何您想要的错误检查并将字符串转换为整数:
long x;
string sx;
cin >> sx;
x = strtol(sx.c_str(), NULL, 10);
虽然使用 int userInput
看起来很简单,但当用户输入非数字值时它会失败。您可以改用 std::string
并检查它是否包含数值
std::string userInput;
int value;
std::cout << "Input number of the equation you want to use (1,2,3): " ;
while (std::cin >> userInput) {
std::istringstream s(userInput);
s >> value;
if (value >= 1 && value <= 3)
break;
std::cout << "Please enter a correct input (1,2,3): " ;
}
std::istringstream
类似于其他输入流。它提供来自内部内存缓冲区的输入,在本例中为 userInput
.
提供的值
我正在尝试验证用户输入,但我已经尝试了两个编译器,但我遇到了以下两种情况之一。要么它会: -不断循环错误消息而不要求用户输入 或者 -等待用户输入,如果输入不正确,会不断循环报错。
代码如下:
cout << "Input number of the equation you want to use (1,2,3): " ;
cin >> userInput;
cout << endl;
while (userInput <= 0 || userInput >= 4)
{
cout << "Please enter a correct input (1,2,3): " ;
cin >> userInput;
cout << endl;
}
if (userInput == 1)
{
userInput 声明为整数。是否有更简单的方法来验证用户输入,或者是否需要 while 循环?我对编码还是很陌生。
我会添加额外的检查以确保如果用户输入非整数输入,则在尝试下一次读取之前清除流。
cout << "Input number of the equation you want to use (1,2,3): " ;
cin >> userInput;
cout << endl;
while (userInput <= 0 || userInput >= 4)
{
if ( !cin.good() )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
cout << "Please enter a correct input (1,2,3): " ;
cin >> userInput;
cout << endl;
}
我建议改用 do 循环,这样重复的行就会更少
int userInput = 0;
do
{
cout << "Input number of the equation you want to use (1,2,3): " ;
cin >> userInput;
cout << endl;
if ( !cin.good() )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
} while (userInput <= 0 || userInput >= 4);
如果您要执行任何错误检查,您不希望 cin >> int。如果用户输入非整数,您将陷入难以恢复的境地。
而是将 cin 转换为字符串,执行任何您想要的错误检查并将字符串转换为整数:
long x;
string sx;
cin >> sx;
x = strtol(sx.c_str(), NULL, 10);
虽然使用 int userInput
看起来很简单,但当用户输入非数字值时它会失败。您可以改用 std::string
并检查它是否包含数值
std::string userInput;
int value;
std::cout << "Input number of the equation you want to use (1,2,3): " ;
while (std::cin >> userInput) {
std::istringstream s(userInput);
s >> value;
if (value >= 1 && value <= 3)
break;
std::cout << "Please enter a correct input (1,2,3): " ;
}
std::istringstream
类似于其他输入流。它提供来自内部内存缓冲区的输入,在本例中为 userInput
.