ok so i have a do- while loop and I'm trying to format it so when the user types a y or just leaves it blank and hits the enter button it gives a true statement but so far everytime I test by just hitting enter it just keeps newlining. Some help with what to do will be great. thank you guys!
1 2 3 4 5 6 7 8 9 10 11 12 13
cout << "hello !!!\n"
<< "would you like another greeting?(y\n)\n";
cin << response;
if((toupper(response) == 'Y')||( response == '\n'))
@i like red pandas
There was nothing wrong with your answer -- you shouldn't go back and delete stuff.
Dealing with user input is not anywhere near as simple as we tend to think it is (or ought to be).
C++'s stream operators are powerful and tend to lull us into the idea that just getting input and correctly parsing a stream are one and the same. Other languages make it a little less flexible and/or more pedantic.
For example, in Python, to get input you must mystring = raw_input(). Once you have that, you can try to convert it/parse it to whatever you want, which Python makes easy with dynamic typing and built-in string/list processing capabilities.
In C++, you must be a little more explicit about it, and put the sanity checks in all the right spots.
For example, the snippet I posted works because a c-string always has at least one character -- the terminating null. So by checking against the first character in the c-string, there cannot ever be an error on that test -- even if the input stream is in an error state. It is a simple trick for getting a single unconditional character from the user, given the following mantra:
The user will always press ENTER at the end of every input.