I was wondering how to use cin
so that if the user does not enter in any value and just pushes ENTER
that cin
will recognize this as valid
To detect the user pressing the Enter Key rather than entering an integer:
char c;
int num;
cin.get(c); // get a single character
if (c == 10) return 0; // 10 = ascii linefeed (Enter Key) so exit
else cin.putback(c); // else put the character back
cin >> num; // get user input as expected
Alternatively:
char c;
int num;
c = cin.peek(); // read next character without extracting it
if (c == '\n') return 0; // linefeed (Enter Key) so exit
cin >> num; // get user input as expected