Stuck on project

I have this project to do for my c++ class but I am struggling to get the program to read input from a file.
This is my assignment: Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data from a file (You will need to create the input file) and functions to output data to the console. Add functions to search the array to find the index of a specific player, and update the data of a player. Before the program terminates, give the user the option to save data in a file (write a function for this). Your program should be menu driven, giving the user various choices. Your main function should mostly have variable declarations and function calls in it. Most operations should be handled by other functions.



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

using namespace std;

struct player {
		string name;
		string position;
		int touchdowns;
		int catches;
		int pass;
		int receive;
		int rush;
	};

		int addData( ifstream& ifile, int count , player football[])
		{
				if (count == 0)
				{
					ifile >> football[count].name;
					ifile >> football[count].position;
					ifile >> football[count].touchdowns;
					ifile >> football[count].catches;
					ifile >> football[count].pass;
					ifile >> football[count].receive;
					ifile >> football[count].rush;
					count++;
				}
				else if (count != 0)
				{
					int i = 0;
					string name;
					string position;
					int touchdowns;
					int catches;
					int pass;
					int receive;
					int rush;

					while (i != -1)
					{
						ifile >> name >> position >> touchdowns >> catches >> pass >> receive >> rush;
						if((*(&football + 1) - football) > 0)
						{
							if (name == football[i].name)
							{
							i++;
							}
							if (name != football[i].name)
							{
							football[count].name = name;
							football[count].position = position;
							football[count].touchdowns = touchdowns;
							football[count].catches = catches;
							football[count].pass = pass;
							football[count].receive = receive;
							football[count].rush = rush;
							i = -1;
							count++;
							}
						}
					}
				}
				return count;
			}

	void display(int count, player football[])
	{
			cout << "There are " << count << " players," << endl;
			if (count <= 0)
			{
				cout << "No players to display. ";
			}
			else
			{
				for (int i = 0; i < count;i++)
				{
					cout << i + 1 <<". " << football[i].name << endl;
					cout << "Position: " << football[i].position << endl;
					cout << "Touchdowns: " << football[i].touchdowns << endl;
					cout << "Catches: " << football[i].catches << endl;
					cout << "Passes: " << football[i].pass << endl;
					cout << "Receiving Yards: " << football[i].receive << endl;
					cout << "Rushing Yards: " << football[i].rush << endl;
				}
			}
	}

	void findandUpdate(string search, int count, player football[])
	{
			string name;
			string position;
			int touchdowns;
			int catches;
			int pass;
			int receive;
			int rush;

			bool found = false;

			for (int i = 0; i < count; i++)
			{

				if (search == football[i].name)
				{
					found = true;
					cout << "Searched player is at index " << i + 1 << endl;
					cout << "You may now update the player's name, player's position, number of touchdowns, number of catches,"
						<< "number of passing yards, number of receiving yards, and the number of rushing yards \n";
					cout << "Name: "; cin >> name;
					cout << "Position: "; cin >> position;
					cout << "Touchdowns: "; cin >> touchdowns;
					cout << "Catches: "; cin >> catches;
					cout << "Passes: "; cin >> pass;
					cout << "Received: "; cin >> receive;
					cout << "Rushed: "; cin >> rush;

					football[i].name = name;
					football[i].position = position;
					football[i].touchdowns = touchdowns;
					football[i].catches = catches;
					football[i].pass = pass;
					football[i].receive = receive;
					football[i].rush = rush;
				}
			}
			if (found == false)
			{
				cout << "No results.";
			}
	}

		int main()
		{
			cout << "Press 1 to add a player.\nPress 2 to display players.\nPress 3 to find a player.\nPress 4 to update player information.\nPress 5 to save data.\n";
			int x = 0;
			cin >> x;

			ifstream ifile;
			player football[10];
			ifile.open("footballplayers.txt");

			int i = 0;
			int count = 0;

			while (i != -1)
			{
				switch (x)
				{
				case 1:
				{
					count = addData(ifile, count, football);
				}
				break;
				case 2: display(count, football);
					break;
				case 3:
				{
					string name;
					cout << "Enter football player name: ";
					cin >> name;
					findandUpdate(name,count, football);
				}
						break;
				default: i = -1;
				}
			}
			ifile.close();

		string answer;
		cout << "Save data? Type yes or no.";
		cin >> answer;
		if (answer == "yes")
		{
			ofstream ofile;
			ofile.open("SavePlayerData.txt");
			for (int i = 0; i < count; i++) {
				ofile << football[i].name + " ";
				ofile << football[i].name + " ";
				ofile << football[i].position + " ";
				ofile << football[i].touchdowns + " ";
				ofile << football[i].catches + " ";
				ofile << football[i].pass + " ";
				ofile << football[i].receive + " ";
				ofile << football[i].rush + " ";
				ofile << "\n";
			}
			ofile.close();
		}
	}
Last edited on
should line 45 have an i in it?
what is that even supposed to do, that line is really odd. It looks like C code written to prank someone. (I know what it does, but I want to know what you think it does).
Last edited on
What is the format of the data file you are using - please post a sample.

If name can contain a space, then >> name will only extract up to the white-space char.

If you have an if statement like if (count == 0), then if count != 0 the else part will be executed and you know in the else that count != 0. There's no need to test for count != 0 in the else as it will always be true.
In addition to the comments by jonin and seeplus:

save data in a file (write a function for this)

Where is your function for this?

Lines 180-181: Why do you write the name twice?
This is going to cause problems when you read the file back.

Consider this:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
constexpr int MAX_PLAYERS = 10;
const string PlayerData = "footballplayers.txt";	//	Global

//*********************************************************************
//	Player.h
//*********************************************************************
class Player 
{	string name;
	string position;
	int touchdowns{};
	int catches{};
	int pass{};
	int receive{};
	int rush{};
public:
	void Display(int n);
	bool ReadData(ifstream& ifile);
	void Input();
	void SavePlayer(ofstream& ofile);
	bool Match(const string & who) const;
};

//*********************************************************************
//	Team.h
//*********************************************************************
class Team
{	int count{};
	Player	players[MAX_PLAYERS];
public:
	void Load();
	bool Menu();
	bool AddOnePlayer (const Player& temp);	
	void PromptForPlayer();		
	bool AddPlayer();			//	1
	void DisplayAll();			//	2
	void FindPlayer();			//	3
	void UpdatePlayer();		//	4
	bool SaveData();			//	5
};

//*********************************************************************
//	Player.cpp
//*********************************************************************
void Player::Display(int n)
{
	cout << n+1 << ".   " << name << endl;
	cout << "Position: " << position << endl;
	cout << "Touchdowns: " << touchdowns << endl;
	cout << "Catches: " << catches << endl;
	cout << "Passes: " << pass << endl;
	cout << "Receiving Yards: " << receive << endl;
	cout << "Rushing Yards: " << rush << endl;
	cout << endl;
}

bool Player::ReadData(ifstream& ifile)
{
	ifile >> name >> position >> touchdowns >> catches >> pass >> receive >> rush;
	return ifile.good();
}

void Player::SavePlayer(ofstream& ofile)
{
	ofile << name << " " << position << " " << touchdowns << " " << catches 
		  << " " << pass << " " << receive << " " << rush << endl;
}

void Player::Input()
{	cout << "Name: "; cin >> name;
	cout << "Position: "; cin >> position;
	cout << "Touchdowns: "; cin >> touchdowns;
	cout << "Catches: "; cin >> catches;
	cout << "Passes: "; cin >> pass;
	cout << "Received: "; cin >> receive;
	cout << "Rushed: "; cin >> rush;
}

bool Player::Match(const string& who) const
{
	return (name == who);
}

//*********************************************************************
//	TEAM.cpp
//*********************************************************************
// 
//	Load all players from file
//	
void Team::Load()
{	Player		temp;
	ifstream	ifile;

	ifile.open(PlayerData);
	if (!ifile.is_open())
	{	cout << PlayerData << " Not found" << endl;
		cout << "It will be created when you do a SAVE." << endl;
		return;
	}
	do
	{	if (!temp.ReadData(ifile))
			break;		//	Read failed
		if (!AddOnePlayer(temp))
			break;		//	Array full
	} while (ifile.good());
	cout << count << " Players loaded" << endl;
}

//	Add one player to the player array
bool Team::AddOnePlayer(const Player& temp)
{
	if (count == MAX_PLAYERS)
	{
		cout << "Too many players" << endl;
		return false;
	}	
	players[count] = temp;
	count++;
	return true;
}

//
//	1	Add Player
//
bool Team::AddPlayer()
{	Player		temp;

	cout << "Please input the player's name, position, number of touchdowns, number of catches,"
		 << "number of passing yards, number of receiving yards, and the number of rushing yards \n";
	temp.Input();
	return AddOnePlayer(temp);
}

//	
//	2	Display all players
//
void Team::DisplayAll()
{	
	if (count == 0)
		cout << "No players to display. ";
	else
		cout << "There are " << count << " players," << endl;
	for (int i = 0; i < count; i++)
		players[i].Display(i);
}

//
//	3	Find a player
//
void Team::FindPlayer()
{	Player	temp;
	bool	found = false;
	string	who;

	cout << "Player name to search for: ";
	cin >> who;
	for (int i = 0; i < count; i++)
	{	if (players[i].Match(who))
		{	found = true;
			players[i].Display(i);
		}
	}
	if (found == false)
		cout << "No results.";	
}

//
//	4	Update a player
//
void Team::UpdatePlayer()
{
	Player	temp;
	bool	found = false;
	string	who;

	cout << "Player name to update: ";
	cin >> who;
	for (int i = 0; i < count; i++)
	{
		if (players[i].Match(who))
		{	found = true;
			players[i].Display(i);
			cout << "Please update the player's name, position, number of touchdowns, number of catches,"
				 << "number of passing yards, number of receiving yards, and the number of rushing yards \n";
			players[i].Input(); 		
		}
	}
	if (found == false)
		cout << "No results.";
}

//	
//	5	Save all players to file
//
bool Team::SaveData()
{	ofstream ofile;

	ofile.open(PlayerData,ios::trunc);
	if (!ofile.is_open())
		return false;
	for (int i = 0; i < count; i++)
		players[i].SavePlayer(ofile);
	ofile.close();
	cout << count << " Players saved." << endl;
	return true;
}

bool Team::Menu()
{
	int x = 0;

	cout << "Press 1 to add a player.\n";
	cout << "Press 2 to display all players.\n";
	cout << "Press 3 to find a player.\n";
	cout << "Press 4 to update player information.\n";
	cout << "Press 5 to save data.\n";
	cout << "Press 6 to exit.\n";
	cin >> x;
	switch (x)
	{
	case 1: AddPlayer();
		break;
	case 2: DisplayAll();
		break;
	case 3: FindPlayer();
		break;
	case 4: UpdatePlayer();
		break;
	case 5: SaveData();
		break;
	case 6: return false;	//	Exit menu
	default: cout << "Invalid option" << endl;
	}
	return true;	//	Call menu again
}

//*********************************************************************
//	Main.cpp
//*********************************************************************
int main()
{	Team	team;

	team.Load();
	while (team.Menu())
		;
	if (!team.SaveData())
		return 2;	//	Could not open output file
	return 0;
}
Topic archived. No new replies allowed.