954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to ignore a whole line with cin?

I'm writing a function that reads an IP address from input and generates its 32-byte values. If the input is correct, the whole thing can be done by this:

int a, b, c, d;
char buf;
cin>>a>>buf>>b>>buf>>c>>buf>>d;
...

If it's not, such as 256.0.0.0, or 1..2.3.4, I want to ignore the whole line and start with a new line. One solution I found is using a string to read the leave characters. Like:

if (!checkinput())
{
    cin.clear();
    string str;
    getline(cin, str);
}

I just hate to use a new variable. How can I do the job with istream's own functions?

lingol
Newbie Poster
2 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

How do I flush the input stream?

cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks!

lingol
Newbie Poster
2 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You