Any suggestions ??

>So i'm working on this task,i have two codes(i prefer c++).The last function is missing in the first code.

>Create a player filming program with features for:
a) Creating a file (by adding a certain number of entries and rewriting the file data to an array) with data for players in the world championship, with fields: name, surname, country team, player number, goals scored. Keyboard input, checking for correct data by: number, goals scored. Display the current content of the array (file) on the screen.

b) Reporting for:
- all players by a race number assigned by the keyboard;
- the players from a given team (with a request for continuation);

c) Updating the data in the file by changing the field - marked goals for a player by given team and family.
Main function main () - with menu for selecting functions and checking the status of the data. Using global variables or parameter transmission functions - optional.

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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
 #include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const int N = 5;

struct player 
{
  
string ime;
  
string familia;
  
string otbor;
  
int nomer;
   
int vk_golove;
 


} igrachi[N], podredba;

void add_player (player a[]);

void search_by_number (player a[], int &nomer_na_igracha);

void search_by_team (player a[], string & ime_na_otbora);

void klasirane (player a[]);

void aktualizcia (player a[], int &nomer_na_igracha, string & ime_na_otbora);

fstream igrachi_file;

 
void
add_player (player a[]) 
{
  
int br;
  
igrachi_file.open ("igrachi.txt", ios::app);
  
  do
    {
      
cout << "\n Broi igrachi:";
      
cin >> br;
    
}
  while (br <= 0 || br > N);
  
if (igrachi_file.fail ())
    
    {
      
cout << "Error. The file is missing.";
      
exit (1);
    
}
  
for (int i = 0; i < br; i++)
    
    {
      
cout << "Vuvedi ime na igrach:";
      
cin >> a[i].ime;
      
igrachi_file << a[i].ime << endl;
      
cout << "Vuvedi familia na igrach:";
      
cin >> a[i].familia;
      
igrachi_file << a[i].familia << endl;
      
cout << "Vuvedi otbor na igracha:";
      
cin >> a[i].otbor;
      
igrachi_file << a[i].otbor << endl;
      
cout << "Vuvedi nomer na igracha:";
      
cin >> a[i].nomer;
      
if (a[i].nomer > 99 || a[i].nomer < 1)
	
	{
	  
	  do
	    {
	      
cout << "Vuvedete nomer ot 1 do 99. \n";
	      
cin >> a[i].nomer;
	    
}
	  while (a[i].nomer > 99 || a[i].nomer < 1);
	
}
      
igrachi_file << a[i].nomer << endl;
      
cout << "Vuvedi vkarani golove na igracha:";
      
cin >> a[i].vk_golove;
      
if (a[i].vk_golove < 0)
	
	{
	  
	  do
	    {
	      
cout << "Vuvedete golove >= 0 \n";
	      
cin >> a[i].vk_golove;
	    
}
	  while (a[i].vk_golove < 0);
	
}
      
igrachi_file << a[i].vk_golove << endl;
    
 
}
  
igrachi_file.close ();

}


void
search_by_number (player a[], int &nomer_na_igracha) 
{
  
int flag = 0;
  
igrachi_file.open ("igrachi.txt", ios::in);
  
igrachi_file.seekg (0);
  
if (igrachi_file.fail ())
    
    {
      
cout << "Fail to open the file";
      
exit (1);
    
}
  
for (int i = 0; i < N; i++)
    
    {
      
igrachi_file >> a[i].ime;
      
igrachi_file >> a[i].familia;
      
igrachi_file >> a[i].otbor;
      
igrachi_file >> a[i].nomer;
      
igrachi_file >> a[i].vk_golove;
      
 
if (nomer_na_igracha == a[i].nomer)
	
	{
	  
cout << "Igrach: " << a[i].ime << " " << a[i].familia 
	    <<"\nOtbor: " << a[i].otbor << "\nNomer: " << a[i].
	    nomer << "\nVkarani golove: " << a[i].vk_golove << endl;
	  
flag++;
	
}
    
}
  
if (!flag)
    
cout << endl << "Nqma takuv igrach!" << endl;
  
igrachi_file.close ();

}


void
search_by_team (player a[], string & ime_na_otbora) 
{
  
int flag = 0;
  
igrachi_file.open ("igrachi.txt", ios::in);
  
igrachi_file.seekg (0);
  
if (igrachi_file.fail ())
    
    {
      
cout << "Fail to open the file";
      
exit (1);
    
}
  
for (int i = 0; i < N; i++)
    
    {
      
igrachi_file >> a[i].ime;
      
igrachi_file >> a[i].familia;
      
igrachi_file >> a[i].otbor;
      
igrachi_file >> a[i].nomer;
      
igrachi_file >> a[i].vk_golove;
      
 
if (ime_na_otbora == a[i].otbor)
	
	{
	  
cout << "Igrach: " << a[i].ime << " " << a[i].familia 
	    <<"\nOtbor: " << a[i].otbor << "\nNomer: " << a[i].
	    nomer << "\nVkarani golove: " << a[i].vk_golove << endl << endl;
	  
flag++;
	
}
    
}
  
if (!flag)
    
cout << endl << "Nqma takuv otbor!" << endl;
  
igrachi_file.close ();

}


void
klasirane (player a[]) 
{
  
igrachi_file.open ("igrachi.txt", ios::in);
  
igrachi_file.seekg (0);
  
for (int i = 0; i < N; i++)
    
    {
      igrachi_file >> a[i].ime;
      
igrachi_file >> a[i].familia;
      
igrachi_file >> a[i].otbor;
      
igrachi_file >> a[i].nomer;
      
igrachi_file >> a[i].vk_golove;
    
 
} 
cout << "ime" << setw (15) << "familia" << setw (15) << "otbor" <<
    setw (15) << "nomer" << setw (15) << "vkarani golove" << endl;
  
for (int i = 0; i < N; i++)
    
    {
      
 
cout << a[i].ime << right << setw (15) << a[i].
	familia << right << setw (15) << a[i].
	otbor << right << setw (15) << a[i].
	nomer << right << setw (15) << a[i].vk_golove << endl;
    
} 
igrachi_file.close ();

} 
 
void

main () 
{
  
 
int choice;
  int nomer_na_igracha;
  
string ime_na_otbora, familia_na_igracha;
  
 
player a[N];
  
  do
    
    {
      
cout <<
	"================================================================================"
	<< endl;
      
cout << "\t\tMenu\n\n";
      
cout << "= Izberete:\n";
      
cout << "= 1.Dobavqne na igrachi\n";
      
cout << "= 2.Spisuk s igrachite\n";
      
cout << "= 3.Spravka za igrach po nomer\n";
      
cout << "= 4.Spravka za igrach po otbor\n";
      
cout << "= 5.Aktualizirane\n";
      
cout << "= 6.Krai\n";
      
cout <<
	"\n================================================================================"
	<< endl;
      
cin >> choice;
      
switch (choice)
	
	{
	
case 1:
	  {
	    add_player (a);
	    
break;
	  }
	
case 2:
	  {
	    klasirane (a);
	    break;
	  }
	
case 3:
	  {
	    cout << "Tursene po nomer: ";
	    
cin >> nomer_na_igracha;
	    
search_by_number (a, nomer_na_igracha);
	    break;
	  }
	
case 4:
	  {
	    cout << "Tursene po otbor: ";
	    
cin >> ime_na_otbora;
	    
search_by_team (a, ime_na_otbora);
	    break;
	  }
	
case 5:;
	
}
    
 
 
}
  while (choice != 6);

}
Last edited on
First part of second code:
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
#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

const char* FOOTBALL_DB =  "FOOTBALL_DB.BIN";
const int NAME_SIZE = 50;
const int MIN_NAME_SIZE = 4;
const int TEAM_SIZE = 20;
const int MIN_TEAM_SIZE = 2;
const int BSIZE = 100;
const int MIN_PLAYER_NUMBER = 0;
struct FPlayer{

           char name[NAME_SIZE];//The full name of the player
           char team[TEAM_SIZE];//The team
           int number;
           int scores;//They could be negative

};



void pause()

{
         cin.get();
         cin.ignore();
}

void clrscr()

{
         system("CLS");
}

bool check_number(int& num, char* team)

{

         FPlayer fp;
         fstream file(FOOTBALL_DB, ios::binary | ios::in);
         if(!file)
         {

                               cerr << "Error" << endl;
                                pause();
         }

         while(file.read(reinterpret_cast<char*>(&
fp), sizeof(FPlayer)))

         {

                                                                                                   if(num == fp.number && strcmp(fp.team, team) == 0)
                                                                                                   {
                                                                                                                  cout << "ERROR -- NUMBER ALREADY EXISTSa" << endl;
                                                                                                                  return false;
                                                                                                   }
         } 
         file.close();
         return true;
}

bool check_str(char* str, int MIN, int MAX, bool digit_check = false)//True -- check for digits in the str

{

         bool flag = true;
         int name_len = strlen(str);

         if(MIN > name_len || name_len > MAX)

         {

                                 cout << "ERROR -- INVALID NAME LENGHT" << endl;
                                 pause();
                                 flag = false;

         }

         if(digit_check)

         {

                                 for(int i = 0; i < name_len; i++){ if(isdigit(str[i])) {

                                                                                                                                                        cout << "Name can't content digits!" << endl;

                                                                                                                                                        flag = false;}

         }   }
         return flag;
}

void show(const FPlayer& fp)

{
                cout << "Name: " << fp.name << endl;
                cout << "Team: " << fp.team << endl;
                cout << "Numer : " << fp.number << endl;
                cout << "Scores : " << fp.scores << endl;
}

int get_number(char* team="")

{

                int number = 0; 

                do{

                                cout << "Enter the number of the player" << endl;
                                cin >> number;

                }while(number <= MIN_PLAYER_NUMBER || !check_number(number, team));

                return number;
}

FPlayer get_info()

{

                char buffer[BSIZE];
                FPlayer fp;
                bool flag;

                do{

                   flag = false;  
                   cout << "Enter the name:" << endl;
                   cin.ignore();
                   cin.getline(buffer, BSIZE);
                   if(!check_str(buffer, MIN_NAME_SIZE, NAME_SIZE, true))flag = true;
                   else strcpy(fp.name, buffer);
                   cout << "Enter the team:" << endl;
                   cin.getline(buffer, BSIZE);
                   if(!check_str(buffer, MIN_TEAM_SIZE, TEAM_SIZE)) flag = true;
                   else strcpy(fp.team, buffer);
                   cout << "Enter the scores:" << endl;
                   cin >> fp.scores;//It's possible the player to have -30 scores
                   fp.number = get_number(fp.team);
                }while(flag);     
                return fp;
}

void init_file()

{

                ifstream file(FOOTBALL_DB, ios::binary);

                if(!file)

                {

                                 cerr << "ERROR -- FILE DOESN'T EXIST. CREATING. . ." << endl;
                                 pause();
                                 file.close();
                                 ofstream f(FOOTBALL_DB, ios::binary);

                                 if(!f)

                                 {

                                           cerr << "ERROR -- DURING CREATING THE FILEa" << endl;
                                           pause();
                                           exit(0);

                                 }

                }

}

void write_file(FPlayer& fp)

{

                fstream file(FOOTBALL_DB, ios::binary | ios::out | ios::app);

                if(!file)

                {

                                 cerr << "ERROR -- CAN'T OPEN THE FILE" << endl
                                 pause();

                }

                file.write(reinterpret_cast<char*>(&fp) , sizeof(FPlayer));
                file.close();
}

void read_file()

{

                FPlayer fp;
                fstream file(FOOTBALL_DB, ios::binary | ios::in);
                bool flag = false;

                if(!file)

                {

                                 cerr << "Error" << endl;

                                 pause();

                }

                while(file.read(reinterpret_cast<char*>(&
fp), sizeof(FPlayer))){ show(fp);

                                                                                                                                                 flag = true;}

                if(!flag) cout << "tNo players avaiable" << endl;
                pause();
                clrscr();
                file.close();
}
Last edited on
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
void read_by_number(int number)//Idea -- switch with who'll be cased the number: 

{//like 1 by number, 2 by name & etc.

                fstream file(FOOTBALL_DB, ios::binary | ios::in);
                FPlayer fp;//Temp object -- for reading from the file
                unsigned int nCtr = 0;
                bool flag = false;
                if(!file)

                {

                                 cerr << "Error" << endl;
                                 pause();

                }

                while(file.read(reinterpret_cast<char*>(&
fp), sizeof(FPlayer)))

                {

                                                                                                          if(fp.number == number)

                                                                                                          {

                                                                                                                                   cout << "Found: " << (++nCtr) << endl;
                                                                                                                                   show(fp);
                                                                                                                                   flag = true;

                                                                                                          }

                                                                                                          

                }

                if(!flag) cout << "tNo players with that number" << endl;

                pause();
                clrscr();
                file.close();

}

void read_by_team()//Idea -- switch with who'll be cased the number: 

{//like 1 by number, 2 by name & etc.

                fstream file(FOOTBALL_DB, ios::binary | ios::in);
             
                FPlayer fp;//Temp object -- for reading from the file
                unsigned int nCtr = 0;
                char buffer[BSIZE];//It's not good idea to use buffer for every single function, but it's not too good to use global buffer too 
                char team[TEAM_SIZE];
                bool flag, flag2 = false;

                if(!file)

                {

                                 cerr << "Error" << endl;
                                 pause();

                }

                

                do

                {

                                flag = false;  
                                cout << "Enter the team:" << endl;
                                cin.ignore();
                                cin.getline(buffer, BSIZE);

                                

                                if(!check_str(buffer, MIN_TEAM_SIZE, TEAM_SIZE)) flag = true;

                                else strcpy(team, buffer);

                }while(flag);

                while(file.read(reinterpret_cast<char*>(&
fp), sizeof(FPlayer)))

                {

                                                                                                          if(strcmp(fp.team, team) == 0)

                                                                                                          {

                                                                                                                                   cout << "Found: " << (++nCtr) << endl;

                                                                                                                                   show(fp);

                                                                                                                                   

                                                                                                                                   flag2 = true;
                                                                           }

                                                                                                          

                }

                if(!flag2) cout << "tNo players from this team" << endl;

                pause();

                clrscr();

                file.close();

}

void edit_scores()

{

                fstream file(FOOTBALL_DB, ios::binary | ios::in | ios::out);

                FPlayer fp;//Temp object -- for reading from the file 

                unsigned int nCtr = 0;

                char team[TEAM_SIZE], name[NAME_SIZE], buffer[BSIZE];

                int scores = 0;

                bool flag, flag2 = false;

                

                if(!file)

                {

                                 cerr << "ERROR -- CAN'T OPEN THE FILEa" << endl;
                                 pause();
                                 return;

                }

                

                do{

                   flag = false;  

                   cout << "Enter the name:" << endl;

                   cin.ignore();

                   cin.getline(buffer, BSIZE);

                   

                   if(!check_str(buffer, MIN_NAME_SIZE, NAME_SIZE, true))flag = true;

                   else strcpy(name, buffer);

                   cout << "Enter the team:" << endl;

                   cin.getline(buffer, BSIZE);

                  

                   if(!check_str(buffer, MIN_TEAM_SIZE, TEAM_SIZE)) flag = true;

                   else strcpy(team, buffer);

                   cout << "Enter the new scores:" << endl;

                   cin >> scores;//It's possible the player to have -30 scores

                }while(flag);     

                

                while(file.read(reinterpret_cast<char*>(&
fp), sizeof(FPlayer)))

                {                                                                              
 if(strcmp(fp.team, team) == 0 && strcmp(fp.name, name) == 0)//Separating name to first name and last name is unusless -- 
                                                                                                          {//there could be 2 players with same last names
                                                                                                                                   cout << "Found!" << endl;
                                                                                                                                   show(fp);
                                                                                                                                   file.seekp(sizeof(FPlayer) * nCtr);
                                                                                                                                   fp.scores  = scores;
                                                                                                                                   file.write(reinterpret_cast<char*>(&fp)
, sizeof(FPlayer));
                                                                                                                               
                                                                                                                                   flag2 = true;
                                                                                                          } ++nCtr;

                }
                
                if(!flag2) cout << "tPlayer with that name and team don't exist" << endl;
                pause();
                clrscr();
                file.close();

}

int menu()//It don't have any check for same players, because there do not exist any unique element!
{
                int ch = 0; //The choise of the user
                cout << "1. Add new player"<< endl;
                cout << "2. Show all players"<< endl;
                cout << "3. Show by: number"<< endl;
                cout << "4. Show by: team"<< endl;
                cout << "5. Edit scores"<< endl;
                cout << "6. Exit"<< endl;
                cin >> ch;
                return ch;

}
And part three of second code:
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
int main()
{
         FPlayer fp;
         init_file();
         for(;;)
         {
                        switch(menu())
                        {
                                         case 1: fp = get_info();
                                                         write_file(fp);
                                                         break;
                                         case 2: read_file();
                                                         break;
                                         case 3: read_by_number(get_number());
                                                         break;
                                         case 4: read_by_team();
                                                         break;
                                         case 5: edit_scores();
                                                        break;
                                         case 6: cout << "Have a nice day!" << endl;
                                                         pause();
                                                         exit(0);
                        }
         }      
         return 0;

}
Hello goshko2,

After looking over all the code I would stay with the first code as it is more to the point and after removing some blank lines much shorter to work with.

Andy
Topic archived. No new replies allowed.