General Question

HeroicZach's avatar

Why do I need "clear" the I/O input with a dummy getline() call before I use getline() in terminal I/O?

Asked by HeroicZach (195 points ) October 19th, 2009

So I’m writing a C++ program, see, and it uses cin>> a lot. For some sub-parts of the program, however, I need to use getline(cin,whatIWant); to access data values. No matter what I do, the FIRST call to getline after I have already used a cin>> above that does absolutely nothing – C++ skips it as if there is data already in the terminal that it just reads. I have determined it is reading absolutely nothing with a test dummy variable. Is there any more elegant solution than:

string dummy;
getline(cin, dummy); // OK, now I can get real values now

Thanks for your help!

Observing members: 0 Composing members: 0

6 Answers

Response moderated
HeroicZach's avatar

@frdelrosario Thank you for indirectly calling me stupid.

I’ve received very kind and constructive help regarding C++ programming in the past on Fluther. I’m pretty sure Fluther is more than a web forum to debate ethereal questions.

Looking through your answer profile, you might want to chill out a bit and stop attacking people for asking what they don’t know on a site constructed for that very purpose.

Perhaps I don’t know how to phrase this elaborate question into a Google query.

Just chill out a bit, go take a walk, breathe the air of life – you can think then about how we’re all not as smart as you for asking questions to which you, in your almighty academic god status, already know the answer, yet are too inconvenienced to divulge without insult.

frdelrosario's avatar

I’m sorry.

But then I still think that if you’d asked your programming question in a programming forum, you’d have your answer by now.

I’d understand if someone used Fluther for a technical question because the users in technical forums are bigger assholes than I, though.

HeroicZach's avatar

@frdelrosario No problem, I overreacted slightly – I’m running into a few problems, as you can tell by my desperate question.

I ended up solving the issue anyway. Lines in the terminal from the cin>> call leave the escape \0, which getline then reads, causing getline to terminate prematurely. Oops, I guess!

phoenyx's avatar

A lot of fluther members (including me and the founders) are professional programmers. I don’t mind programming questions at all.

noyesa's avatar

The insertion operator, when used on cin, ignores all leading whitespace but stops pulling data from the stream when it encounters another whitespace character, such as a newline, in the stream. The getline() function stops reading from a stream when it finds a newline character and returns the characters it has collected thus far. Since a newline operator is the first character in the stream after it is read by the insertion operator, calling getline() will cause it to return before collecting any characters, which is why it appears to return nothing.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther