Call by reference

Oct 22, 2015 at 4:29pm
This program is supposed to be scoring goals. The first two, eichel and kane are different from the last one, hodgson. A function was added to main called score_goals, it can't be apart of the member functions. Anyways, score_goals (hodgson, 7) gets hodgson to score 7 goals and when I put in hodgson.print() the 7 goals are supposed to be referenced there as well but when I make void score_goals (Hockey player, int &goals) call by reference like it should be, it doesn't work. It says that there's an error.

main
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
#include <cstdlib>
#include <iostream>
#include "Hockey.h"
using namespace std;


void tripping ();
void fighting ();
void score_goal ();
void assist_goal ();
void print ();
void score_goals (Hockey player, int &goals);
      
int main(int argc, char *argv[])
{
	Hockey eichel, kane, hodgson;
	
	
    cout << "Eichel stats: ";
    eichel.score_goal ();
    eichel.score_goal ();
    eichel.assist_goal ();
    eichel.tripping ();
    eichel.print ();

    cout << "\n\nKane stats: ";
	kane.assist_goal ();
	kane.assist_goal ();
	kane.assist_goal ();
	kane.fighting ();
	kane.fighting ();
	kane.print ();
	
	score_goals (hodgson, 7);
	cout << "\n\nHodgson stats: ";
	hodgson.print();
	
    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

void score_goals (Hockey player, int &goals)
	{
		int count;
		for (count = 0; count < goals; count++)
			player.score_goal();
		cout << "\n\nPlayer in function: ";
		player.print ();
		cout <<"\n\n";
	}


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

// Hockey class keeping track of a player's goals, assists,
// penalties and penalty minutes
class Hockey
{
      private:
        int goals,            // number of goals
            assists,          // number of assists
            penalties,        // number of penalties
            penalty_minutes;  // total of penalty minutes
      public:
         Hockey (); 
         void tripping ();
         void fighting ();
         void score_goal ();
         void assist_goal ();
         void print ();
};

// Records the fact that the player has scored another goal
void Hockey::score_goal ()
{
     goals++;
}

Hockey::Hockey ()
{
     goals=0;
     assists=0;
     penalties=0;
     penalty_minutes=0;
}

void Hockey::tripping ()
{
     penalties++;
     penalty_minutes += 2;
     
}

void Hockey::fighting ()
{
     penalties++;
     penalty_minutes++;
     penalty_minutes++;
     penalty_minutes++;
     penalty_minutes++;
     penalty_minutes++;
}

void Hockey::assist_goal ()
{
     assists++;
}

// Prints the player's current statistics
void Hockey::print ()
{
     cout << "\n\nGoals:   " << goals;
     cout << "\nAssists: " << assists;
     if (penalties == 1)
       cout << "\n" << penalties << " penalty for " 
            << penalty_minutes << " minutes.";
     else
        cout << "\n" << penalties << " penalties for " 
          << penalty_minutes << " minutes.";
}
Last edited on Oct 22, 2015 at 4:30pm
Oct 22, 2015 at 5:46pm
Why do you have to pass goals by reference? Do you understand what that is supposed to mean?
Oct 22, 2015 at 6:00pm
Yes I understand what it means but my professor told me that it has to be passed by reference. I didn't understand it myself. If score_goals was made apart of the member functions then things would be accomplished much more easily but he requested it to be by reference.
Oct 23, 2015 at 11:06am
I'm not sure you do understand what it means. If you did, you'd understand why:

 
score_goals (hodgson, 7);


can't possibly make any sense. How would a reference to an integer literal possibly work? What is it that you think you're doing here?
Last edited on Oct 23, 2015 at 11:06am
Topic archived. No new replies allowed.