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:
If I read a list of names using getline(filestream,string1), whereas string 1 is the main string variable, then attempt to output
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.
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);
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.