-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFootball Players.cpp
254 lines (218 loc) · 5.86 KB
/
Football Players.cpp
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 <conio.h>
#include <fstream>
using namespace std;
struct football
{
string name;
string position;
int touchdowns;
int catches;
int passingYard;
int receivingYard;
int rushingYard;
};
int count = 0;
const int size = 10;
void head();
char menu();
football addPlayer();
void show(football player);
void viewPlayer(football player[size]);
void search(football player[size]);
int finding(football player[size], string search);
void loadData(football player[size]);
string parseData(string line, int field);
void storeData(football player);
main()
{
football player[size];
loadData(player);
while (true)
{
system("cls");
char Option;
Option = menu();
if (Option == '1')
{
viewPlayer(player);
}
else if (Option == '2')
{
player[count] = addPlayer();
char yn;
cout << " DO YOU WANT SAVE DATA ! (Y/N) : ";
cin >> yn;
if ( yn == 'y' || yn == 'Y')
{
storeData( player[count]);
count++;
}
}
else if (Option == '3')
{
search(player);
}
else if (Option == '4')
{
break;
}
}
}
void head()
{
cout << "****************************************" << endl;
cout << "* Football Players Information *" << endl;
cout << "****************************************" << endl;
cout << "\n";
}
char menu()
{
char Option;
head();
cout << " Menu >> " << endl;
cout << "________________________________________" << endl;
cout << "\n";
cout << " 1. View All Players " << endl;
cout << " 2. Add Player " << endl;
cout << " 3. Search " << endl;
cout << " 4. Exit" << endl;
cout << "\n Select Your Option.....";
Option = getche();
return Option;
}
football addPlayer()
{
football player;
system("cls");
head();
cout << " Add Player >> " << endl;
cout << "________________________________________" << endl;
cout << "\n";
cout << " PLayer Name : ";
getline(cin, player.name);
cout << " Position : ";
cin >> player.position;
cout << " Number of Touchdowns : ";
cin >> player.touchdowns;
cout << " Number of Catches : ";
cin >> player.catches;
cout << " Number of Passing Yards : ";
cin >> player.passingYard;
cout << " Number of Receving Yards : ";
cin >> player.receivingYard;
cout << " Number of Rushing Yards : ";
cin >> player.rushingYard;
cout << "\n New Player Added Succesfully :)" << endl;
cout << "\n Press any Key to continue.....";
getch();
return player;
}
void viewPlayer(football player[size])
{
system("cls");
head();
cout << " All Players >> " << endl;
cout << "________________________________________" << endl;
cout << "\n";
cout << " Name\t\tPosition\tTouchdowns\tCatches\tPassing Yard\tReceiving Yard\tRushing Yard" << endl;
cout << "\n";
for (int idx = 0; idx < count; idx++)
{
show(player[idx]);
}
cout << "\n Press any Key to continue.....";
getch();
}
void show(football player)
{
cout << " " << player.name << "\t\t" << player.position << "\t\t" << player.touchdowns << "\t\t" << player.catches << "\t" << player.passingYard << "\t\t" << player.receivingYard << "\t\t" << player.rushingYard << endl;
}
void search(football player[size])
{
string search;
system("cls");
head();
cout << " Search Player >> " << endl;
cout << "________________________________________" << endl;
cout << "\n";
cout << " Enter Player Name : ";
// cin.ignore();
getline(cin, search);
int idx = -1;
idx = finding(player, search);
getch();
if (idx > count || idx < 0)
{
cout << " Nothing Find :( " << endl;
}
else
{
cout << " Name\t\tPosition\tTouchdowns\tCatches\tPassing Yard\tReceiving Yard\tRushing Yard" << endl;
cout << "\n";
show(player[idx]);
}
cout << "\n Press any Key to continue.....";
getch();
}
int finding(football player[size], string search)
{
for (int idx = 0; idx < count; idx++)
{
if (search == player[idx].name)
{
return idx;
}
}
return -1;
}
void loadData(football player[size])
{
fstream file;
string line;
file.open("PlayersData.txt", ios::in);
while (!file.eof())
{
getline(file, line);
player[count].name = parseData(line, 1);
player[count].position = parseData(line, 2);
player[count].touchdowns = stoi(parseData(line, 3));
player[count].catches = stoi(parseData(line, 4));
player[count].passingYard = stoi(parseData(line, 5));
player[count].receivingYard = stoi(parseData(line, 6));
player[count].rushingYard = stoi(parseData(line, 7));
count++;
}
file.close();
}
string parseData(string line, int field)
{
string item;
int comma = 1;
for (int i = 0; i < line.length(); i++)
{
if (line[i] == ',')
{
comma++;
}
else if (comma == field)
{
item = item + line[i];
}
}
return item;
}
void storeData(football player)
{
fstream file;
file.open("PlayersData.txt", ios::app);
file << "\n";
file << player.name << ",";
file << player.position << ",";
file << player.touchdowns << ",";
file << player.catches << ",";
file << player.passingYard << ",";
file << player.receivingYard << ",";
file << player.rushingYard << ",";
file.close();
}