That's the correct behaviour of the
stringstream
's extraction operator: you requested an
int
and it provided the first valid
int
represented in the
string
.
If you want to extract the four ip octects as integers then try the following code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string ip;
int anwswer;
int octet[4];
char c;
do
{
cout << "Enter IPv4: ";
getline (cin,ip);
stringstream s(ip);
for (int n=0; n<4; ++n)
s >> octet[n] >> c;
for (int n=0; n<4; ++n)
cout << "octet[" << n << "]=" << octet[n] << endl;
cin>>c;
if(c == 'y')
cout<<"DONE!"; else
cout<<"NOT DONE!"; /
} while (c != 'y');
return 0;
}