View Record Problem

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
L40 doesn't do what you think. It has an extra terminating ; !! Also use .empty() to test if a string doesn't have any content.

Try:

 
if (gb_nickname[x].empty())


and for L83
 
if (!gb_nickname[x].empty())

@seeplus Good day. I tried replacing the codes that you suggested to me but it seems that it still doesn't work either.
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
Thanks. I appreciate that. It was a huge help! I'm just a little confused in analyzing my codes.
Perhaps 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
#include <iostream>
#include <fstream>
#include <string>

constexpr size_t maxrow {5};

std::string gb_nickname[maxrow] {};
unsigned gb_age[maxrow] {};
int gb_score1[maxrow] {};
int gb_score2[maxrow] {};

void addrecord() {
    std::string nickname;
    unsigned age {};
    int score1 {};
    int score2 {};

    //system("clear");

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

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

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

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

    for (size_t 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;
        }

    std::ofstream myfile("players.txt", std::ios::app);

    if (!myfile)
        std::cout << "Cannot open file\n";
    else {
        myfile << " Nickname: " << nickname << '\n' << " Age: " << age << '\n' << " Score 1: " << score1 << '\n' << " Score 2 " << score2 << '\n';
        std::cout << "\nFile was saved successfully!\n";
    }

    std::cout << "Press any key followed by <CR> to continue... ";
    std::cin.ignore();
    std::cin.get();
}

void viewrecord() {
    //char menu {};

    size_t counter {};

    //system("clear"); //clear the screen
    std::cout << "\n\t\t\t\t\t\t\b\b\b\b\b\b\b\b        RECORDS LIST\n";

    for (size_t x = 0; x < maxrow; ++x)
        if (!gb_nickname[x].empty()) {
            ++counter;
            std::cout << counter << "." << " Nickname: " << gb_nickname[x] << '\n' << " Age: " << gb_age[x] << '\n' << " Score 1: " << gb_score1[x] << '\n' << " Score 2: " << gb_score2[x] << '\n';
        }

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

    //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() {
    unsigned num {};

    do {
        //system("clear");

        std::cout << "                       MAIN MENU\n";
        std::cout << "1. Add record\n";
        std::cout << "2. View players records\n";
        std::cout << "3. Exit\n";
        std::cout << "\nPick a number: ";
        std::cin >> num;

        switch (num) {
            case 1:
                addrecord();
                break;

            case 2:
                viewrecord();
                break;

            case 3:
                break;

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

    } while (num != 3);
}


Note that although the players.txt file accumulates the entered data, the program doesn't first read the file to obtain already entered data. If you want option 2) to display all entered data, then you'll need a function to read the file and update the arrays. If you want to do this, then the format of the file isn't the best for reading and parsing.
Topic archived. No new replies allowed.