semester please end

So I'm having a bit of trouble with spliting a string of three words separated each by a whitespace character. I know I'm supposed to use the find() function and the substr() function but it isn't working properly with the code I have so far. pls help

1
2
3
4
5
6
7
  	pos=line.find(" ");
	first=line.substr(0,pos);
	pos2=line.find(" ",pos+1);
	middle=line.substr(pos+1,pos2);
	pos3=line.find(" ",pos2+1);
	last=line.substr(pos2+1,pos3);
	return;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

void SplitString (const string line)
{
  size_t pos = line.find (" ");
  string first = line.substr (0, pos);
  size_t pos2 = line.find (" ", pos + 1);
  string middle = line.substr (pos + 1, pos2 - pos-1);
  size_t pos3 = line.find (" ", pos2 + 1);
  string last = line.substr (pos2 + 1, pos3);

  cout << first << '\t' << middle << '\t' << last;
}

int main ()
{
  SplitString ("Anna Margaretha Kozuch");
  cout << "\n\n";
  system ("pause");
  return EXIT_SUCCESS;
}
Last edited on
Is it an actual requirement to use the find() and substr() functions? This really seems like a job for a stringstream.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string line {"Anna Margaretha Kozuch"};
    std::string first, middle, last;
    std::stringstream sin(line);
    sin >> first >> middle >> last;
    std::cout << first << '\t' << middle << '\t' << last << std::endl;

    return 0;
}



Last edited on
We only learned the find, replace, insert, substr, and erase functions, so I assume that my professor would only like us to use those when completing this assignment.
Thomas1965 thanks, your version works, but I now have the problem of printing an empty string on the last loop pass. I'm using a while loop to read in till failure, like this.
1
2
3
4
5
6
7
8
9
10
while(getline(fin,line))
	{
		cout<<"Original Line: "<<line<<endl<<endl;
		breakup(line,first,middle,last);
		cout<<first<<" is first"<<endl;
		cout<<middle<<" is middle"<<endl;
		cout<<last<<" is last"<<"\n"<<endl;
	}
	fin.close();
	return 0;


Original Line: Do Not Know

Do is first
Not is middle
Know is last

Original Line: here is summer

here is first
is is middle
summer is last

Original Line: butterscotch zipper tang

butterscotch is first
zipper is middle
tang is last

Original Line: lolipops And Rainbows

lolipops is first
And is middle
Rainbows is last

Original Line:

is first
is middle
is last


--------------------------------
Process exited after 0.08499 seconds with return value 0
Press any key to continue . . .
I have exact same problem in my similiar program. What can I do?!
Do you have any leading spaces in your input? Can you post the whole 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
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <fstream>
void breakup(std::string,std::string &,std::string &,std::string &);
std::string makealpha(std::string &,std::string &,std::string &);
using namespace std;
int main()
{
	string line; string first; string middle; string last; string neww;
	
	ifstream fin("A7infile.txt");
	

	while(getline(fin,line))
	{
		cout<<"Original Line: "<<line<<endl<<endl;
		breakup(line,first,middle,last);
		cout<<first<<" :is first"<<endl;
		cout<<middle<<" :is middle"<<endl;
		cout<<last<<" :is last\n"<<endl;
		neww=makealpha(first,middle,last);
		cout<<neww<<" :is the alphabetized line\n"<<endl;
	}
	fin.close();
	return 0;
}


void breakup(string line,string &first,string &middle,string &last)
{
	string::size_type pos; string::size_type pos2; string::size_type pos3;
	
	pos=line.find(" ");
	first=line.substr(0,pos);
	pos2=line.find(" ",pos+1);
	middle=line.substr(pos+1,pos2-pos-1);
	pos3=line.find(" ",pos2+1);
	last=line.substr(pos2+1);
	return;
}

std::string makealpha(string &first,string &middle,string &last)
{
	string neww;
	
	if(first<middle&&first<last&&middle<last)
		neww=first+" "+middle+" "+last;
	else if(first<last&&first<middle&&last<middle)
		neww=first+" "+last+" "+middle;
	else if(middle<first&&middle<last&&last<first)
		neww=middle+" "+last+" "+first;            
	else if(middle<first&&middle<last&&first<last) 
		neww=middle+" "+first+" "+last;            
	else if(last<first&&last<middle&&first<middle)               
		neww=last+" "+first+" "+middle;            
	else if(last<middle&&last<first&&middle<first) 
		neww=last+" "+middle+" "+first; 
	return neww;
}
Do I have to call getline before the while loop and then call it again in the while (getline())?
Topic archived. No new replies allowed.