Multiple problems with program.

This is and assignment for my class. You are required to make a program with a switch menu and some other content that was covered in class and while my menu works fine along with some other stuff you also need a save file and open file function. This is where my problems begin... when i import a file back to my program there seems to be a miscommunication the makes it so there is 5 + however many records were input and saved to said file and these files are just blank and they also mess up when they are displayed. Next, when i try to sort the records by name then only the names will switch not the whole record. also for an aesthetic note my columns seem to change places depending on the length of the value input.

Now for the real question, am i using the struct correctly within my display, my open file, my save file or even if my input info.


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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstring>
using namespace std;

struct Player {
	string name;
	int gamesPlayed;
	int points;
	int goals;
	int assists;
	int penaltyMinutes;
};


const int MAXSIZE = 30;

void openFile(Player[], int&, ifstream&);
void saveFile(const Player[], int, ofstream&);
void getInfo(Player[], int&, bool&);
void displayInfo(const Player[], int, bool);
void sortInfo(Player[], int, bool&);
void searchInfo(Player[], int, bool&);
void removeInfo(Player[], int&);
bool okToSearch(int size);
int indexOf(string target, Player athlete[], int size);
char menuStuff();

ofstream fout;
ifstream fin;


int main ()
{
	Player names[MAXSIZE];
	int numOfPlayers = 0;
	bool sorted = false;
	bool run = true;
	
	do
	{
		cout << "Stat Tracker Program  --  " << numOfPlayers <<
			 " players being tracker." << endl;
		switch ( menuStuff() )
		{
			case 'O': openFile(names, numOfPlayers, fin) ; break;
			case 'S': saveFile(names, numOfPlayers, fout) ; break;
			case 'A': getInfo(names, numOfPlayers, sorted) ; break;
			case 'D': displayInfo(names, numOfPlayers, sorted) ; break;
			case 'C': sortInfo(names, numOfPlayers, sorted) ; break;
			case 'F': searchInfo(names, numOfPlayers, sorted) ; break;
			case 'R': removeInfo(names, numOfPlayers) ; break;
			case 'Q': run = false; break;
			default : cout << "ERROR: INVALID ENTRY" << endl;
		}
	}
	while (run);
	cout << endl << "Program Terminated" << endl;
	
	system ("pause");
	
}

char menuStuff()
{
	char response;
	cout << endl << "Choose an option" << endl;
	cout << "(O)pen File, (S)ave File, (A)dd Players and Stats, (D)isplay Players and Stats,\n(C)atergoize Players and Stats," 
			" (F)ind Players and Stats,\n(R)emove Players and Stats, (Q)uit" << endl;
	cout << "> ";
	cin >> response;
	cin.ignore(256, '\n');
	return toupper(response);
}

void saveFile(const Player athlete[], int size, ofstream&)
{

	char filename[30];
	cout << "Please Enter a Filename: ";
	cin.getline(filename, 30);
	fout.open(filename, ios::app);
	
	if(!fout.fail())
	{
		cout << "Saving INFO to the disc.";
		for(int i=0; i < size; i++)
		{
			fout << athlete[i].name << ';'
				 << athlete[i].gamesPlayed << ';'
				 << athlete[i].points << ';'
				 << athlete[i].goals << ';'
				 << athlete[i].assists << ';'
				 << athlete[i].penaltyMinutes;
			
			if (i < size - 1) fout << endl;
		}
		cout << endl << athlete << " Players Written to Disk." << endl;
		fout.close();
		system("pause");
		system("cls");

	}
	else
	{
		cout << "ERROR: Problem with FILE" << endl;
		system("pause");
		system("cls");

	}
}

void openFile(Player athlete[], int& size, ifstream&)
{
	char filename[30];
	string str;
	stringstream strstrm;
	
	cout << "Please enter the FILE you would like to open: ";
	cin.getline(filename, 30);
	fin.open(filename);
	if (!fin.fail())
	{
		system("cls");
		cout << "Reading from Disc";
		size  = 0;
		while(!fin.eof() && size < MAXSIZE)
		{
			getline(fin, str, ';');
			athlete[size].name = str;
			
			getline(fin, str, ';');
			strstrm.str(""); strstrm.clear();
			strstrm << str;
			strstrm >> athlete[size++].gamesPlayed;
			
			getline(fin, str, ';');
			strstrm.str(""); strstrm.clear();
			strstrm << str;
			strstrm >> athlete[size++].points;
			
			getline(fin, str, ';');
			strstrm.str(""); strstrm.clear();
			strstrm << str;
			strstrm >> athlete[size++].goals;
			
			getline(fin, str, ';');
			strstrm.str(""); strstrm.clear();
			strstrm << str;
			strstrm >> athlete[size++].assists;
			
			getline(fin, str, ';');
			strstrm.str(""); strstrm.clear();
			strstrm << str;
			strstrm >> athlete[size++].penaltyMinutes;
		}
		cout << endl << size << " Players from the disc" << endl;
		
		system("pause");
		system("cls");
	}
	else
	{
		cout << "ERROR: Cannot open FILE" << endl;
		system("pause");
		system("cls");
	}
	
}

void getInfo(Player athlete[], int& size, bool& sorted)
{
	Player tmp;
	char response;
	char str[256];
	if (size < MAXSIZE)
	{
		system("cls");
		cout << "Input Player Info" << endl << endl;
		cout << "Name: ";
		cin.getline(str, 256, '\n');
		tmp.name = str;
		cout << "Games Played: ";
		cin >> tmp.gamesPlayed;
		cout << "Goals:	";
		cin >> tmp.goals;
		cout << "Assists: ";
		cin >> tmp.assists;
		cout << "Points: ";
		cin >> tmp.points;
		cout << "Penalty Minutes: ";
		cin >> tmp.penaltyMinutes;
		cout << endl;
		cout << "Add this Player to the Tracker? (y/n) ";
		cin >> response;
		if (toupper(response) == 'Y')
			athlete[size++] = tmp;
		}
		else
		{
			cout << "Tracker at max CAPACITY; Cannot enter more Players." << endl;
			system("pause");
		}
	system("pause");
	system("cls");
}
void displayInfo(const Player athlete[], int size, bool sorted)
{
	system("cls");
	
	if(size < 1)
	{
		cout << "Nothing to display" << endl;
	}
	else
	{
		cout << "Listing of Players being Tracked" << endl << endl;
		cout << fixed << setprecision(2);
		cout << "Player Name         Games Played	Goals	Assists	  Points    Penalty Minutes" << endl;
		cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
		
		cout << left;
		for (int i = 0; i < size; i++)
		{
			cout << athlete[i].name << right
				 << setw(15) << athlete[i].gamesPlayed
				 << setw(8) << athlete[i].goals
				 << setw(13) << athlete[i].assists
				 << setw(10) << athlete[i].points
				 << setw(15) << athlete[i].penaltyMinutes << left << endl;
		}
		
		cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
		cout << size;
		cout << " Players Tracked" << endl;
	}
	system("pause");
	system("cls");
}
void sortInfo(Player athlete[], int size, bool& sorted)
{
    int i, j, min;
	for (i = 0; i < (size-1); i++) 
	{
    	min = i;
    	for (j = (i+1); j < size; j++) 
		{
      		if(athlete[j].name < athlete[min].name) 
			  min = j;
    	}
   		 if (i != min) swap(athlete[i].name, athlete[min].name);
  	}
	cout << "Names Sorted!";
	sorted = true;
}
void searchInfo(Player athlete[], int size, bool& sorted)
{
	if(okToSearch(size))
	{
		string name;
		int ndx;
		cout << "Enter the Player you want to find: ";
		getline(cin, name);
		ndx = indexOf(name, athlete, size);
		if(ndx < size)
			cout << name << " found at line " << ndx+1 << "." << endl;
		else
			cout << name << " was not found." << endl;
	}
	else 
	{
		cout << "Please sort before searching.";
	}
}
void removeInfo(Player athlete[], int& size)
{
	
	string name;
	int ndx;
	
	cout << "Enter the name you want to delete: ";
	getline (cin, name);
	
	
	if(ndx < size)
	{
		for(int i = ndx; i < size-1; i++)
		{
			athlete[i] = athlete[i+1];
		}
		size--;
		cout << name << " has been removed." << endl;
	}
	else
	cout << name << " was not found." << endl;
	
}

bool okToSearch(int size)
{
	if(size == 0)
	{
		cout << "The Tracker is Empty." << endl
			 << "Please add Players before searching." << endl;
		return false;
	}
	else 
	{
		return true;
	}
}

int indexOf(string target, Player athlete[], int size)
{
	for (int i = 0; i < size; i++)
		if (athlete[i].name == target) return i; 
  	return size;
}


Any help would be greatly appreciated.
Thanks in advance.
closed account (48T7M4Gy)
Now for the real question, am i using the struct correctly within my display, my open file, my save file or even if my input info.


I'm sure you're genuine and mean well but the real question is 'have you tested your program'. What data have you tested it on and what results did you get. There is a good chance if it runs without errors on sample data (we don't have it) and you have printed out the data and it is to your expectations then everything is fine.

If it isn't performing and you can't work out why then that's where we can help. Otherwise ...

Cheers :)
Sorry haven't done this before.
When i just try to open a text file that was saved as test.txt and display the input data (in this case Name: Luke, Games Played: 13, Goals: 12, Assists: 12, Points: 24, Penalty Minutes: 12) was displayed like so,

Listing of Players being Tracked

Player Name         Games Played        Goals   Assists   Points    Penalty Minutes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Luke             13       0            0         0              0
              02046820731            0        24              0
              3      12            0         0              0
              0       0           12         0              0
           -376    -373            0         0             12
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 Players Tracked
Press any key to continue . . .


but if the program was run on its own without opening a file and just display the input then it turns out to the desired effect,(same input)


Listing of Players being Tracked

Player Name         Games Played        Goals   Assists   Points    Penalty Minutes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Luke             13      12           12        24             12
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 Players Tracked
Press any key to continue . . .


now for the sorting problem (input 1 Name:Luke, Games Played: 13, Goals:12, Assists:12, Points: 24, Penalty Minutes:12) (Intput 2 Name:John, Games Played: 45, Goals: 22, Assists: 54, Points: 76, Penalty Minutes: 66) the display output before


Listing of Players being Tracked

Player Name         Games Played        Goals   Assists   Points    Penalty Minutes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Luke             13      12           12        24             12
John             45      22           54        76             66
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 Players Tracked
Press any key to continue . . .


and the output after sorting


Listing of Players being Tracked

Player Name         Games Played        Goals   Assists   Points    Penalty Minutes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
John             13      12           12        24             12
Luke             45      22           54        76             66
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 Players Tracked
Press any key to continue . . .


For the sorting problem i know what it is just don't know how to fix it.
hopefully this helps more than my first set of information. Also as for the test data. I have been consistently using these 2 inputs.

Thanks again.
Last edited on
closed account (48T7M4Gy)
Taking the first display I think you have something wrong with you data file and if you lpost it here I can check. Just cut and paste it from the text file as is.
Luke;13;24;12;12;12

is what is in the text file after it is saved.
closed account (48T7M4Gy)
OK I'll have a look by running your program. I am surprised your text file has only one player.

One observation right at the start is the ;'s in your data. That is probably where the problem lays.
Last edited on
closed account (48T7M4Gy)
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
vvoid openFile(Player athlete[], int& size, ifstream&)
{
	char filename[30];
	cout << "Please enter the FILE you would like to open: ";
	cin >> filename;

	fin.open(filename);
	if (!fin.fail())
	{
		size = 0;
		while (!fin.eof() && size < MAXSIZE)
		{
			fin >> athlete[size].name;
			fin >> athlete[size].gamesPlayed;
			fin >> athlete[size].points;
			fin >> athlete[size].goals;
			fin >> athlete[size].assists;
			fin >> athlete[size].penaltyMinutes;
			size++;
		}
		cout << endl << size << " Players from the disc" << endl;
	}
	else
		cout << "ERROR: Cannot open FILE" << endl;
}


If you change to this and remove the ;'s replacing them with spaces I think most of the troubles arte solved.
Last edited on
closed account (48T7M4Gy)
Data file:

Gladys 12 23 11 10 9
Luke 13 24 12 12 12
Alice 4 18 9 7 13
Bill 8 8 16 4 10
MARY 9 1 2 4 18



Output to screen:

Listing of Players being Tracked

Player Name         Games Played        Goals   Assists   Points    Penalty Minutes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Gladys             12      11           10        23              9
Luke             13      12           12        24             12
Alice              4       9            7        18             13
Bill              8      16            4         8             10
MARY              9       2            4         1             18
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 Players Tracked
Press any key to continue . . .


This is just formatting and output order on each line. No big deal for you to fix by shuffling the struct attributes around.
that worked Thank you so much :)
closed account (48T7M4Gy)
With your sort function you need to swap all the associated attributes, not just the name as the attribute you are sorting on.
yeah thats what i was just thinking but thanks again
closed account (48T7M4Gy)
Cheers :)
Topic archived. No new replies allowed.