Mar 15, 2022 at 7:30am UTC
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 Mar 15, 2022 at 7:32am UTC
Mar 15, 2022 at 1:08pm UTC
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 Mar 15, 2022 at 1:09pm UTC
Mar 15, 2022 at 1:36pm UTC
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).