You are given a file consisting of students’ names in the following form:
lastName, firstName middleName.
(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 each student’s entire name into a single variable. (Review the getline() function in the Chapter 03 textbook .pdf.)
It must use a function that takes as input a string consisting of the student’s entire name, and returns a string consisting of the altered name.
Use the str.find() function to find the index of “,” (the comma after the lastName);
Use the str.length() function to find the length of the string;
And use the str.substr() to extract the firstName, middleName, and lastName.
Examine the inFile.eof() function in the Chapter 05 textbook .pdf to learn how to use it to read to the end of an input file in a while-loop.
You are given a file consisting of students’ names in the following form:
lastName, firstName middleName.
(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 each student’s entire name into a single variable. (Review the getline() function in the Chapter 03 textbook .pdf.)
It must use a function that takes as input a string consisting of the student’s entire name, and returns a string consisting of the altered name.
Use the str.find() function to find the index of “,” (the comma after the lastName);
Use the str.length() function to find the length of the string;
And use the str.substr() to extract the firstName, middleName, and lastName.
Examine the inFile.eof() function in the Chapter 05 textbook .pdf to learn how to use it to read to the end of an input file in a while-loop.
well, can you open the file and print its contents to the screen as a starting point?
What do you not know how to do, what have you done so far? Working with a file in an assignment means you have to have *some* background and previous work ... start on it, make a main program, then look up <ifstream> and getline() and see if you can just read the file and dump it to the screen. Post that, then ask a question on what you don't understand.
I made this so far but theres a weird thing that shows up after I get the answers I wanted.... This shows up: terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
bash: line 1: 73 Aborted (core dumped) ./a.out
it is crashing. Probably because you are still trying to use the eof() idea, which is troublesome.
try the while(geline) idea instead of do/while/eof. Also consider checking the result of find for npos (it was not found, do not use). likely it goes past end of file, reads a big fat nothing, searches the nothing for a comma, does not find one, then ask for a *very, very large* substring on an empty string using 'npos' as 'junk' ... and that crashes. Your debugger can confirm this, or you can just add an if () around the find to avoid taking substrings that don't exist, or a little easier, if(fullName != "") may work as well.
When posting code, please use code tags so that the code is readable!
[code]
// code goes here
[/code]
There are various issues with using the string functions - mainly around obtaining the firstName - as you are trying to get a string starting past the end! Also, you are asked for a function that takes a string param and returns a string containing the re-fromatted string.