I saw a program that asks the user to input y or n to continue the process... and when you input one of them it automatically presses enter and proceed to the next process
I want to know how to do that .. and can I manage the number of digits or characters to be taken before auto pressing enter?
you need to press enter in order to put y or n inside answer variable
but what I want is... you don't need to press enter .. it should press enter automatically after you press y or n.
#include <iostream>
#include <conio.h> //this header is needed to use _getch()
usingnamespace std;
int main () {
cout << "Do you want to see the secret word? (y/n): ";
char move = _getch(); //_getch loads move with one character that the user enters
//the code continues after one key is pressed
if(move == 'y') {
cout << "\nThe secret word is chicken burger!" << endl;
}
elseif(move == 'n') {
cout << "\nWhy didn't you want to see the secret word?" << endl;
}
else {
cout << "\nInvalid input." << endl;
}
return 0;
}
If you want an option after another option, try using a switch statement.
Use the code above, then re-use the code within the switch statement to get input once again, but for another option. Or just get the switch statement to call a function with the above code in to get more input.