-3

我正在尝试评估单个字符:

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";

我不断收到输入错误作为我的控制台输出。任何想法为什么?我无法让 while 循环重复。

4

2 回答 2

6

您在else这里缺少一个:

if (x == 'n' || x == 'N')

应该:

else if (x == 'n' || x == 'N')

并且您需要在之后添加大括号while以包含输入和if语句。

于 2013-10-01T15:47:13.713 回答
4

你忘记了大括号{}之后while

while (repeat)
{

  char x;
  cout << "Would you like to tansfer another file? Y/N ";
  cin >> x;

  if (x == 'y' || x == 'Y')
      repeat = true;
  else
  if (x == 'n' || x == 'N')
      repeat = false;
  else
      throw "Input error";

}
于 2013-10-01T15:47:38.150 回答