Using while statement ask the user to input a character, accept only character Y or N. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using while statement ask the user to input a character, accept only character Y or N.

If other character are inputted ask the user to enter another character.

20th Feb 2022, 1:54 PM
Justine Santos
Justine Santos - avatar
2 Answers
+ 2
#include <iostream> using namespace std; int main() { char input; cout<<"Enter yes or no(Y/N)? "; cin >> input; // if input not == N or input not == Y return input else(input == to N/Y) cout<<"Successfuly"; while(!(input=='N' or input=='Y')) { cout<<"Enter yes or no(Y/N)? "; cin >> input; } cout<<"Successfuly!"<<endl; return 0; }
20th Feb 2022, 5:01 PM
the ice
the ice - avatar
+ 1
Show me your code and I'll try to help you make it work. #include <iostream> int main() { char res; while( std::cin >> res && ( res != 'Y' && res != 'N' ) ) { std::cout << "Enter Y or N please\n"; } std::cout << res; return 0; } https://www.sololearn.com/post/75089/?ref=app (Edited)
20th Feb 2022, 2:18 PM
Ipang