-2

Here is code:

#include<iostream>  
#include<string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int score_one;
int score_two;
int score_third;
int final_score = score_one * score_two * score_third;

int main()
{

cout << "What was your first score?" << endl;
cin >> score_one;

cout << "What was your second score?" << endl;
cin >> score_two;

cout << "What was your third score?" << endl;
cin >> score_third;

cout << "Your average score is: " << final_score << endl;

return 0;

}

Originally I am trying to get the average, by dividing the three scores, but that doesn't work, nor my arithmetic. It does not even multiple the variables. I use cin to get the numbers. Not sure what I am missing.

  • 1
    Note: if those variables were not global (global variables are default-initiailzed for you) odds are good the compiler would have been able to tell you what you were doing wrong. With the optimizer turned on and a few extra warning flags (`-O3 -pedantic -Wall -Wextra`), you could have gotten a few lines of "warning: 'score_one' is used uninitialized in this function" – user4581301 May 30 '18 at 00:02
  • that reminds me of Babbage's quote... `'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.` – Swift - Friday Pie May 31 '18 at 07:12

5 Answers5

3

At the time that you assign to final_score, the values of the other scores are 0 (as you haven't assigned to them yet and they're global). You then read into the scores, but never update final_score!

You need to add this after you read in the third score:

final_score = score_one * score_two * score_third;

This will update final_score.


I would also suggest staying away from global variables. I'd also suggest initializing your variables when you declare them to avoid garbage values.

Also, you're not actually calculating the average! To do that, you'll need to add your values and divide by 3, since you have 3 values total. But you've declared final_score as an integer, so you won't be able to store the average with full precision. I'd suggest declaring as a double.

Taking into account all these changes, your code will look like:

int main()
{
    int score_one = 0;
    int score_two = 0;
    int score_third = 0;
    double final_score = 0;

    cout << "What was your first score?" << endl;
    cin >> score_one;

    cout << "What was your second score?" << endl;
    cin >> score_two;

    cout << "What was your third score?" << endl;
    cin >> score_third;

    final_score = (score_one + score_two + score_third) / static_cast<double>(3);

    cout << "Your average score is: " << final_score << endl;

    return 0;
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • 1
    +1 for also pointing out that that the original does not compute the average (I think everyone, including me, focused on fixing the order). – cantordust May 30 '18 at 00:03
  • To finish things off you should test that the reads succeeded. – user4581301 May 30 '18 at 00:04
  • Math in c++ doesn't work like math in general. In math, you define relations like `total = x * y * z` which must always hold true. In c++ you preform sequential operations. The expression `total = x * y * z` assigns the *current* value of the right side to the left side and there ends the relationship between the variables. Further changes to any of `x`, `y` or `z` will not impact `total` in any way (assuming these variables are fundamental arithmetic types such as `int`). – François Andrieux May 30 '18 at 00:09
  • what is static_cast(3)? i'm new to c++ completely so it may be outside my scope right now – Jonathan Garland May 30 '18 at 00:28
  • How exactly is multiplying three numbers together and then dividing by 3 an "average" of anything? – T.C. May 30 '18 at 02:53
  • @T.C. Ha! Good call--I completely missed that. Thanks for the catch! :) – scohe001 May 30 '18 at 04:39
  • @JonathanGarland take a look at [how integer division works](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division). And then see [how you can avoid it](https://stackoverflow.com/questions/7571326/why-does-dividing-two-int-not-yield-the-right-value-when-assigned-to-double). – scohe001 May 30 '18 at 04:41
2

This line should be moved after you cin to the variables on the right hand side of the equation

int final_score = score_one * score_two * score_third;
cout << "Your average score is: " << final_score << endl;

The variable isn't somehow recomputed when those variables are later set.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

This part

int final_score = score_one * score_two * score_third;

should be inside main() after the last cin.

cantordust
  • 1,542
  • 13
  • 17
2

You have already received some answers, but I would like to offer another point of view.

It seems to me that you are used to a program like Excel, where you can set a cell to a formula (like the product of 3 other cells), and then, whenever you change any of those cells, the product is immediately updated, automatically. C++ (and, in general, programming languages) does not work like that. When you write a line like

int final_score = score_one * score_two * score_third;

you are not setting a rule, which will cause the value to be recalculated. The approach is different!

A program is executed from the beginning to the end (in practice, from the top to the bottom), and every time you assign a value to a variable (like final_score), what you are doing is reading the current value of the input variables (here, your three scores), calculating the result (which in this case is undefined, because you haven't initialised any of the scores), and assigning it to the variable, just this time. That's it. If you later change the scores, the change will not be reflected automatically on your final_score. If you want the value to be recalculated, you have to do it manually. That's why you have to move that line after the lines that read the input from the user, as the others have said.

0

You really should not use global variables, see here on why you should avoid them.

Next, instead of doing using std::cin etc. Just get used to typing it.

Lastly, use appropriate flags in your compiler to help you catch mistakes. The compiler is meant to be your friend. A good compiler would tell you,

int score_one;
int score_two;
int score_third;
int final_score = score_one + score_two + score+third / 3;

Is not initialized. To really achieve what you are thinking, you could use a function that will return a double. And that would look something like

double doAverage(int score1, int score2, int score3)
{
    return (score1 + score2 + score3) / 3.0;
}

But that will probably come later in your coding practices.

#include<iostream>  


int main()
{
    // Delare your variables here and initialize them to zero.
    int score_one = 0;
    int score_two = 0;
    int score_third = 0;
    double final_score = 0;

    std::cout << "What was your first score?" << std::endl;
    std::cin >> score_one;

    std::cout << "What was your second score?" << std::endl;
    std::cin >> score_two;

    std::cout << "What was your third score?" << std::endl;
    std::cin >> score_third;

    // Take all scores and divide it. This is the important part since
    // order matters in your code.
    final_score = (score_one + score_two + score_third) / 3.0;

    std::cout << "Your average score is: " << final_score << std::endl;

    return 0;

}

You're on the right track, you just have to look at your code and read it outloud to yourself. One of the best things you can do in programming is starting from the top and saying, "Okay, where does this break?" And follow it line by line making sense of it.

Sailanarmo
  • 1,139
  • 15
  • 41