Equivalent of cin.getline for one char?

Jan 12, 2011 at 7:57pm
I have been substituting all I can with cin.getline instead of cin, since apparently that leaves behind a \n in the stream. but what if I want the user to just input one character that isn't an array/string? Some would maybe say that I could just set the char variable to be a one dimensional array, and the user would only know to enter one character. But I thought that would be unprofessional seeming, and it would also make it where I couldn't use == and would have to use that string compare function. So, is there some way I could get around this without using cin?
Jan 13, 2011 at 3:06am
Trying to handle all sorts of possible user input on console is very difficult and error-prone. You never know what kind of input user entered in. This is why a lot of companies have made GUI for user input. Using the GUI mode, you can filter what user can enter and this can save a lot of head-aches on your end.

With browser, GUI development is very easy (assume simple HTML and some CSS, JavaScript) thrown in.

Without browser, do consider the different GUI SDK around.

Edit:
Oops I forget to mention ncurses someone told me. Basically you use the console in a "GUI-like" manner and this can control user input only valid characters. Worth a look but in comparison to "true-bred" GUI, I feel the ncurses UI still lack that *oomph* :P
Last edited on Jan 13, 2011 at 3:08am
Jan 13, 2011 at 3:09am
Yes, as a matter of fact there is! It's std::cin.get(char).
http://cplusplus.com/reference/iostream/istream/get/

Happy programming!

-Albatross
Jan 13, 2011 at 3:22am
I'm sure Albatross is more familiar than I, but:

If you are looking to just get one character, why not define a char, which by default will only input one character to it?

example:

1
2
3
4
5
6
char ch;

cout << "Enter yes/no: ";
cin >> ch; // this will only grab y or n

cout << "\nYou entered: " << ch << endl;


Example run:

Enter yes/no: yes

You entered: y
Jan 13, 2011 at 3:35am
That's because he's looking to avoid the use of cin >>.

-Albatross
Jan 13, 2011 at 3:41am
Ah, very true. Silly of me :)
Last edited on Jan 13, 2011 at 3:41am
Jan 13, 2011 at 4:17am
But his stated concern is the leftover newline in the stream. std::cin.get(char) still leaves newlines in the stream (unless, of course, it is the newline character being extracted).

Rather than force your code to fit an artificial rule, you should rather apply an actual rule:

The user will always press ENTER to after every input.

Hence, whether the user is inputting his name or a number, he will press ENTER to signal he is done. That means that your program needs to extract that enter key press after every user input.

Hence:
1
2
3
4
5
6
7
8
9
10
string user_name;
unsigned user_age;

cout << "Please enter your full name: ";
getline( cin, user_name );

cout << "Please enter your age: ";
cin >> user_age;
cin.clear();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

etc.

Hope this helps.
Topic archived. No new replies allowed.