0

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?

2
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by lingol
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.