• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

Very newb question about std::cin

I used:

std::cin >> someStdStringVar;

and then I follow it up with another prompt and


If you've used std::cin frequently you'll know that this won't work. There are still characters left in the buffer (2 in my case) and my bet is that they are the \r\n pair not stored into someStdStringVar previously.

The only way I managed to get them out of there was to add another getline() call before the one I'm interested in.

I tried std::cin.seekg(0,std::ios_base::end); and also std::cin.ignore(INFINITE); before the call to getline() but both caused the following call to getline() to fail to block while waiting for input and filled the buffer with nothing.

I don't mind calling getline() and extra time, but it seems crude. Why is seekg() not working as I expect it to?


2 Solutions
I would guess that
std::cin.ignore (INFINITE, '\n');
should work. From cin I wouldn't expect any EOF since it is a stream but not a file.
Sandra-24Author Commented:
Nope does the same as the rest :(

Using cin is not as simple as it looks.
maybe this will help :

std::cin.flush(); // flush the complete stream

Not sure though !
This works for me, Sandra:
#include <iostream>
#include <limits>

int main()
        std::string str1,str2;
        using std::cin;
        const int INFINITE = std::numeric_limits<int>::max();

        cin >> str1;
        std::cout << "str1 [" << str1 << "]\nstr2 [" << str2 << "]\n";

Can you post a simple example of your problem?
Sandra-24Author Commented:
You guys are right. The trouble was that I didn't define INFINITE in my program. Ordinarily that would prevent it from compiling, but it worked because a define of INFINITE does exist 0xFFFFFFFF which proabably wrapped to a negative number (or so I'm guessing). At any rate it's working now, thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now