program troubles

For this program I have to set the class and blank array that I need to load from a file. I'm having a few problems:
1. displayPresident function. Teacher made it seem I could call the President constructor and display using this code. However it is not working.
2. I need serious with the while loop the reads from file to the array and sets the class names from the file.
3. anything else that is messing me up. I know this completely messed up and I have been struggling for hours trying to get this to work.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class President
{
private:
	string first_name;
	string last_name;
	int begin_year;
	int end_year;
	string party_affil;
public:
	President();
	void setFirstName(string);
	void setLastName(string);
	void setBeginYear(int);
	void setEndYear(int);
	void setPartyAffil(string);
	string getFirstName();
	string getLastName();
	int getBeginYear();
	int getEndYear();
	string getPartyAffil();
};

void displayPresident();
void outputPresident(fstream &outFile);
void loadData(President pres_array[], const int SIZE, int count);
void outputData(President pres_array[], int count);

int main()
{
	const int SIZE = 40;
	int count = 0;
	President pres_array[SIZE];
	loadData(pres_array, SIZE, count);
	outputData(pres_array, count);
	return 0;
}


//********************************************************************
void President::displayPresident()
{
cout << left;
cout << setw(15) << "First Name: "
<< first_name << endl;
cout << setw(15) << "Last Name: "
<< last_name << endl;
cout << setw(15) << "Begin Year: "
<< begin_year << endl;
cout << setw(15) << "End Year: "
<< end_year << endl;
cout << setw(15) << "Party: "
<< party_affil << endl;
cout << endl;
}
//********************************************************************
void outputPresident(fstream &outFile)
{
fstream outFile;
outFile.open("a5.txt", ios::app);
outFile << left;
outFile << setw(15) << "First Name: "
<< first_name << endl;
outFile << setw(15) << "Last Name: "
<< last_name << endl;
outFile << setw(15) << "Begin Year: "
<< begin_year << endl;
outFile << setw(15) << "End Year: "
<< end_year << endl;
outFile << setw(15) << "Party: "
<< party_affil << endl;
outFile << endl;

outFile.close();

}
//********************************************************************
void loadData(President pres_array[], const int SIZE,
	int count)
{
	fstream inFile;
	inFile.open("prez_data", ios::in);

	string 	first,
		last,
		party;
	int	begin,
		end,
		tempn;


	while (!inFile.eof() && count < SIZE)
	{
		getline(inFile, first);
		pres_array[tempn].setFirstName(first);
		getline(inFile, last);
		pres_array[tempn].setLastName(last);
		inFile >> begin;
		pres_array[tempn].setBeginYear(begin);
		inFile >> end;
		pres_array[tempn].setEndYear(end);
		inFile.ignore();
		getline(inFile, party);
		pres_array[tempn].setPartyAffil(party);

		tempn = pres_array;
		system("pause");
		++count;
	}
	inFile.close();
}
//**********************************************************************
void outputData(President pres_array[], int count)
{
	fstream outFile;
	outFile.open("a5.txt", ios::out, ios::app);

	outFile << "President Listing: " << endl;
	outFile << endl;

	for (int i = 0; i < count; i++)
	{
		outFile << left;
		outFile << setw(15) << "First Name: "
			<< pres_array[i].getFirstName() << endl;
		outFile << setw(15) << "Last Name: "
			<< pres_array[i].getLastName() << endl;
		outFile << setw(15) << "Begin Year: "
			<< pres_array[i].getBeginYear() << endl;
		outFile << setw(15) << "End Year: "
			<< pres_array[i].getEndYear() << endl;
		outFile << setw(15) << "Party: "
			<< pres_array[i].getPartyAffil() << endl;
		outFile << endl;
	}
	outFile.close();
}
//**********************************************************************
void President::setFirstName(string f)
{
	first_name = f;
}
//**********************************************************************
void President::setLastName(string l)
{
	last_name = l;
}
//**********************************************************************
void President::setBeginYear(int b)
{
	begin_year = b;
}
//**********************************************************************
void President::setEndYear(int e)
{
	end_year = e;
}
//**********************************************************************
void President::setPartyAffil(string p)
{
	party_affil = p;
}
//**********************************************************************
string President::getFirstName()
{
	return first_name;
}
//**********************************************************************
string President::getLastName()
{
	return last_name;
}
//**********************************************************************
int President::getBeginYear()
{
	return begin_year;
}
//**********************************************************************
int President::getEndYear()
{
	return end_year;
}
//**********************************************************************
string President::getPartyAffil()
{
	return party_affil;
}
//**********************************************************************
President::President()
{
	first_name = "";
	last_name = "";
	begin_year = 0;
	end_year = 0;
	party_affil = "";

the input looks like this:
string line (First Name)
string line (last Name)
int (year)
int (year)
string line (party affiliation)

This pattern repeats 17 times. I have to use getline because some of the lines in the file for (first, last, and party) have more than one word on the line.

I really appreciate the help!!
What are the contents of your input file?
Last edited on
George
Washington
1789
1797
Federalist
James
Madison
1809
1817
Democrat Republican
Millard
Fillmore
1850
1853
Whig
Andrew
Jackson
1829
1837
Democrat
Harry
S. Truman
1945
1953
Democrat
William Henry
Harrison
1841
1841
Whig

Here are a few sets of the repeating pattern
What are the contents of the file "a5.txt" when you call outputData()?
Last edited on
it doesn't ouput right now because I can't get my loop to work. However it should output the input file to the output file
Last edited on
1
2
3
	int	begin,
		end,
		tempn;


Should be :
1
2
3
	int	begin,
		end,
		tempn = 0;
I have tried that and it did not work. Also tried it as a for loop, tried a for loop nested in the while loop, nothing I try wants to work.
1
2
void loadData(President pres_array[], const int SIZE,
	int count)


Should be :
1
2
void loadData(President pres_array[], const int SIZE,
	int &count)
Hello nyork3415,

Online 38 you create an array of type President then in the displayPresident function you output first_name, but which one because pres_array where you would get first_name from is an array.

SakurasouBusters is half correct

Should be :
1
2
3
int	begin,
	end,
	tempn = 0;


Should actually be:
1
2
3
int	begin{ 0 },
	end{ 0 },
	tempn{ 0 };

where tempn must be set to zero before it is used in the while loop. Otherwise you could be trying to store something outside the boundary of the array the first time through the loop. And all variables should be initialized before they are used anywhere they are defined. It is just good practice.

line 111 will not work. You are trying to set int tempn equal to an array of type President. It should be either ++tempn or tempn++ which ever way you like.

Hope that helps,

Andy
1
2
3
tempn = pres_array;
system("pause");
++count;


Should be :
1
2
3
4
tempn = pres_array;
system("pause"); // Why have to pause the program???
++tempn;
++count;


Your code even does not compile. Should have let us know it sooner.
Anyways, there are numerous problems you have to fix.
Last edited on
Topic archived. No new replies allowed.