如果我输入一个字母而不是一个数字,为什么会得到一个无限循环? 我正在编写一个家庭作业的代码(刚开始C++,所以请轻松一点)。我们今天刚刚开始了while、do-while和for循环。该程序运行良好,但如果在程序要求整数时输入字母,它将无限循环。发生了什么事?(代码如下) ***编辑:为了澄清,循环的部分是:“您输入的数字是负数。请输入正数继续。”但用户没有机会输入其他数字。它只是一直在打印这个 #include <iostream> using namespace std; int main ( ) { //define variables int num1, num2, total; char answer1; do { //user enters a number cout << "\nPlease enter a positive number and press Enter: \n"; cin >> num1; //check that the given num1 value is positive while (num1 < 0) { cout << "The number you entered is negative.\nPlease enter a positive number to continue.\n"; cin >> num1; } cout << endl; //add the sum of 1 through num1 value num2 = 1; total = 0; while (num1 >= num2) { total = total + num2; num2 ++; } //tell the user the sum cout << "The total of all the integers\nfrom 1 to " << num1 << " is: \n"; cout << total; //ask if the user wants to try again cout << "\n\nWould you like to try again with a new number?\nEnter y for yes or n for no.\n"; cin >> answer1; } while (answer1 == 'y'); cout << endl; return 0; } #包括 使用名称空间std; int main() { //定义变量 整数num1,num2,总计; char-answer1; 做 { //用户输入一个数字 cout>num1; //检查给定num1值是否为正值 而(num1num1; } cout=num2) { 总计=总计+num2; num2++; } //告诉用户总数 cout
当您输入一个字母时,设置了如果我输入一个字母而不是一个数字,为什么会得到一个无限循环? 我正在编写一个家庭作业的代码(刚开始C++,所以请轻松一点)。我们今天刚刚开始了while、do-while和for循环。该程序运行良好,但如果在程序要求整数时输入字母,它将无限循环。发生了什么事?(代码如下) ***编辑:为了澄清,循环的部分是:“您输入的数字是负数。请输入正数继续。”但用户没有机会输入其他数字。它只是一直在打印这个 #include <iostream> using namespace std; int main ( ) { //define variables int num1, num2, total; char answer1; do { //user enters a number cout << "\nPlease enter a positive number and press Enter: \n"; cin >> num1; //check that the given num1 value is positive while (num1 < 0) { cout << "The number you entered is negative.\nPlease enter a positive number to continue.\n"; cin >> num1; } cout << endl; //add the sum of 1 through num1 value num2 = 1; total = 0; while (num1 >= num2) { total = total + num2; num2 ++; } //tell the user the sum cout << "The total of all the integers\nfrom 1 to " << num1 << " is: \n"; cout << total; //ask if the user wants to try again cout << "\n\nWould you like to try again with a new number?\nEnter y for yes or n for no.\n"; cin >> answer1; } while (answer1 == 'y'); cout << endl; return 0; } #包括 使用名称空间std; int main() { //定义变量 整数num1,num2,总计; char-answer1; 做 { //用户输入一个数字 cout>num1; //检查给定num1值是否为正值 而(num1num1; } cout=num2) { 总计=总计+num2; num2++; } //告诉用户总数 cout,c++,infinite-loop,C++,Infinite Loop,当您输入一个字母时,设置了cin的错误状态,并且在调用cin.clear()之前不会有任何进一步的输入。因此,语句cin>>num1不会更改num1的值,您将永远循环 试试这个: while (num1 < 0) { cout << "The number you entered is negative.\nPlease enter a positive number to continue.\n"; cin.clear();
cin
的错误状态,并且在调用cin.clear()
之前不会有任何进一步的输入。因此,语句cin>>num1
不会更改num1
的值,您将永远循环
试试这个:
while (num1 < 0)
{
cout << "The number you entered is negative.\nPlease enter a positive number to continue.\n";
cin.clear();
cin >> num1;
}
应该可以解决您的问题。基本上,您试图从流中读取字符,但无法将其解析为int,因此流处于错误状态
你应该检查错误,清除它并做出相应的反应。这就是
basic\u istream
的工作原理。在你的情况下,当cin>>num1
获得错误输入时-failbit
被设置,而cin
未被清除。因此下次它将是相同的错误输入。要正确处理这个问题,你可以添加检查正确的input和清除并忽略cin
,以防输入错误。例如:
#include<limits>
//user enters a number
cout << "\nPlease enter a positive number and press Enter: \n";
do {
while(!(cin >> num1)) {
cout << "Incorrect input. Please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(num1 < 0) cout << "The number you entered is negative. Please enter a positive number to continue.\n";
} while(num1 < 0);
#包括
//用户输入一个数字
cout>num1){
cout您可以使用“char”数据类型作为用户的输入
然后使用“static_cast”(“变量名”)
char输入;
int选择;
cin>>输入;
choose=static_cast(choose)-48;///然后将'if'语句与变量'choose'一起使用
您缺少cin.clear()
。您缺少错误检查。我想我宁愿使用and,而不是使用看似简单的流运算符。这是不够的。num1
未初始化,因此字母条目不保证触发此循环。在OP的示例中,它似乎确实被触发,但正如您所说,它不保证触发。谢谢,但不能保证。触发错误的非法字符未被提取,因此在清除后,您将在>
中首先得到它,然后再次设置错误。您需要以某种方式重新同步输入。@LightnessRacesinOrbit这是在C++11中已更改的内容。在C++11之前,可以保证失败的>
(对于数值)没有修改目标。在C++11中,可以保证它会修改目标,将其设置为0表示格式错误,将其设置为+/-最大值表示溢出。文本“此答案”“没有解释如何解决这个问题。我知道这是一个链接,但是如果你要参考一些额外的资源,你应该引用一些重要的段落。我们应该写什么来代替”“?@AlphaMiner什么都没有,streamsize就是你写的。
#include<limits>
//user enters a number
cout << "\nPlease enter a positive number and press Enter: \n";
do {
while(!(cin >> num1)) {
cout << "Incorrect input. Please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(num1 < 0) cout << "The number you entered is negative. Please enter a positive number to continue.\n";
} while(num1 < 0);
char input;
int choose;
cin >> input;
choose = static_cast<int>(choose) - 48;///then use 'if' statement with the variable 'choose'