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

I'm pretty sure this should work:

char c;
cin >> c;

I haven't tried it, but it only allows for a one character input I think...

Commenting code is very good -- but commenting with wrong information is very very bad.

>> // Clears the unused characters from the buffer
that function does not do what you said it does. The clear method clears error bits.

>> Ignores the buffer generated by cin.clear
Doesn't do that either. cin.clear() doesn't create a buffer

Thanks for the help an corrections.

This is the code I now have:

/*******************************************************************************

     User input function */
     
void userInput()
{

     cout << "\nPlease input a letter: ";
     //cin.ignore(1024, '\n');
     //cin.get(userInputCheck, 2);
     //cin.ignore(1024, '\n');
     cin >> character2;
     while (!cin.good())
     {
           cout << "You have entered an invalid input, please input a letter: ";
           cin.ignore(1024, '\n');
           cin >> character2;
     }
     //if (!isalpha(userInputCheck[0]))
     //     userInput();
     //character = toupper(userInputCheck[0]);
     character = toupper(character2);
}

The code snippet suggestion works, I now must write code that checks when the user has entered more than 1 character. What's happening now is that if the user enters more than 1 letter in the input, then the function will loop for the amount of letters entered by the user (ie 4 times for gggg) and then dump back to the game menu.

I may try using a character vector to accept the input from the user, then taking only the first element in that character array and proceeding from there.

When I've had a chance to attempt to write something to fix that, I'll post again in this thread.

Thanks!

accept input as a string---no error checking needed as string type is always valid

check first char of string to see if it's alphabetical char, if not get new string

if first char of string is alphabetical char convert it to upper case as needed.

can clear the input string by assigning it "" if it's STL string or assigning the null char to index zero if it's a null terminated char array.

So something like this would work then:

/*******************************************************************************

     User input function */
     
void userInput()
{
     string character2;
     cout << "\nPlease input a letter: ";
     //cin.ignore(1024, '\n');
     //cin.get(userInputCheck, 2);
     //cin.ignore(1024, '\n');
     cin >> character2;
     while (!isalpha(character2[0]))
     {
           cout << "You have entered an invalid input, please input a letter: ";
           //cin.ignore(1024, '\n');
          character2.clear();
           cin >> character2;
     }
     //if (!isalpha(userInputCheck[0]))
     //     userInput();
     //character = toupper(userInputCheck[0]);
     character = toupper(character2);
}

Edit: I tried the code that I wrote above, and it works perfectly! Thanks for the suggestioni, I wouldn't have though to try it that way.

also, just a note you can do this to store up to n charictors in a row.

char character2 [insert a number here];
cin >> character2;

> also, just a note you can do this to store up to n charictors in a row.
Simple char arrays are no match for a C++ string in a C++ program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.