Help!

Hi all,

I'm taking my first CS course this summer, and I'm loving it. I'm having a bit of trouble understanding how and when to use cin.get(). I am trying to take an input of numbers followed by a character, to identify which route to take.

thanks in advance.

Edit:

The input can only be one line...
Last edited on
could you use a switch statement?

Hmm can you be a bit more specific on what the task is? Other than that, cin.get() stores the next character into a variable of type char.
closed account (zb0S216C)
std::cin.get( ) returns an integer, not a character. The integer returned is the numerical equivalent to the character entered. You can, however, cast the integer returned to a character using a C-style cast, like this:

char cInputBuf( ( char )std::cin.get( ) );

Wazzak
Last edited on
so lets say ch is a char variable and the program contains the line cin.get(ch). If the input is er32r, ch=='e' is true correct?
closed account (zb0S216C)
Yes, that is correct. Since char can only store a single character, std::cin.get( ) extracts just the one character from the input buffer; in this case, that character is e.

Calling std::cin.get( ) without passing it a buffer( ch ), it will return the numerical equivalent as I previously said. However, if you pass it an input buffer, the character entered is placed into the buffer as a character( obviously, you would except this to happen ).

Wazzak
Last edited on
Topic archived. No new replies allowed.