having trouble reading file with a function

I have this assignment that works, however, it only outputs the last name on my list. my input file has about 6 names on it. They are formatted: lastname, firstname middlename (the middle name is optional. It may not be in the file, so it may not be output).

So my issue is I only get the last name output. It works BEAUTIFULLY for that. Unfortunately I need all the names output.

Any help would be GREATLY appreciated!

I know the last function printNames isn't done yet. I plan on doing that after I get this issue resolved.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

		// declare functions
		// void function to open the input and output files
		void openFiles (ifstream& input, ofstream& output);
		// void function to read the names in the file
		void readNames (ifstream& input, string& fullname);
		// void function to change the name format
		void changeNames (ifstream& input, string fullname, string firstname,
			string middlename, string lastname);
		// void function to output the results to the output file
		void printResults (string&, string&);

int main()
{
	// declare variables within main
	string fullname, firstname, middlename, lastname;
	ifstream input;
	ofstream output;
	openFiles (input, output);
	readNames (input, fullname);
	changeNames (input, fullname, firstname, middlename, lastname);

	
	

system ("pause");
}

		// make a function to open the input and ouput files
		void openFiles (ifstream& input, ofstream& output)
		{
			input.open ("input.txt");
			// check to see that the input file can be opened
				if ( ! input )
				{
					// if it can't, give an error message
					cout << "Input file could not be opened! ";
					// and close the program
					exit (1);
				}
			output.open("output.txt");
			// check to see that the output file can be opened
				if (! output)
				{
					// if it can't, give an error message
					cout <<"The output file could not be opened! ";
					// and close the program
					exit (1);
				}
		}
		// have a function read the names and get the name size
		void readNames (ifstream& input, string& fullname)
		{
			while (getline(input, fullname));
		}
		void changeNames (ifstream& input, string fullname, string firstname,
			string middlename, string lastname)
		{
			int comma = fullname.find(',');
			int length = fullname.length();
			lastname = fullname.substr (0, comma);
			int space = fullname.find(' ', comma +2);
				do
				{
					input>>fullname;
						if (space!= -1)
						{
						firstname = fullname.substr(comma + 2, space - comma -2);
						middlename = fullname.substr(space + 1, length);
						cout<< firstname <<", "<<middlename <<", ";
						}
						else 
						{
						firstname = fullname.substr(comma + 2, length);
						cout<< firstname <<", ";
						}
						cout<<lastname<<endl;
						}
				while (input);
			
		}
Last edited on
57
58
59
60
61
		// have a function read the names and get the name size
		void readNames (ifstream& input, string& fullname)
		{
			while (getline(input, fullname));
		}


What is line 60 supposed to do? It throws away everything but the last line.
I'm not too sure. I thought we were supposed to have 4 functions:
one to open the files
one to read the names
one to convert the names
one to print the results...

when i remove the "void readNames" function all i get is commas for an output.

Where do I need to look for the correct way to read the names?
Hint: Have you considered calling the functions "readName()" and "changeName()"?
what would that change?

Well, if the functions processed one line per call, then maybe you could restructure your program a little such that you could process all lines...
Topic archived. No new replies allowed.