istream cin - How to get just one Character

Helle there,

i got following while loop:

1
2
3
4
5
6
7
8
9
  char cEingabe = '\0';

  do 
  {
    std::cin >> cEingabe;
    
    std::cout << "Your Choice was:" << cEingabe << std::endl << std::endl;
   
  } while ('q' != cEingabe);


At the moment the code was running, but there is a misbehavior, which i try to fix.

If i insert the string "nk" the Output is:
[LINE 1:] Your Choice was: n
[LINE 2:] Cour Choice was: k

How can i prepare "cin" that just LINE 1 is in the output.
I tried diverse Methods of istream but i dont get it.

Thanks for your time.
hi,

in the reference for operator>> http://cplusplus.com/reference/iostream/istream/operator%3E%3E/ , there is an example when they limit the size of the word read in input by using cin.width (unsigned int i). maybe you can try that. Otherwise, maybe the iostream::get function could be used in your case.
Thanks for that Reply, but i had tested both merthods before i opened the new topic and both are ineffective.
:?
You have to play with platform-specific code to set the terminal to unbuffered input. Google "msdn SetConsoleMode" for Windows.

Good luck!
closed account (DSLq5Di1)
I think they want to get the first character from input and ignore the rest, no?

#include <limits>

1
2
3
4
5
6
7
8
9
10
  char cEingabe = '\0';

  do 
  {
    std::cin >> cEingabe;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
    std::cout << "Your Choice was:" << cEingabe << std::endl << std::endl;
   
  } while ('q' != cEingabe);
Last edited on
Yeah, thats what i am searching for. Thanks to you.
Oops! I should have read the first post more carefully. :-(
Topic archived. No new replies allowed.