Structure

Hi Guys can you help me with this problem ? I can't quite get it :(
I'm new in C++ :(
Write a C++ program to keep records and compute for the scores of 5 players. The information of each player contains: Nickname, Age and two best played scores.

The program will prompt the user to choose the operation of records from a menu as shown below:

==============================================
MENU
==============================================

1. Add record
2. View players records
3. Compute for the average
4. Show the player(s) who gets the max average.
5. Show the player(s) who gets the min average.
6. Exit

Hi,

can you show us what you did/tried?
#include<iostream>
using namespace std;

struct Plrrecord {
char nickname[100];
char age[100];
char average[100];
char score1[100];
char score2[100];
}plr;
void addrecord(Plrrecord);
void viewrecord(Plrrecord);
void average(Plrrecord);
void maxave(Plrrecord);
void minave(Plrrecord);

int main() {
char option;
cout << "=================================================\n";
cout << "\t\t\tMENU\n";
cout << "1. Add record\n";
cout << "2. View players records\n"
<< "3. Compute for the average\n"
<< "4. Show the player(s) who gets the max average.\n"
<< "5. Show the player(s) who gets the min average.\n"
<< "6. Exit\n";

switch (option)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
}
system("pause>0");
return 0;
}

void addrecord(Plrrecord)
{
for( int i=0;i<5;i++)
{
cout<<"Enter your nickname:";
cin>>nickname;
cout<<"Enter age:";
cin>>age;
cout<<"Enter your 1st score:";
cin>>score1;
cout<<"Enter your 2nd score:";
cin>>score2;
}
}
void viewrecord(Plrrecord)
{
}
void average(Plrrecord)
{

}

I think i messed up </3
This is the working code and I have mentioned the errors in comments

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
#include<iostream>
using namespace std;

struct Plrrecord {
char nickname[100];
char age[100];
char average[100];
char score1[100];
char score2[100];
}plr;
void addrecord(Plrrecord);
void viewrecord(Plrrecord);
void average(Plrrecord);
void maxave(Plrrecord);
void minave(Plrrecord);

int main() {
char option;
cout << "=================================================\n";
cout << "\t\t\tMENU\n";
cout << "1. Add record\n";
cout << "2. View players records\n"
<< "3. Compute for the average\n"
<< "4. Show the player(s) who gets the max average.\n"
<< "5. Show the player(s) who gets the min average.\n"
<< "6. Exit\n";

switch (option)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
break;//there should be a break statement after switch (unless its c#)
}
system("pause>0");
return 0;
}

void addrecord(Plrrecord Plr)//you also need data variable in function
{
for( int i=0;i<5;i++)
{
cout<<"Enter your nickname:";
cin>>Plr.nickname;//since the function is not inside the struct the name needs to be defined
cout<<"Enter age:";
cin>>Plr.age;//same as above
cout<<"Enter your 1st score:";
cin>>Plr.score1;//same as above
cout<<"Enter your 2nd score:";
cin>>Plr.score2;//same as above
}
}
void viewrecord(Plrrecord)
{
}
void average(Plrrecord)
{

}


Also

1
2
3
4
5
char nickname[100];
char age[100];
char average[100];
char score1[100];
char score2[100]


this piece of code takes an awfully lot of memory(500kb), either use strings or use vectors

HOPE IT HELPS
How to call the void(addrecord) in Switch???
1
2
3
4
5
6
std::string nickname;
unsigned short age;
double average;
double score1;
double score2;
 


std::vector<Plrrecord>;

Always put a default: case in a switch

Edit:
How to call the void(addrecord) in Switch???


You know how to do function calls.
Last edited on
I'm gonna cry T.T
{
case '1':addrecord();
cout<<addrecord(Plrrecord);
break;
case '2':
is it like this?
case '1':void addrecord(Plrrecord Plr);
break;

it doesnt work
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) void addrecord(Plrrecord Plr);

That's not how you call a function. You clearly know how to call a function, because you do it successfully elsewhere in your code, but if you're still confused about how to do it, I strongly recommend going back to your textbook and making sure you understand it. It's absolutely crucial to C and C++ programming.

addrecord is done by the ctor, viewrecord by the overloaded << operator, average is calculated as objects are constructed.
Data types changes as per shadder & TheIdeasMan's suggestions, the latter also mentioning std::vector. Incorporating all

these good stuff, see below and shout if anything's unclear:
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::cin;
using std:: cout;

int constexpr num_players {5};

struct Player
{
    std::string _nickname;
    unsigned short _age;
    double _average;
    double _score1;
    double _score2;

    Player();
    bool operator < (const Player& rhs)const;
};
struct MaxAvg
{
    bool operator () (const Player& lhs, const Player& rhs)
    {
        return (lhs._average > rhs._average);
    }
};
std::ostream& operator << (std::ostream& os, const Player& p);

int main()
{
    std::vector<Player> Players;
    bool fQuit = false;
    while (!fQuit)
    {
        cout << "1. Add players \t2. View players records";
        cout << "\n3. Show player(s) with max average \t4. Show player(s) with min average";
        cout << "\n5. Quit \n";

        int option{};
        cin >> option;
        switch (option)
        {
            case 1:
            {
                int i{};
                do
                {
                    cout << "Player: " << i + 1 << " of " << num_players << '\n';
                    Player p;
                    Players.push_back(std::move(p));
                    i++;
                } while (i < num_players);
            }
            break;
            case 2:
            {
                if(Players.size() == 0)
                {
                    cout << "No player records available \n";
                }
                else
                {
                    for (auto& elem : Players)
                    {
                        cout << elem;
                    }
                    cout << '\n';
                }
            }
            break;
            case 3:
            {
                if(Players.size() == 0)
                {
                    cout << "No player records available \n";
                }
                else
                {
                    std::sort(Players.begin(), Players.end());
                    cout << "Player(s) with min average: \n";
                    for (auto& elem : Players)
                    {
                        if (elem._average == Players[0]._average)
                        {
                            cout << elem << '\n';
                        }
                    }
                }
            }
            break;
            case 4:
            {
                if(Players.size() == 0)
                {
                    cout << "No player records available \n";
                }
                else
                {
                    std::sort(Players.begin(), Players.end(), MaxAvg());
                    cout << "Player(s) with max average: \n";
                    for (auto& elem : Players)
                    {
                        if (elem._average == Players[0]._average)
                        {
                            cout << elem << '\n';
                        }
                    }
                }
            }
            break;
            case 5:
            {
                fQuit = true;
                cout << "Goodbye!\n";
            }
            break;
            default:
                cout << "Invalid option, try again \n";
                break;
        }
    }
return 0;
}
Player::Player()
{
    cout << "Enter your nickname: \n";
    cin >>_nickname;
    cout << "Enter age: \n";
    cin >> _age;
    cout << "Enter your 1st score: \n";
    cin >> _score1;
    cout << "Enter your 2nd score: \n";
    cin >> _score2;
    _average = (_score1 + _score2)/2;
}
std::ostream& operator << (std::ostream& os, const Player& p)
{
    os << "Nickname: " << p._nickname << " Age: " << p._age << " ";
    os << "1st score: " << p._score1 << " 2nd score: " << p._score2;
    os << " Average: " << p._average << '\n';

    return os;
}
bool Player::operator < (const Player& rhs)const
{
    return _average < rhs._average;
}


Topic archived. No new replies allowed.