-
Converting question
I need to convert a character input into numbers. For instance, in this example:
127.0.4.56
Since this is not a float, I need to convert 127, 0, 4, and 56 into numbers. My question is how can I read those numbers separately.... use an array? use a particular input stream? or something else??
Thanks.
-
Here's one way -
Code:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream ss;
int a,b,c,d;
char discard;
char* str = "127.0.4.56";
ss << str;
ss >> a >> discard >> b >> discard >> c >> discard >> d;
cout << a << ' ' << b << ' ' << c << ' ' << d;
return 0;
}
-
you can use atoi() and take advantage of the fact that atoi() stops at non-numeric input so the . delimeter will not be a problem
-
just out of curiousity, what the heck is <sstream>? I have never seen it in my life.
-
I would do what stoned_coder says... parse an array of your individual character sets with the '.' as a delimiter and then atoi() each result....
-
Its a string stream. works like sprintf() and sscanf()
-
Thanks
Thank giys for your info. I'll see if it works.