验证Char功能

时间:2013-04-24 22:20:05

标签: function char cin

所以我必须能够验证用户输入,并确保它是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;
}

1 个答案:

答案 0 :(得分:0)

你想&#34;而(选择!= y&amp;&amp; choice!= x)&#34;,||这是一个合乎逻辑的重言式。