Hello all,
I've tried reading back threads and the various posted tutorials, etc that here and I've not stumbled upon the answer I'm looking for.
I am attempting to write a hangman game as a school project, and I am having trouble getting just one character from the input stream. This is the code I have so far:
/*******************************************************************************
Code to get user input */
char userInputCheck[1]; // A "1 cell" array that will contain a single value
char character;
cout << "Please input a letter: ";
cin.ignore(1024, '\n'); // Ignores a buffer size of 1024 characters, or until a newline is found
cin.get(userInputCheck, 2); // Tells the program to disregard from character 2 onward
cin.clear(); // Clears the unused characters from the buffer
cin.ignore(1024, '\n'); // Ignores the buffer generated by cin.clear
character = toupper(userInputCheck[0]);
/*******************************************************************************/
Its pretty ugly, but it works ok. I don't yet know how to code something better (which is what I'm trying to find out). The original idea that I had was to read the text stream from the user into an array declared to size of 1, ignoring all other characters after the first character so as not crash the program.
There HAS to be a better way of doing this. I've read about the get function at cplusplus.com. cplusreference.com, and I've googled the net but I've come up with nada. If someone would be so kind as to help me with this, I (and my teammates for this program) would appreciate it.
Thanks