Call by reference or value?

I'm doing a class program and the output shows some garbage values so I figured some things needed to be call by reference but I'm not sure as to where. Sorry if the code doesn't display nicely, I haven't mastered how to display it on here yet.

header
#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:
void initialize ();
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++;
}

void Hockey::initialize ()
{
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.";
}

main
#include <cstdlib>
#include <iostream>
#include "Hockey.h"
using namespace std;


void initialize ();
void tripping ();
void fighting ();
void score_goal ();
void assist_goal ();
void print ();

int main(int argc, char *argv[])
{
Hockey eichel, kane;

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 ();

cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
Use the <> code tags under the format section - http://www.cplusplus.com/articles/jEywvCM9/
Hello dear cplusplus annoyed,
the problom with the code is that you never actually initizalized the private member variables of the hockey class. You defined a function 'void initialize()' but you never called it in main once you defined the 'eichel' and 'kane' objects.
Try adding the lines

1
2
eichel.initialize();
kane.initialize();

Otherwise if you do not wan to worry about doing this each time you create a hockey object in the header file instead of 'void initialize()' write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Hockey{
private:
        int goals, // number of goals
        assists, // number of assists
        penalties, // number of penalties
        penalty_minutes; // total of penalty minutes
public:
        Hockey ();   // Constructors: takes care of initializing variables
        void tripping ();
        void fighting ();
        void score_goal ();
        void assist_goal ();
        void print ();
};

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

Topic archived. No new replies allowed.