getline( cin, str ) bad!

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm making a simple console program for input/output files, for practicing my C++ :) I found out that to get a whole string (including spaces) i'd have to use getline. So i put it in like
getline( cin, cont );
//cont is a string


The problem though is that it doesn't stop to let me input the string, why is this? What's the solution?

Share on other sites
Well, I'm not pretty sure... but perhaps that is because there's already something on the queve. So the getline function reads that instead of waiting for new input. Try putting a cin.get() functions before that statement (note, this is just for checking if that's the error...). Or perhaps another getline function.

Hope it helps :)

Share on other sites
Not sure if this is your problem but consider the following:

#include <iostream>#include <string>using namespace std;int main(){    string s;    char c=cin.get();    getline(cin,s);    cout << "[" << c << "]\n[" << s << "]\n";    return 0;}

Output:
[x]
[]

This exhibits the behaviour you describe on the getline line. The problem is that after the character has been read with cin.get(), there is a newline character waiting in the input stream, as jorlemb suggested, so the getline just reads this and returns with an empty string result.

The solution to this would be adding a cin.ignore line before the getline:

#include <iostream>#include <string>using namespace std;int main(){    string s;    char c=cin.get();    cin.ignore(1,'\n');    getline(cin,s);    cout << "[" << c << "]\n[" << s << "]\n";    return 0;}

Output:
[x]
[hello]

The program above now works as you would expect.

HTH Paul

Share on other sites
Quote:
 Original post by charlandoThe problem though is that it doesn't stop to let me input the string, why is this? What's the solution?

You would need to give us the actual code you are using and what input you are testing it on. By itself, your getline() call is fine - it is the interaction with the rest of your code that causes trouble.

Share on other sites
Thanks EasilyConfused! After adding the cin.ignore it worked fine :)

Share on other sites
So using this method would not allow spaces?

char c;
cin >> c;

But the method above allows spaces to be stored in that variable?

Share on other sites
Quote:
 Original post by SaddistSo using this method would not allow spaces?char c;cin >> c;But the method above allows spaces to be stored in that variable?

Yep. cin >> whatever_type will skip whitespace whereas getline will just read anything up to a newline character by default, although any termination character can be specified as the third parameter to getline.

[EDIT] Oops. See Fruny below. I thought cin >> always skipped whitespace but apparently that is just for std::strings.

[Edited by - EasilyConfused on September 17, 2006 4:45:13 PM]

Share on other sites
To be precise, char c; cin >> c; will only read one character (white space or not) and string s; cin >> s; will only read one word, stopping at whitespace. Whether or not leading spaces are skipped or included in the string is something that can be controlled by setting flags on the stream.

cin.ignore() will ignore one character, in your case, likely to be the newline that's causing you trouble, but a better solution is to use cin >> ws; which will eat leading white space (including '\n'). You may have to #include <iomanip> to get access to it (I can never remember which manipulator is where).

Share on other sites

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• Forum Statistics

• Total Topics
628698
• Total Posts
2984275

• 20
• 10
• 13
• 13
• 11