【问题标题】:Validating Char function验证 Char 函数
【发布时间】:2013-04-24 22:20:05
【问题描述】:

所以我必须能够验证用户输入并确保它是 y 或 n 出于某种原因它没有给我它并且它正在向 while 循环的主体发送垃圾邮件。请帮助。谢谢

char getCharChoice(string message, char y, char n)
{
char choice;

// Print the message and get the input
cout<<message<< " ";
cin>>choice;
// Check for valid input
while (choice != y || choice != n)
{

    cin.clear(); // reset the input stream
    cin.ignore(5000, '\n'); // take any remaining input off the stream

    // Ask for a choice and read user input
cout << "Not a valid input please: Choose y or n:  ";
    cin >> choice;
}


return choice;
}

【问题讨论】:

    标签: function char cin


    【解决方案1】:

    你想要“while (choice != y && choice != x)”,||使它成为一个合乎逻辑的重言式。

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      相关资源
      最近更新 更多