seperating strings

Hi I am nnew to c++ and have a simple question.
say i have a string of str = "hello world 23.133";
I wish to store a string values to
str1 = "hello";
str2 = "world";
double = 23.133;

another one will be to ignore \n
str = "123\n45 foot'\n'ball 5\n67"
str1 = "12345"
str2 = "foot''ball"
double = 567;

another question is if string does not contain
world world double = return false
so for example str = 12345 12345 12345 will be ok
but with str = 12345 12345 hello will return false
thank you~



Last edited on
The easiest way to do this is to use stringstream
1
2
3
stingstream ss(my_string );//mystring = "hello world"
ss >> s1;//s1 now = "hello"
ss >> s2;//s2 now = "world" 

this should work with \n too.

I'm not sure if I understand your second question, but would it work simply to split a string (like above) and compare all of it's parts?
For the second question, you can always check the stream's state to see if an input failed.
1
2
3
4
stringstream ss( "hello" );
int x;
ss >> x;
if (ss.fail()) cout << "That's not an integer!";
Last edited on
Topic archived. No new replies allowed.