0

Im trying to find a way to limit my input type to just only one in the game Hangman without using the LetterFill. The hangman works perfectly and when you input more than 1 character (ex. ABC) it will accept it and input and 1 by 1 but my prof said that it should only input only 1 character and if there are more than 1 it will eliminate/ignore the other letters any idea how to do it?

Code:

    char x;
    cin >> x;
    if (guesses.find(x) == string::npos)
        guesses += x;
Euxicius
  • 47
  • 9
  • [https://stackoverflow.com/questions/22028142/read-only-one-char-from-cin](https://stackoverflow.com/questions/22028142/read-only-one-char-from-cin) – BluesSolo Oct 25 '18 at 15:04
  • 1
    Read in a `std::string` using `std::getline` and thn check of the `size()` of the string is 1, if not ask again. – NathanOliver Oct 25 '18 at 15:11

1 Answers1

0

The solution may be to ignore all the rest untill end of line, like:

char x;
cin >> x;
cin.ignore(256,'\n');

other solution (Good for unlimited size)

char x;
cin >> x;
while(cin.peek() != '\n')
   cin.ignore();
SHR
  • 7,940
  • 9
  • 38
  • 57