当条件为真或假时,如何使其返回并再次询问问题,要求用户重新输入值?以下是我想实现的内容:
#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 (true) { /*...*/ else break; }
(我知道你保留它来坚持原始代码,这个评论更多地是针对 OP 的)。 - syam