Programming Assignment Help

Apr 12, 2020 at 10:53pm
I'm having trouble with my assignemnt and was hoping I could get some help. My professor wants me to convert a name in the format of lastname, firstname middlename to a format of firsname middlename lastname.

My input file looks like this:

Mckenney, Cody Jeremiah
Keeler, Sasha Marie
Hester, John Gomez
Schwartz, Hugo Cannon
Barker, Maxine Parks
Keeler, John Keith
Sparks, Bob River
Nichols, Jon Larry
Ramos, Seth Dill
Mccormick, Candace Cruz

I have most of it done, but I cant seem to understand why the output file is repeating some of the words in the string.

This is what my output file ends up looking like:

The altered name is: Cody Jer Cody Jeremiah Mckenney
The altered name is: Sasha Sasha Marie Keeler
The altered name is: John G John Gomez Hester
The altered name is: Hugo Can Hugo Cannon Schwartz
The altered name is: Maxine Maxine Parks Barker
The altered name is: John K John Keith Keeler
The altered name is: Bob Ri Bob River Sparks
The altered name is: Jon Lar Jon Larry Nichols
The altered name is: Seth Seth Dill Ramos
The altered name is: Candace C Candace Cruz Mccormick

I know I'm a noob programmer, but any help would be appreciated.





#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
// Variables
int timesRun;
string wholeName, hold;
int comma, space;
// File Stuff
ifstream inFile;
ofstream outFile;
// Opening files
inFile.open("student_names.txt");
outFile.open("altered_names.txt");
// Program Description
cout << "This program is meant to accquire some names from a text file and convert them to a specific format. " << endl;

for (timesRun = 10; timesRun > 0; timesRun--)
{
getline(inFile, wholeName);
hold = wholeName;
comma = wholeName.find(", ");
space = wholeName.find(" ");

outFile << "The altered name is: " << hold.substr(comma +1 , space ) << hold.substr(space , hold.length()) << hold.substr(0, comma ) << endl;



}

return 0;
}
Last edited on Apr 12, 2020 at 11:01pm
Apr 13, 2020 at 4:11am
Rather than some complex expression which doesn't work, break it down
1
2
3
4
5
comma = wholeName.find(", ");
last_name = wholeName.substr(0,comma);
given_names = wholeName.substr(comma+1);
space = given_names.find(" ");
// Now you extract first and last, then print 
Apr 13, 2020 at 7:12am
1
2
3
getline( inFile, surname, ',' );
getline( inFile, firstnames );
string wholename = firstnames + ' ' + surname;
Apr 13, 2020 at 10:38am
Hello FalseKing,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button, but you may still to indent your code.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Unless your assignment is meant to deal with sub strings it is easier to use what you have.

lastchance has shown you the shortest way of doing this.

You start with has shown Mckenney, Cody Jeremiah and just need to change the order. You have a comma after the last name that you can use and a space between the first and middle names that you can use.

The code I used is:
1
2
3
4
5
6
7
while (std::getline(inFile, last, ','))
{
	std::getline(inFile, first, ' ');
	std::getline(inFile, middle);

	outFile << first << ' ' << middle << ' ' << last << '\n';
}

The while loop is a more better way of reading a file of unknown length. Putting the first read in the while condition will cause the while loop to fail when you reach end of file.

I did this to show how you can use the third parameter of "std::getline" to break up the line into 3 variables. If this is not needed then delete line 3 and change the variable name in line 4 and you will have it shortened as lastchance did.

If you have to use the ".find" and ".substr" functions then you are going about in the wrong way. That part I will have to rework.

Andy
Topic archived. No new replies allowed.