2013-04-24 64 views
1

SoI必須能夠驗證用戶輸入,並確保它是因爲某種原因它不會給我,它是垃圾郵件正在while循環..請help.Thanks驗證Char功能

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

回答

0

你想要「while(choice!= y & & choice!= x)」,這是一個邏輯同義反復。

+0

謝謝你是正確的我搞砸了。非常感謝你 – 2013-04-24 22:25:07