Help with strings

I need to split my string into 3 separate strings.For some reason only a few of my strings do what I want but the rest don't. Can some one help me? Thanks in advanced

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

void breakup(string, string&, string&, string&);
ifstream infile("Threewords.txt");
int main()
{
     int pos;
     string  one, two, three, line;
     
     
     while(getline(infile,line)){
          cout<<"Original String: ";
          breakup(line, one, two, three);
          cout<<line<<endl;
          cout<<one<<endl;
          cout<<two<<endl;
          
          
         
     }
     
     
     infile.close();
     system("pause");
     return 0;
}


void breakup (string line, string &one, string &two, string &three){ 
     
     int pos, pos2, pos3;
    
     
     
     pos=line.find(' ', 0);
     one=line.substr(0,pos);
     pos2=line.find(' ', pos-1);
     two=line.substr(pos+1,pos2);
    
    return;
}  



OUTPUT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Original String: Save our ships
Save
our
Original String: It is hot
It
is
Original String: Wy Le Coyote
Wy
Le
Original String: How are you
How
are
Original String: Come with me
Come
with
Original String: Come here please
Come
here
Original String: You should go
You
sho
Original String: I am fine
I
a
Original String: I love you
I
l
Original String: I miss you
I
m
Press any key to continue . . .
Someone please?
In main, the while loop... You only 'cout' one and two NOT three.

In the breakup function, you only get two words... Not 3!

int pos3; unreferenced local variable. You never save anything to it!
Last edited on
OK here is my fixed code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

void breakup(string, string&, string&, string&);
ifstream infile("Threewords.txt");
int main()
{
     int pos;
     string  one, two, three, line;
     
     
     while(getline(infile,line)){
          cout<<"Original String: ";
          breakup(line, one, two, three);
          cout<<line<<endl;
          cout<<one<<endl;
          cout<<two<<endl;
          cout<<three<<endl;
          
         
     }
     
     
     infile.close();
     system("pause");
     return 0;
}


void breakup (string line, string &one, string &two, string &three){ 
     
     
    
     int pos, pos2, pos3;
     
     pos=line.find(' ', 0);
     one=line.substr(0,pos);
     pos2=line.find(' ', pos+1);
     two=line.substr(pos+1,pos2);
     pos3=line.find(' ',pos2+1);
     three=line.substr(pos2+1);
     
    
    return;
}  


Here is my new output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Original String: Save our ships
Save
our ship
ships
Original String: It is hot
It
is ho
hot
Original String: Wy Le Coyote
Wy
Le Co
Coyote
Original String: How are you
How
are you
you
Original String: Come with me
Come
with me
me
Original String: Come here please
Come
here plea
please
Original String: You should go
You
should go
go
Original String: I am fine
I
am f
fine
Original String: I love you
I
love y
you
Original String: I miss you
I
miss y
you
Press any key to continue . . .


It still isnt correct....

This just took me a while, I've never used these functions on string before. But I got it working!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//find where each word is.
	 pos = line.find( ' ', 0 );
	 pos2 = line.find( ' ', pos + 1 );
	 pos3 = line.find( ' ', pos2 + 1 );

	 //run these backwards, because changing pos2 before using it
	 //will give you the wrong output. i.e working out 1..2..3.., rather than 3..2..1..
	 pos3 -= pos2;
	 three = line.substr( pos2 + 1, pos3 );

	 //Do this, because, you only want the length of the new word.
	 // position in string, minus the string that came before, leaves the length of the new string
	 pos2 -= pos;
	 two = line.substr( pos + 1, pos2 );

	 one = line.substr( 0, pos );
Thank you so much. This works perfect. I couldn't figure it out for the life of me.
No prob. It just took me the best part of an hour to figure it out, ahaha!
Topic archived. No new replies allowed.