Newbie function question

Pocky Is God

Ars Legatus Legionis
24,457
Taking an intermediate class at the local Uni for OOP in c++. I'm supposed to read names from a file using ifstream in the format "first name, middle, last name" and output that information as "last name, first name middle name"

Knowing I have two commas in my name scheme, here is a function I wrote that I thought should work:

Code:
string changeNameformat(string theFullName)

{
	string lastNameOnly;
	string firstNameOnly;
	string middleNameOnly;
	string middleAndLastNameonly;

	firstNameOnly = theFullName.substr(0, theFullName.find(","));
	middleAndLastNameonly = theFullName.substr(theFullName.find(",") + 2, theFullName.length() - firstNameOnly.length() - 2);
	middleNameOnly = middleAndLastNameonly.substr(middleAndLastNameonly.find(","));
	lastNameOnly = middleAndLastNameonly.substr(middleAndLastNameonly.find(",") + 2, middleAndLastNameonly.length() - middleAndLastNameonly.length() - 2);

	return ""name format I want"

If I read a list of names using getline(filestream,string1), whereas string 1 is the main string variable, then attempt to output
Code:
 cout << string2 << endl;

		string2 = changeNameformat(string1);
I get an execution error.

If I do a cout<< asking for the user to input the name in the format "first name, middle name, last name" it works and compiles, but the function doesn't return the change in format, instead giving me an output of literally "first name, last name".

So....what am I doing wrong? Been working on this for about 2 days now, and my professor hasn't answered my emails yet.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
A few thoughts: according to the documentation substr takes 1 or 2 arguments. If you only provide the first argument, it keeps going until it reaches the end of the string. So middleNameOnly is really lastName (with the comma and space).

Second, find takes one or two arguments. The second argument is where to start searching (0 if not provided). So, you don't need to cut out the middleAndLastName into a seperate string.

Third, order matters. Printing string2 before setting it to the result of your function isn't likely to work well. (Probably a null pointer error of some sort).

Fourth, You probably should actually specify what to return.


Edit: oh, and also, you need to learn to use the debugger. After all, it is so much easier to understand what is happening if you can see the values at various points in the program.
 

Pocky Is God

Ars Legatus Legionis
24,457
Hey thanks.

I purposely left the return statement out of my code, since it wasn't returning anything anyways. So if I don't need the separate string for middleandlastname, how best can I divy up the original format of name, middle, last to reorganize into last name, first name middle name. I figured using the two commas to separate everything out SHOULD have worked, but alas, it does not.

As for giving the result of the function back to the string2 for my cout<< statement, is there anything else that I need to do other than call the function in int main()?
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Hey thanks.

I purposely left the return statement out of my code, since it wasn't returning anything anyways. So if I don't need the separate string for middleandlastname, how best can I divy up the original format of name, middle, last to reorganize into last name, first name middle name. I figured using the two commas to separate everything out SHOULD have worked, but alas, it does not.

By grabbing each name from the original string directly. From the start to the first comma is the first name, from the first comma + 2 to the second comma is the middle name, and from the second comma + 2 to the end is the last name. Three calls to substr.

And you can get the second comma location with the two argument version of findfind. Just tell it to start searching after the first comma.

As for giving the result of the function back to the string2 for my cout<< statement, is there anything else that I need to do other than call the function in int main()?

That would depend on exactly what the assignment was. But, for a test program, something like (keep in mind, this isn't exact; for instance I am leaving off the argc and argv):

Code:
int main() {
  // get CommaSeperatedNames from somewhere
  string reversedNames = reverseNames(commaSeperatedNames)
  cout << reversedNames << endl;
}