当条件为真或为假时,如何让它返回并再次提问,让用户重新输入值?
这是我想要实现的:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
cout<<"Enter numbers. Press 5 to stop: ";
cin>>n;
bool tr=true;
while(tr)
{
if(n!=5)
cout<<"You entered "<<n; //How to make it return again, since its false? I keep getting infinite loops :( ;
else
tr=false;
}
return 0;
}
最佳答案
您需要在 while 循环中提示用户,以便它在每次迭代中发生:
int n;
bool tr = true;
while(tr)
{
cout << "Enter numbers. Press 5 to stop: ";
cin >> n;
if(n!=5) {
cout << "You entered " << n;
} else {
tr = false;
}
}
关于c++ - 如果条件为真则返回 if 条件,如果为真则返回 while,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15175902/