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?

Recommended Answers

All 2 Replies

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.