Problems in Show the player(s) who got the min and max average

Hello. This is a sequel to the question that I posted recently. I'm working on the system and I have trouble showing the player(s) who got the max average and minimum average that I input based on the inputs that I've done in 1. Add record and 3. Compute for the average. I'm stuck here for hours analyzing the codes to find a way to solve this problem. I need help in fixing the code below. Any help will do. Thanks.

In 6. Open the file and 7. Close the file, I'll find a way first in solving this and I'll call for help if I'm stuck in this project.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

const int maxrow = 5;
string gb_nickname [maxrow] = {};
double gb_age [maxrow] = {};
double gb_score1 [maxrow] = {};
double gb_score2 [maxrow] = {};

struct variables
{
    string nickname;
    double age;
    double score1;
    double score2;
};

void addrecord()
{   
    variables v;
    system ("clear");
        
        cin.ignore ();
        cout << "Enter nickname: ";
        getline (cin, v.nickname);
        
        
         
        cout << "Age: ";
        cin >> v.age;
       
         
         
        cout << "Enter Score 1: ";
        cin >> v.score1;
        
        cout << "Enter Score 2: ";
        cin >> v.score2;
        
        
      for (int x = 0; x < maxrow; x++)
        if (gb_nickname[x].empty()) 
        {
            gb_nickname[x] = v.nickname;
            gb_age[x] = v.age;
            gb_score1[x] = v.score1;
            gb_score2[x] = v.score2;

            break;
        }
        
    
    ofstream myfile ("players.txt", ios::out | ios::app);
    
    myfile << endl;
    myfile << " Nickname: " << v.nickname << endl << " Age: " << v.age  << endl<<  " Score 1: " << v.score1 << endl <<  " Score 2: " << v.score2 << endl;
    
    
    myfile.close ();
    
    cout << endl;
    cout << "File was saved successfully! Press any key to continue... " << endl;
    cin.get();
}

void viewrecord ()
{
    char menu; //(y/n)
    
 int counter = 0;
 
 while (menu != 'M')/* user can input "y" or "Y"*/ 
 
 {   system ("clear"); //clear the screen
    cout << "\n";
  
   
    cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\b        RECORDS LIST" << endl;
    

   
     

 
 for (int x = 0; x <maxrow; x++)
    {
       if (gb_nickname[x] != "\0")

       {

        counter++;
        
        cout << endl << counter <<"."  << " Nickname: " << gb_nickname [x] << "   " << endl << " Age: " <<  gb_age [x] << "   "<< endl << " Score 1: " <<  gb_score1 [x] << "   "<< endl << " Score 2: "<<  gb_score2 [x] << endl;
        
    
       }

    }
cout << "\v\v\v\t\t\t\t\t\t\b\b\b\b\b\b\b\b\b\bPress 'M' to go to Main Menu: ";
cin >> menu; 
   
   
   if (counter == 0) 
{
    cout << "        No records found." << endl;

}
}
}

void average(string search)
{   char menu;
    
    int counter = 0;
     while (menu != 'M') {
         
     
    for (int x = 0; x < maxrow; x++) 
    {
        if (gb_nickname[x] == search)
        {
            counter++;
            
             cout << endl << counter <<"."  << " Nickname: " << gb_nickname [x] << "   " << endl << " Age: " <<  gb_age [x] << "   "<< endl << " Score 1: " <<  gb_score1 [x] << "   "<< endl << " Score 2: "<<  gb_score2 [x] << endl;
             double sum = gb_score1 [x] + gb_score2 [x];
        cout << "The average of " << gb_nickname [x] << " is " <<sum/2 <<".";
        
         ofstream myfile ("players.txt", ios :: app);
    myfile << "\nAverage: " << sum/2 << endl;
    myfile.close();
        }
    }
    
    if (counter == 0)
    {
        cout << "No records found." << endl;
    }
    
   cout << "\v\v\v\t\t\t\t\t\t\b\b\b\b\b\b\b\b\b\bPress 'M' to go to Main Menu: ";
cin >> menu; 
    
    
    
}
}


int main()
{
     int num;
     string gb_nickname;
     
     home:

   do {
    system ("clear");
    
    cout << "                        ==THE ARCADE'S BEST=="                       << endl;
    
    cout << "1. Add record" << endl;
    cout << "2. View players records" << endl;
    cout << "3. Compute for the average" << endl;
    cout << "4. Show the player(s) who got the max average" << endl;
    cout << "5. Show the player(s) who gets the min average" << endl;
    cout << "6. Open the file" << endl;
    cout << "7. Close the file" << endl;
    cout << "8. Exit" << endl;
   
    cout << "\nPick a number: ";
    cin >> num;
    
    
    
    
    switch (num)
    
    {
        case 1:
        addrecord();
        break;
        
        case 2:
        viewrecord();
        break;
        
        case 3:
        system ("clear");
        cin.ignore();
        cout << "Search Nickname: ";
        getline (cin, gb_nickname);
        average(gb_nickname);
        break;
        
        
    
    
    
    
    
}

} while (num != 8);
    return 0;
} 

Last edited on
As a possible refactor, consider:

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

constexpr size_t maxrow {5};

struct variables {
    std::string nickname;
    double age {};
    double score1 {};
    double score2 {};
    double avg {};
};

// Global variables are a BAD IDEA
variables gb_var[maxrow] {};
double max_avg {std::numeric_limits<double>::min()}, min_avg {std::numeric_limits<double>::max()};

std::istream& operator>>(std::istream& is, variables& v) {
    return std::getline(is >> std::ws, v.nickname, ',') >> v.age >> v.score1 >> v.score2;
}

bool addrecord(variables& v) {
    size_t pos {};
    bool found {};

    for (size_t x {}; x < maxrow && !found; ++x) {
        found = gb_var[x].nickname == v.nickname;

        if (!found && pos == 0 && gb_var[x].nickname.empty())
            pos = x + 1;
    }

    if (found) {
        std::cout << "Name already present\n";
        return false;
    }

    if (pos == 0) {
        std::cout << "Array full\n";
        return false;
    }

    v.avg = (v.score1 + v.score2) / 2.0;

    if (v.avg > max_avg)
        max_avg = v.avg;

    if (v.avg < min_avg)
        min_avg = v.avg;

    gb_var[pos - 1] = v;
    return true;
}

bool getrecord() {
    variables v;

    std::cout << "Enter nickname: ";
    std::getline(std::cin, v.nickname);

    std::cout << "Age: ";
    std::cin >> v.age;

    std::cout << "Enter Score 1: ";
    std::cin >> v.score1;

    std::cout << "Enter Score 2: ";
    std::cin >> v.score2;

    return addrecord(v);
}

void viewrecord() {
    size_t counter {};

    std::cout << "\n\t\t\t\t\t\t\b\b\b\b\b\b\b\b        RECORDS LIST\n";

    for (size_t x {}; x < maxrow; ++x)
        if (!gb_var[x].nickname.empty())
            std::cout << '\n' << ++counter << "." << " Nickname: " << gb_var[x].nickname << '\n' << " Age: " << gb_var[x].age << '\n'
                << " Score 1: " << gb_var[x].score1 << '\n' << " Score 2: " << gb_var[x].score2 << '\n'
                << " Average: " << gb_var[x].avg << '\n';

    if (counter == 0)
        std::cout << "        No records found.\n";
}

void average(const std::string& search) {
    bool found {};

    for (size_t x {}; x < maxrow; ++x)
        if (gb_var[x].nickname == search) {
            std::cout << "The average for " << gb_var[x].nickname << " is " << gb_var[x].avg << '\n';
            found = true;
            break;
        }

    if (!found)
        std::cout << "No records found.\n";
}

int main() {
    std::ifstream ifs("players.txt");

    if (!ifs)
        std::cout << "Cannot open file\n";
    else {
        for (variables v; ifs >> v; addrecord(v));

        std::cout << "Records loaded\n";
        ifs.close();
    }

    unsigned num {};

    do {
        std::cout << "\n                        ==THE ARCADE'S BEST==\n";
        std::cout << "1. Add record\n";
        std::cout << "2. View players records\n";
        std::cout << "3. Display a player average\n";
        std::cout << "4. Show the player(s) who got the max average\n";
        std::cout << "5. Show the player(s) who gets the min average\n";
        std::cout << "6. Exit\n";

        std::cout << "\nPick a number: ";
        std::cin >> num;
        std::cin.ignore();

        switch (num) {
            case 1:
                if (getrecord())
                    std::cout << "Record added OK\n";

                break;

            case 2:
                viewrecord();
                break;

            case 3:
                {
                    std::string gb_nickname;

                    std::cout << "Search Nickname: ";
                    std::getline(std::cin, gb_nickname);
                    average(gb_nickname);
                }
                break;

            case 4:
                std::cout << "Players with maximum average\n";

                for (size_t x {}; x < maxrow; ++x)
                    if (gb_var[x].avg == max_avg)
                        std::cout << gb_var[x].nickname << '\n';
                break;

            case 5:
                std::cout << "Players with minimum average\n";

                for (size_t x {}; x < maxrow; ++x)
                    if (gb_var[x].avg == min_avg)
                        std::cout << gb_var[x].nickname << '\n';
                break;


            case 6:
                {
                    std::ofstream ofs("Players.txt");

                    if (!ofs)
                        std::cout << "Cannot open file\n";
                    else {
                        for (size_t x {}; x < maxrow; ++x)
                            if (!gb_var[x].nickname.empty())
                                ofs << gb_var[x].nickname << ',' << gb_var[x].age << ' ' << gb_var[x].score1 << ' ' << gb_var[x].score2 << '\n';

                        std::cout << "Data written ok\n";
                    }
                }
                break;

            default:
                std::cout << "Invalid option\n";
                break;
        }

    } while (num != 6);
}

Thank you so much @seeplus! The program you fixed for me is working. The codes are so advanced and the codes that you've done to it are not available in our modules. That's why I'm out of logic. I'll study the codes and I'll also find a way to do 6. Open the file and 7. Close the file. I'll go to the forum next time when I still got no more logic left to do. I'm so impressed.
Last edited on
With the way I've re-factored it, you don't use option 6 (file open) and 7 (file close). The file is opened, read and closed before the menu is displayed. Hence option 2 when first used will show the details read from the file The file is re-written when the menu is exited with option 6 (exit).
Topic archived. No new replies allowed.