My program has a basic menu with options a-h, that leads to other menus and I was wondering if there is a way to limit the the user input to one character so the first input doesn't mess with the following menus.
#include <iostream>
#include <climits>
using std::cout;
using std::endl;
using std::cin;
int main(int argc, char **argv) {
// store a single character
char character;
// get a single character from the input stream
cin >> character;
// clear everything else from the input buffer
cin.ignore(INT_MAX, '\n');
// output the character the user entered
cout << character << endl;
// do something based on the character they gave (case-sensitive)
switch (character) {
case'A':
cout << "The first letter of the alphabet!" << endl;
break;
case'Z':
cout << "The last letter of the alphabet!" << endl;
break;
default:
cout << "I don't like that character!" << endl;
break;
}
return 0;
}