Calculation Issues

I am a complete beginner here for C++. This is a program I need to do. The project is finished for class but I can't get the calculation part right and I am stumped. It's right for game 1 but when I add the totals to game 2 and beyond its wrong as far as what the actual percent should be; not by much though. I don't think this is bad for trying my first C++ program but any advice where I am wrong on my variable or calculation would help.

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 <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int numGoal, numShots, numGames;    //variables needed to entered
    float totalGoals = 0;                  // variable that had to be added after first attempt
    float totalShots = 0;
    double shootingPercentage;
    string Continue = "y";             // if a user enters y the shots and goals should be added to orig. totals.
    numGames = 1;
    // This is origional code by the user with ideas used from the textbook. The loop was discussed at lecture.
    while ( Continue == "y")
    {
        
        cout << "Enter the number of goals scored: ";
        cin >> numGoal;
        
        while (numGoal < 0)
        {
            cout << "Error: The number of goals must be greater than 0. Try again: ";
            cin >> numGoal;
            
        }
        cout << "\nEnter the number of shots scored: ";
        cin >> numShots;
        
        while (numShots < 0 )
        {
            cout << "Error: The number of shots must be greater than 0. Try again: ";
            cin >> numShots;
        }
        
        totalGoals += numGoal,
        totalShots += numShots;
        
        shootingPercentage = 100.0 * (numGoal + totalGoals) / (numShots + totalShots);
        
        
        cout << setprecision(1) << fixed << "\nThe shooting percentage of the after after " << numGames << " game(s) is " <<shootingPercentage <<endl;
        numGames++;
        
        cout << "\nIs there more data? (y or n): ";
        cin >> Continue;
    }
    return 0;
    
}
shootingPercentage = 100.0 * totalGoals / totalShots;
I think :)
@kbw is absolutely correct on the mathematics of your code, @OP.

I have some slight qualms with the rest though.

Line 19 and line 24: according to your error messages, the numbers entered in must be greater than 0. But if you tried to enter 0, your program would absolutely accept it. Then it would either crash from dividing by 0, or output 0 as a percentage.

Line 43: grammar issues. Also wouldn't hurt to put a "%" attached to your shootingPercentage for clarity's sake.
KBW is right. because you already increment totalGoals and totalShots at lines 37 & 38, you are double counting the current game when computing the percentage.
Oh my God! Thanks guys and girls if any. I knew it was something simple. I understand about the < 0 part but its how i was instructed to do the code for this. I even brought that up.
Oh ok. In that case you'll just need to change your error message to reflect that. Something like "# of goals must not be negative" or something. Like how it takes forever to score a goal in soccer.
Topic archived. No new replies allowed.