String Manipulation Help

Apr 17, 2015 at 8:32pm
Working on an assignment for a c++ class, and I've been running into some trouble. I'm sure this is probably a very simple issue, but I'm horrible at programming, so bear with me. Here is the exercise guidelines:
You are given a file consisting of students' names in the following form: lastName, firstName midddleName. (Note that a student may not have a middle name.) Write a program that converts each name to the following form: firstName middleName lastName. Your program must read student's entire name in a variable and must consist of a function that takes as input a string, consists of a student's name, and returns the string consisting of the altered name. Use the string function find to find the inex of ,; the function length to find the length of the string; and the function substr to extract the firstName, middleName, and lastName.

The input file looks like this:
Miller, Jason Brian
Blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth

I made an attempt to code at least the extraction and printing of the names, but failed miserably. My output was " Miller". I really don't understand the flaws in my logic, which I know is pathetic, but I would really appreciate any assistance that can be offered. Here's my 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
 #include <iostream>
#include <string>
#include <fstream>

using namespace std;

void getName();


int main()
{
	getName();

	system("pause");
	return 0;
}

void getName()
{
	string fullName;
	ifstream inData;
	do
	{

		string lastName;
		string otherName;
		int junk;
		int otherJunk;
		inData.open("Exercise9Page518.txt");
		inData >> fullName;
		junk = fullName.find(",");
		lastName = fullName.substr(0, junk);
		otherJunk = fullName.find("\n");
		otherName = fullName.substr(junk + 1, otherJunk);
		cout << otherName << " " << lastName << endl;
	}
	while (fullName != "");
	inData.close();
}
Apr 17, 2015 at 8:37pm
Use getline instead of >>.

Don't attempt to open the file every iteration of the loop.

Don't loop on the value of fullName. Loop on the success of the input operation.
Apr 17, 2015 at 8:58pm
I understand the first two things, but the last one doesn't make sense to me. (not from a logic perspective, but from an execution perspective.) Thank you for your help.
Apr 19, 2015 at 5:38pm
I am stuck on the same assignment. I read both of your comments, how does this look?

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void getNames();

int main()
{
    inData.open("Chapter7.9.txt");
    getNames();

    system("pause");
	return 0;
}

void getNames()
{
    string fullName
    ifstream inData;
    do
    {
        string lastName;
        string firstName;
        int junk;
        int other;
        junk = fullName.find (',');
        lastName = fullName.substr(0, junk);
        other = fullName.find("\n");
        otherName = fullName.substr(junk + 1, other);
        cout << otherName << " " << lastName << endl;
    }
    while (fullName != " ");
    inData.close();
}
Apr 19, 2015 at 10:01pm
So I've figured a bit more out, but it's still not working quite right. For some reason it is now skipping every other input, and I don't understand why. Here's my new 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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void getName();


int main()
{
	getName();

	system("pause");
	return 0;
}

void getName()
{
	string fullName;
	ifstream inData;
	string lastName;
	string otherName;
	string firstName;
	int junk;
	int otherJunk;
	inData.open("Exercise9Page518.txt");
	do
	{
		getline(inData, fullName);
		junk = fullName.find(",");
		lastName = fullName.substr(0, junk);
		otherJunk = fullName.length();
		firstName = fullName.substr(junk + 2, otherJunk);
		cout << firstName << " " << lastName << endl;
	}
	while (getline(inData, fullName));
	inData.close();
}
Apr 19, 2015 at 10:02pm
The input file is:
Miller, Jason Brian
Blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth

my output is:
Brian Jason Miller
Kumar Anil Gupta
Beth Rhonda Saleh

I don't understand why it would do this.
Apr 19, 2015 at 10:05pm
Correction:
my output was:
Jason Brian Miller
Anil Kumar Gupta
Rhonda Beth Saleh
Apr 19, 2015 at 10:22pm
I figured the program out. For anyone in the future who needs help with this, this will work for any file.
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void getName();


int main()
{
	getName();

	system("pause");
	return 0;
}

void getName()
{
	string fullName;
	ifstream inData;
	string lastName;
	string otherName;
	string firstName;
	int junk;
	int otherJunk;
	inData.open("Exercise9Page518.txt");
	do
	{
		getline(inData, fullName);
		junk = fullName.find(",");
		lastName = fullName.substr(0, junk);
		otherJunk = fullName.length();
		firstName = fullName.substr(junk + 2, otherJunk);
		cout << firstName << " " << lastName << endl;
	}
	while (inData.eof() == false);
	inData.close();
}
Apr 20, 2015 at 3:42am
The "correct" way to do this is to loop on the success of input extraction, not eof.

1
2
3
4
5
    while ( getline(indata, fullName) )
    {
        junk = fullName.find(",");
        // ...
    }


Notice that the input extraction only occurs in the while condition. It does not also occur in the body of the loop.
Apr 20, 2015 at 4:03am
Thank you guys!!!
Topic archived. No new replies allowed.