cin.get... cin.ignore... why bother?

closed account (3CXz8vqX)
Hi,

I'm a little confused. I wanted a program to 'wait' for a newline, or input...

I've tried the following.

1
2
3
4
5
6
7
8
9
char temp;
cin >> temp;

cin.ignore()
cin.get()

getline(cin, temp)
cin.getline()


...What I get in return is nothing. It just doesn't work, the program just skips it and moves on completely. Not even taking the time the notice half the time. (It threw a tantrum when I stuck one inside a while loop waiting for a '\n' but that's about it).

I'm running Fedora Linux and using Gedit, G++ as my tools. I know this topic has been...talked about quite a bit but apparently nothing...nothing at all works.
That isn't the actual actual code that you are running though, is it. It's hard to fix a problem without knowing what is broken.
http://cplusplus.com/forum/beginner/1988/
There are many solutions suggested here.
I think you have to give an amount of time for ignore() to work properly...
I.e. cin.ignore(10) should ignore for 10 milliseconds... The idea of having ignore i think is to ignore what's called signal bounce, so you ignore any signals for a given amount of time and then check input.
You need to understand what each of the functions do.

cin >> temp;
Skips all whitespace characters until it finds a non-whitespace character, that it writes to temp, e.g. if the stream contained "\t hello\n" before this line it will contain "ello\n" after this line and temp will have the value 'h'. If there is no non-whitespace character to read it will wait until there is.

cin.ignore();
Removes the next character from the stream, e.g. if the stream contained "ello\n" before this line it will contain "llo\n" after this line. If there is no character to read it will wait until there is.

cin.get();
Same as cin.ignore(); just that it returns the character that is removed.

getline(cin, temp);
For this to work temp has to be of type std::string. It will read all characters and put them into temp until it finds a new line character. The newline character is removed from the stream but is not added to temp, e.g. if the stream contained"one \ntwo\n" before this line it will contain "two\n" after this line and temp will have the value "one ". If there is no newline character to read it will wait until there is.

cin.ignore(10);
Showed by SatsumaBenji will do the same as ignore but instead of 1 character it ignores the next 10 characters. If there isn't 10 character to read it will wait until there is.
Well, I bet that helps an awful lot...
Sorry for my previous confusion with the functionality.
Topic archived. No new replies allowed.