我一直在遇到do while循环不重复的问题。 当提示再次添加两个数字时我选择N时,它将按预期退出程序,但是选择Y或y会使程序挂起,将我带到下一行,但使我无法键入任何内容。如果您能帮助我,我将不胜感激。谢谢!
#include <iostream>
using namespace std;
int main()
{
int number1, number2; //Two numbers
int sum; //Sum
char again; //For yet another attempt
{ //Inputting the two numbers
cout << "\nEnter two numbers, and i will add them: ";
cin >> number1 >> number2;
//Adding the two numbers
sum = number1 + number2;
cout << "The sum of the two numbers you have picked is " << sum << "\n\n";
//Does the user want to add two other numbers?
cout << "Do you want to add two other numbers? (Y/N) ";
cin >> again;
}
while (again == ('y') || again == ('Y'));
return 0;
}
答案 0 :(得分:2)
do { statement} while(condition)
在上面的循环语法中执行,我想您在语法方面错过了世界。 尝试替换您的do-while阻止,如下所示。
do{ //Inputting the two numbers
cout << "\nEnter two numbers, and i will add them: ";
cin >> number1 >> number2;
//Adding the two numbers
sum = number1 + number2;
cout << "The sum of the two numbers you have picked is " << sum << "\n\n";
//Does the user want to add two other numbers?
cout << "Do you want to add two other numbers? (Y/N) ";
cin >> again;}while (again == ('y') || again == ('Y'));
答案 1 :(得分:0)
是的,您需要在一段时间内执行一次。不能说比lion_pankaj好。
每次在运行程序之前向代码添加新内容或进行调试时,还应进行构建检查,并检查所有警告或错误(如果有)。还取决于用来帮助发现少量错误的IDE。有时我们都会忘记一点东西。