Mar 14, 2022 at 11:28am Mar 14, 2022 at 11:28am UTC
Hello. I have a problem with the code below. In the view records menu, after I input something in add records, instead of storing all the inputs that I have done in add records, it overwrites the old input that I have done to it. Can someone help me fix the code and explain what is the problem with it? Thanks.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int maxrow = 5;
string gb_nickname [maxrow] = {};
int gb_age [maxrow] = {};
int gb_score1 [maxrow] = {};
int gb_score2 [maxrow] = {};
void addrecord()
{ string nickname;
int age;
int score1;
int score2;
system ("clear" );
cin.ignore ();
cout << "Enter nickname: " ;
getline (cin, nickname);
cout << "Age: " ;
cin >> age;
cout << "Enter Score 1: " ;
cin >> score1;
cout << "Enter Score 2: " ;
cin >> score2;
for (int x = 0; x < maxrow; x++)
{
if (gb_nickname[x] == "\0" );
gb_nickname[x] = nickname;
gb_age[x] = age;
gb_score1[x] = score1;
gb_score2[x] = score2;
break ;
}
fstream myfile ("players.txt" , ios::out | ios::app);
myfile << endl;
myfile << " Nickname: " << nickname << endl << " Age: " << age << endl<< " Score 1: " << score1 << endl << " Score 2 " << 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)
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;
int counter = 0;
for (int x = 0; x <maxrow; x++)
{
if (gb_nickname[x] != "\0" )
{
counter++;
cout << counter <<"." << " Nickname: " << gb_nickname [x] << " " << endl << " Age: " << gb_age [x] << " " << endl << " Score 1: " << gb_score1 [x] << " " << endl << " Score 2: " << gb_score2 [x] << endl;
}
}
if (counter == 0) //if counter is = 0 (or if no input in ADD ITEM) it will print no record found
{
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;
home:
do {
system ("clear" );
cout << " MAIN MENU" << endl;
cout << "1. Add record" << endl;
cout << "2. View players records" << endl;
cout << "3. Exit" << endl;
cout << "\nPick a number: " ;
cin >> num;
switch (num)
{
case 1:
addrecord();
break ;
case 2:
viewrecord();
break ;
break ;
}
} while (num != 3);
return 0;
}
Last edited on Mar 14, 2022 at 11:40am Mar 14, 2022 at 11:40am UTC
Mar 14, 2022 at 1:00pm Mar 14, 2022 at 1:00pm UTC
@seeplus Good day. I tried replacing the codes that you suggested to me but it seems that it still doesn't work either.
Mar 14, 2022 at 1:18pm Mar 14, 2022 at 1:18pm UTC
You have put L41-44 into a compound statement (using {} ) haven't you? If you just remove the ; from L40 then only L41 is part of the if statement.
1 2 3 4 5 6 7 8 9
for (int x = 0; x < maxrow; x++)
if (gb_nickname[x].empty()) {
gb_nickname[x] = nickname;
gb_age[x] = age;
gb_score1[x] = score1;
gb_score2[x] = score2;
break ;
}
Last edited on Mar 14, 2022 at 1:19pm Mar 14, 2022 at 1:19pm UTC
Mar 14, 2022 at 1:35pm Mar 14, 2022 at 1:35pm UTC
Thanks. I appreciate that. It was a huge help! I'm just a little confused in analyzing my codes.