我一直遇到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;
}
do { statement} while(condition)
在循环语法在上面时执行,我想您在语法中错过了做世界的事情。