Variable declarations in classes

I'm trying a make a program that reads in the weight of three people and displays them using classes. every time I try to compile the I get this error:
cs201hw1a.cpp: In member function ‘void Display::print(Player&)’:
cs201hw1a.cpp:46:51: error: ‘weight’ was not declared in this scope
        cout << "Player " << x+1 << " weight: " << weight.at(x) << endl;
                                                   

I can't figure why it's giving me that error. Thank you everybody and here's the code in question:
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
//tyler_henderson_02@subr.edu
//a program to find the total weight of a team ;
#include <iostream>
#include <vector>
using namespace std;

class Player
{
    friend class Display;
    private:
                vector<int> weight;
    public:
                int  Enter_weight()
                {
                    cout << "Enter the weight of 3 players: \n";
                    for (int x(0); x < 4; ++x)
                    {
                        int input;
                        cin >> input;
                        weight.push_back(input);

                        //std::cout << "\n";
                    }
                }
};
class Display //this class displays the info
{
    public:
                void print(Player &team)
                {
                   // float total = add_weight();
                    for (int x(0); x < 4; ++x)
                         cout << "Player " << x+1 << " weight: " << weight.at(x) << endl;
//                  cout << "Player 2 weight: " << weight.at(2) << endl;
//                  cout << "Player 3 weight: " << weight.at(3) << endl;
                   // std::cout << "Total weight: " << total << std::endl;
                  }
};

int main()
{
    float total;
    Player team1;
    while(team1.Enter_weight())
    {

        Display info;
        info.print(team1);
    }

    return 0;
}
Last edited on
You need to specify of which player you want to access the weight.

 
cout << "Player " << x+1 << " weight: " << team.weight.at(x) << endl;


Also make sure to add a return statement to the Enter_weight() function. If you don't want to return anything you should change the return type to void.
Thank you and I have one more question. When I run the program in vim it displays the first set of data correctly but anything after that and it just repeats the first set no matter what I type and when I press crtl + d to end the loop it just goes on forever.
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
#include <iostream>
#include <vector>
using namespace std;

class Player
{
    friend class Display;
    private:
                vector<int> weight;
    public:
                int  Enter_weight()
                {
                    cout << "Enter the weight of 3 players: \n";
                    for (int x(0); x < 4; ++x)
                    {
                        int input;
                        cin >> input;
                        weight.push_back(input);

                        //std::cout << "\n";
                    }
                }
};
class Display //this class displays the info
{
    public:
                void print(Player &team)
                {
                   // float total = add_weight();
                    for (int x(0); x < 4; ++x)
                         cout << "Player " << x+1 << " weight: " << team.weight.at(x) << endl;
                  }
};

int main()
{
    float total;
    Player team1;
    while(team1.Enter_weight())
    {

        Display info;
        info.print(team1);
    }

    return 0;
}

You forgot to add a return statement to Enter_weight().
My bad, I forgot to add it on that post. In vim I made the function like this
1
2
3
4
5
6
7
8
9
10
11
bool Enter_weight()
{
      cout << "Enter the weight of 3 players: \n";
      for (int x(0); x < 4; ++x)
      {
           int input;
           cin >> input;
           weight.push_back(input);
      }
      return true;
}
because the while loop I had in line 39 gave me an error when I tried to use a void type function. After that I got the output error mentioned earlier.
If the function always return true that means the loop condition on line 39 will always be true so the loop will run forever.

To me your class design is a big confusing. You have a player called team1 that contains 4 weights.

If the player class is in fact a team class you should change the name to make that clear. In that case I don't see why you would want to have a loop in main at all. Why not simply call Enter_weight() and then print()?

You could of course have a player class but then it should only contain the weight of one player. You could still have a team class that contains a number of players, or you could use a loop in main to read and or print them all. It's up to you.

I don't see why you have a class called Display. Wouldn't it be better to make the print function part of the player and team classes? Or is this some kind of MVC design? If so, I guess it could make sense to have them in separate classes.
Last edited on
I changed the name of the class because you're right about that being confusing. This is HW assignment and requires any number of lines to work and that's why I have the loop. The reason I have the class Display is because I wanted to practice using classes. Thank you about the separate team and player class idea, I think I'm going to do that. The assignment is:
A team consist of 3 players. The total weight of a team must be less than 400 pounds. Read in the weight of 3 people, print out each person's weight and the total weight of the team and if the team weighs 400 pounds or less. Your program should work for any number of lines of data.
This code displays everything correctly but the only issue is the loop issue
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
#include <iostream>
using namespace std;

class Team
{
    friend class Display;
    private:
                weight[3];
    public:
                int  Enter_weight()
                {
                    cout << "Enter the weight of 3 players: \n";
                    for (int x(0); x < 4; ++x)
                    {
                        cin >> weight[x];
                    }
                }
};
class get_total //this class adds the weight of the players
{
    public:
                float add_weight(Team &team)
                {
                    float total = 0;
                    for (unsigned int x = 0; x < 3; ++x)
                        total += team.weight[x];
                    return total;
                }
};
class Display //this class displays the info
{
    public:
                void print(Team &team)
                {
                   // float total = add_weight();
                    for (int x(0); x < 4; ++x)
                         cout << "Player " << x+1 << " weight: " << team.weight[x] << endl;
                  }
};

int main()
{
    float total;
    Team team1;
    while(team1.Enter_weight())
    {
	 get_total GT;
        total = GT.add_weight(team1);
        if (total <= 400)
        {
            Display info;
            info.print(team1);
            cout << "Total weight of the team: " << total << endl;
            cout << "The Total weight of the team is less than or equal to  400 pounds. \n";
        }
        else
        {
            Display info;
            info.print(team1);
            cout << "Total weight of the team: " << total << endl;
            cout << "The Total weight of the team is over 400 pounds. \n";
        }

    }

    return 0;
}
I did it for you ,now the rogramme is working very good
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
#include <iostream>
using namespace std;

class Team
{
    friend class Display;
    private:
               int weight[3];/////////////////////////////////
    public:
                void  Enter_weight()///////////////////////////////
                {
                    cout << "Enter the weight of 3 players: \n";
                    for (int x(0); x < 3; ++x)/////////////////////////////////
                    {
                        cin >> weight[x];
                    }
                }
				int take_weight(int y){return(weight[y]);}/////////////////////////////////////
};
class get_total //this class adds the weight of the players
{
    public:
                float add_weight(Team &team)
                {
                    float total = 0;
                    for (unsigned int x = 0; x < 3; ++x)
                        total += team.take_weight(x);///////////////////////////////////////
                    return total;
                }
};
class Display //this class displays the info
{
    public:
                void print(Team &team)
                {
                   // float total = add_weight();
                    for (int x(0); x < 3; ++x)///////////////////////////////////////////////
                         cout << "Player " << x+1 << " weight: " << team.weight[x] << endl;
                  }
};

int main()
{
    float total;
    Team team1;
	cout<<"another(y/n) \n";////////////
		char r='y';//////////////////////
		
   while(r=='y')///////////////////////
   { team1.Enter_weight();
    
	 get_total GT;
        total = GT.add_weight(team1);
        if (total <= 400)
        {
            Display info;
            info.print(team1);
            cout << "Total weight of the team: " << total << endl;
            cout << "The Total weight of the team is less than or equal to  400 pounds. \n";
        }
        else
        {
            Display info;
            info.print(team1);
            cout << "Total weight of the team: " << total << endl;
            cout << "The Total weight of the team is over 400 pounds. \n";
        }
		cout<<"another(y/n) \n";/////////////////////
        cin>>r;//////////////////////////////////
   }
    
	system("pause");

    return 0;
}

now look
every line I put /////////////////////////////////
this mean that I built or repaired it
but
1
2
3
4
5
6
7
8
9
10
11
class get_total //this class adds the weight of the players
{
    public:
                float add_weight(Team &team)
                {
                    float total = 0;
                    for (unsigned int x = 0; x < 3; ++x)
                        total += team.take_weight(x);///////////////////////////////////////
                    return total;
                }
};

this code above is wrong in my opinoin if it was just a function this would be better
because classes are for objects witch have information and actions or functions
but this is just a function so I think it is a bad idea to treat classes this way
Thank You Peter87 and Egyptiancoder!
Topic archived. No new replies allowed.