Sorting Lists

Well, I'm mostly done with this project, it sorts a list of 25 students with their gpas and standing and such into alphabetical order, and calculates the gpa average.

I now only have to sort the freshmen, sophomores, juniors, and seniors into their own lists, alphabetize them and then calculate their gpa.

But I am having an issue getting them into their own lists.

I created a new instance of my class Students for each standing, either freshmen, junior, etc, with an array of how many there are (it's fixed).

Here is the whole of the code, I know it's messy and my coding style is crappy but eh. I marked with a comment where the program works up to, but here's a snippet of the faulty code, and the file information it reads. What the following snippet should do is get the last name, first name, and standing of the first person in the list, and put this information into a dummy instance of the class. It then should check if their standing is Freshmen, Sophomore or what have you. If it is a freshman (the first person in the list is) then it should get their final peice of information via the GPA function which also moves the virtual cursor down to the next line. It sets the information previously put into the dummy instance of the class into the correct place depending on the if statement, Freshman, or sophomore, etc.

However when I throw in some random couts to check if the data is all correct and everything...

http://i.imgur.com/CNWJtPg.png

It appears blank... I don't get it... at all...

Williams, Leonard - Freshman - 1.85 -
Smith, Shelia - Senior - 2.99 -
Anderson, Andy - Sophomore - 3.01 -
Wiser, Bud - Freshman - 4.00 -
Robertson, Jully - Junior - 2.78 -
Koran, Korn - Junior - 3.50 -
Smith, Sam - Junior - 2.14 -
Johnson, Jim - Junior - 3.05 -
Johnson, Jane - Junior - 3.75 -
Potter, Pam - Senior - 2.98 -
Brown, Bill - Sophomore - 2.55 -
Crooks, Cathy - Freshman - 1.99 -
Gregg, Howard - Senior - 2.44 -
Nicholas, Judy - Senior - 3.69 -
White, Bob - Sophomore - 1.64 -
Walsh, Fred - Junior - 4.00 -
Dennis, Susan - Senior - 2.06 -
Roberts, Rachel - Sophomore - 4.00 -
Fredericks, Mary - Freshman - 2.89 -
Holmes, Wendy - Senior - 2.56 -
Edwards, James - Sophomore - 3.00 -
Green, Barbara - Sophomore - 3.67 -
Brown, David - Freshman - 2.00 -
Williamson, Walt - Sophomore - 2.95 -
Carson, Jim - Sophomore - 2.03 -

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

	else if (affirm == 'f' || affirm == 'F')
		{
			int NumDummy = 0;
			int NumFresh = 0;
			int NumSen = 0;
			int NumJun = 0;
			int NumSoph = 0;

			ins.open(infile);

			do
			{

				

				TempStu2[NumDummy].GetLastName();
				TempStu2[NumDummy].GetFirstName();
				TempStu2[NumDummy].GetStanding();

				if (TempStu2[NumDummy].Standing == "Freshman")
				{
					Freshmen[NumFresh].GetGPA();
					Freshmen[NumFresh].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Freshmen[NumFresh].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Freshmen[NumFresh].Standing = TempStu2[NumDummy].Standing;

					NumFresh = NumFresh + 1;
				}
				else if (TempStu2[NumDummy].Standing == "Sophomore")
				{
					Sophomores[NumSoph].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Sophomores[NumSoph].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Sophomores[NumSoph].Standing = TempStu2[NumDummy].Standing;
					Sophomores[NumSoph].GetGPA();

					NumSoph = NumSoph + 1;

				}
				else if (TempStu2[NumDummy].Standing == "Junior")
				{
					Juniors[NumJun].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Juniors[NumJun].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Juniors[NumJun].Standing = TempStu2[NumDummy].Standing;
					Juniors[NumJun].GetGPA();

					NumJun = NumJun + 1;

				}
				else if (TempStu2[NumDummy].Standing == "Senior")
				{
					Seniors[NumSen].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Seniors[NumSen].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Seniors[NumSen].Standing = TempStu2[NumDummy].Standing;
					Seniors[NumSen].GetGPA();

					NumSen = NumSen + 1;
				}

				

			} while (!ins.eof());


			ins.close();
Last edited on
(Whole of the program)

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <assert.h>
#define infile "studatabase.txt"


using namespace std;

ifstream ins;

class Students
{

public:
	string StudentLastName;
	string StudentFirstName;
	string GPA;
	string Standing;


	void GetLastName()
	{
		getline(ins, StudentLastName, ',');

	}

	void GetFirstName()
	{
		getline(ins, StudentFirstName, '-');

	}
	void GetStanding()
	{
		getline(ins, Standing, '-');
	}

	void GetGPA()
	{
		getline(ins, GPA, '-');

		if (ins.eof())
		{
			ins.close();
		}

		ins.ignore(0, '\n');
	}


};


float ConvertGPA(string x)
{
	float TempFloat;

	stringstream convert(x);

	if (!(convert >> TempFloat));

	return(TempFloat);
}



int main()
{
	Students StuObj[25];
	Students TempStu;
	Students TempStu2[25];
	Students Freshmen[4];
	Students Seniors[5];
	Students Sophomores[7];
	Students Juniors[5];
	char affirm;
	int offset;
	char temp;
	string TempString = "NULL";
	int y;
	int x;
	float GradePointAverage[25];
	float Calculate;

	offset = 0;

	do
	{
		cout << "Which operation do you want to perform?" << endl;
		cout << "To read in information from studatabase.txt at the root directory, type r" << endl;
		cout << "To sort the master list and calculate GPA average, type s" << endl;
		cout << "To read in the sublists of Freshman, Sohpomores, etc., type f" << endl;
		cout << "To output read information both to a file and to the screen, type o" << endl;
		cin >> affirm;



		if (affirm == 'r' || affirm == 'R')
		{

			ins.open(infile);

			if (ins.fail())
			{
				cout << "File failed to open, please ensure existence of studatabase.txt, and then try again." << endl;

				break;
			}

			do
			{

				if (offset == 26)
				{
					ins.close();
					affirm = 'q';
					break;
				}

				StuObj[offset].GetLastName();
				StuObj[offset].GetFirstName();
				StuObj[offset].GetStanding();
				StuObj[offset].GetGPA();

				offset = offset + 1;

			} while (offset < 25);

			ins.close();

		}
		else if (affirm == 'o' || affirm == 'O')
		{

			offset = 0;

			cout << "Master List" << endl;

			do
			{
				cout << StuObj[offset].StudentLastName << ",";
				cout << StuObj[offset].StudentFirstName << endl;
				cout << StuObj[offset].GPA << endl;
				cout << StuObj[offset].Standing << endl;

				if (offset == 24) {
					cout << "Entire School GPA Average" << endl;
					cout << "About " << Calculate << endl;
				}


				offset = offset + 1;

			} while (offset < 25);
		}
		else if (affirm == 's' || affirm == 'S')
		{

			offset = 25;

			for (x = 0; x < (offset - 1); x++)
			{
				y = x;
				TempStu = StuObj[x];

				for (int inner = x + 1; inner < offset; inner++)
				{
					if (StuObj[inner].StudentLastName < TempStu.StudentLastName)
					{
						TempStu = StuObj[inner];
						y = inner;
					}
				}
				StuObj[y] = StuObj[x];
				StuObj[x] = TempStu;
			}


			int var;
			var = 0;
			offset = 0;



			while (var < 25 && offset < 25)
			{
				if (var == 25 || offset == 25)
				{
					break;
				}

				GradePointAverage[var] = ConvertGPA(StuObj[offset].GPA);

				var = var + 1;
				offset = offset + 1;
			}

			var = 0;
			Calculate = 0;

			do
			{


				Calculate = Calculate + GradePointAverage[var];

				var = var + 1;

			} while (var < 25);

			Calculate = Calculate / 25;

			Calculate = Calculate - 0.0008;

			//working up to here

		}
		else if (affirm == 'f' || affirm == 'F')
		{
			int NumDummy = 0;
			int NumFresh = 0;
			int NumSen = 0;
			int NumJun = 0;
			int NumSoph = 0;

			ins.open(infile);

			do
			{

				

				TempStu2[NumDummy].GetLastName();
				TempStu2[NumDummy].GetFirstName();
				TempStu2[NumDummy].GetStanding();

				if (TempStu2[NumDummy].Standing == "Freshman")
				{
					Freshmen[NumFresh].GetGPA();
					Freshmen[NumFresh].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Freshmen[NumFresh].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Freshmen[NumFresh].Standing = TempStu2[NumDummy].Standing;

					NumFresh = NumFresh + 1;
				}
				else if (TempStu2[NumDummy].Standing == "Sophomore")
				{
					Sophomores[NumSoph].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Sophomores[NumSoph].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Sophomores[NumSoph].Standing = TempStu2[NumDummy].Standing;
					Sophomores[NumSoph].GetGPA();

					NumSoph = NumSoph + 1;

				}
				else if (TempStu2[NumDummy].Standing == "Junior")
				{
					Juniors[NumJun].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Juniors[NumJun].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Juniors[NumJun].Standing = TempStu2[NumDummy].Standing;
					Juniors[NumJun].GetGPA();

					NumJun = NumJun + 1;

				}
				else if (TempStu2[NumDummy].Standing == "Senior")
				{
					Seniors[NumSen].StudentLastName = TempStu2[NumDummy].StudentLastName;
					Seniors[NumSen].StudentFirstName = TempStu2[NumDummy].StudentFirstName;
					Seniors[NumSen].Standing = TempStu2[NumDummy].Standing;
					Seniors[NumSen].GetGPA();

					NumSen = NumSen + 1;
				}

				

			} while (!ins.eof());


			ins.close();


		}
		else
		{
			cout << "Error, please try again" << endl;
		}

	} while (affirm != 'r' || affirm != 'R' || affirm != 'o' || affirm != 'O' || affirm != 's' || affirm != 'S' || affirm != 'f' || affirm != 'F');
	

}


Hello,
It's because your getStanding function would input " Freshmen ", " Senior ", etc instead of "Freshmen", "Senior".
Notice the space character before and after those words.
So it doesn't match in all your IF statements.

One of the solution is changing all the condition in your IF statements to match with " Freshmen " instead of "Freshmen".

Of course this might not be the best solution :)

Good luck,
Last edited on
Oh my god I feel silly.

Well, I won't make that mistake again... ugh.

Thank you so much, that fixed it, it now works as intended.

You've been by far the most helpful of any C++ site I've posted my problem on, others just poke fun at my coding style which is quite subjective although I realize organizing would help me find errors better, I still dislike methods.

ALL YOUR DATA IS BELONGING TO INT MAIN();
Last edited on
Topic archived. No new replies allowed.