I'm new and don't know where to start, assignment due soon.

Write your question here.

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.

jude6363jr (1)
Write your question here.

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.
You are being ill-advised to use .eof(). You should use getline() as the condition statement within a loop.

Something like:

1
2
3
for (std::string str; std::getline(inFile, str); ) {
    // Process str here
}


What don't you understand about using str.find() and the other string methods?
See http://www.cplusplus.com/reference/string/string/ for further info.
"Reading from a file can also be performed in the same way that we did with cin"
http://www.cplusplus.com/doc/tutorial/files/
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



#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("Ch07_Pr03_DataIn.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();
}
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.

Perhaps:

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

using namespace std;

string getName(const string& name);

int main() {
	ifstream inData("Ch07_Pr03_DataIn.txt");

	if (!inData)
		return (cout << "Cannot open file\n"), 1;

	for (string fullname; getline(inData, fullname); )
		cout << getName(fullname) << '\n';
}

string getName(const string& fullName) {
	const auto junk { fullName.find(',') };
	const auto otherJunk { fullName.find(' ', junk + 2)};
	auto newnam { (otherJunk != string::npos ? fullName.substr(junk + 2, otherJunk - junk - 2) : fullName.substr(junk + 2)) + ' '};

	newnam += otherJunk != string::npos ? fullName.substr(otherJunk + 1) + ' ' : "";
	newnam += fullName.substr(0, junk);

	return newnam;
}

Topic archived. No new replies allowed.