Help with math functions

Background info: for every win is 5 points, every tie is 2 points, every loss is 0 points.
i am trying to make a function that would add up all the points.
the function I am trying to get help on is TeamScore::getpoints()
// Mutator functions
void TeamScore::updateWins()
{
num_wins++;
}
void TeamScore::updateTies()
{
num_ties++;
}
void TeamScore::updateLosses()
{
num_losses++;
}

// Get Functions
string TeamScore::getName()
{
return teamName;
}

int TeamScore::getNum_wins()
{
return num_wins;
}

int TeamScore::getNum_ties()
{
return num_ties;
}

int TeamScore::getNum_losses()
{
return num_losses;
}
int TeamScore::getPoints()
{
int totalPts = (num_wins * 5) += (num_ties * 2) += (num_losses * 0);
return totalPts;
}
Last edited on
Use +, not +=.

+= has a different meaning. In particular, neither (num_wins * 5) nor (num_ties * 2) can appear on the left hand side of +=.
when i used + it comes of '+': result of expression not used
What does TeamScore::getPoints() look like after the change?
Hello ellgoose,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

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

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



It would be helpful to have the class and enough code to compile and test.

Andy
Hello ellgoose,

After guessing at the missing code and after making the changes that helios mentioned I get this output.


team 1's total points are 31  // <--- Original values.

team 1's total points are 36  // <--- After adding 1 win.


 Press Enter to continue:



The new function is:
1
2
3
4
5
6
7
8
int TeamScore::getPoints()
{
    //int totalPts = (num_wins * 5) + (num_ties * 2)/* + (num_losses * 0)*/;

    //return totalPts;

    return (num_wins * 5) + (num_ties * 2);
}

There is no need to go through all that work when 1 line will do. "(num_losses * 0)" it does not matter the value of "num_losses" is. Anything * 0 is always zero. You are wasting your time to put this in the calculation. But it makes no difference if you feel that it is needed.

Andy
Topic archived. No new replies allowed.