jsa*_*san -3 c++ boolean while-loop
我正在尝试评估一个字符:
bool repeat = true;
while (repeat)
//code
char x;
cout << "Would you like to tansfer another file? Y/N ";
cin >> x;
if (x == 'y' || x == 'Y')
repeat = true;
if (x == 'n' || x == 'N')
repeat = false;
else
throw "Input error";
Run Code Online (Sandbox Code Playgroud)
我一直得到输入错误作为我的控制台输出.有什么想法吗?我不能让while循环重复.
你错过了一个else:
if (x == 'n' || x == 'N')
Run Code Online (Sandbox Code Playgroud)
应该:
else if (x == 'n' || x == 'N')
Run Code Online (Sandbox Code Playgroud)
并且您需要在while包含输入和if语句之后添加大括号.