What does cout << cin.get() ; do?

What does cout << cin.get() ; do?

When I enter 22 for example. It outputs 50. Why? how does this work?
just shows the ascii code of the character you have entered... 22 can not be true because 22 doesn't have ascii code.. in fact it shows ascii code of 2
so 1kjdsiewkjl as input --> ascii code of 1 as output ok?
closed account (j2NvC542)
Provide code, please.
cout << cin.get();
To understand this, you need to understand the pieces first. When using cout, each section is evaluated before being "sent" to cout. In this case, you have cin.get().

cin.get()
All that cin.get() does is take a value, typically entered by your keyboard, and converts it to an int. Since a keyboard prints "characters" and you typed 22, you're really typing '2''2'. cin.get() also only accepts one character, therefore, your second '2' is sitting in the buffer (I believe). Since '2' is a char and not an int (like what cin.get() returns) it is converted (as dhm said) to a numerical value, in this case, ASCII. '2' = 50.

Now you have:
cout << 50;
This is pretty self explanatory, but I'll elaborate. Now that the statement has been evaluated to something that cout can understand, cout must do something with it. In this case, cout takes the int, 50, and converts it into a string, "50", and then displays it on the screen.

I hope that helps you understand that a little better.
Yes I understand it more now. Thanks for the help. However, I am soon at the pages (in the book that i am reading) where cout is deeply explained. So I will get it sooner or later.

I checked the character '2' in the Ascii table and saw what you meant. But when i tried:

cout << '\50' ;

It ouput the corresponding Ascii character according to the octal system and gave '(' instead of 2 which I wanted. How do you output characters with Ascii code accoring to decimal system? Example: '\(symbols that show that you want decimal system) 50'.
Last edited on
closed account (j2NvC542)
You can cast an integer to a char, which will make it the corresponding character. Just like when you output a char as an integer, you get the ascii value.
 
cout << char(ascii_value);
Last edited on
Yes, it worked. Thanks so much for the help.
Topic archived. No new replies allowed.