且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何允许用户进行两次以上的输入?

更新时间:2023-02-05 11:22:08

当行

cin >> rerun;

执行

时,'\n'留在输入流中.下次您运行

is executed, the '\n' is left in the input stream. Next time you run

getline(cin, input);

您会得到一个空行.

要解决此问题,请添加行

To fix the problem, add the line

cin.ignore();

紧接着

cin >> rerun;

这是您的循环的外观:

do
{
  getline(cin, input);
  cin >> rerun;
  cin.ignore();
}while (rerun == 'y');