My program is meant to calculate the shooting percentage of a hockey team. It's an assignment I've had to build on over the last few weeks and for this one I've had to do (and have completed) the following:
-Use a loop for error checking (completed)
-Display and keep track of number of games played (completed)
-Keep track of the total number of goals that have been scored and the total number of shots attempted and use both totals to calculate the shooting percentage
-Display the change in shooting percentage from when the program starts and till when it ends
The last two item(s) is where I am struggling. I understand that I'll have to create a new variable in order to store my input values from each game but I am having trouble on figuring out how to go about saving the data and using it again later on. Will I be creating a new formula to solve for the calculated shooting percentage from all games and have that stored and later displayed in another variable?
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int numGoal, numShots, numGames,;
double ShootingPercentage;
string Continue = "y";
numGames = 1;
while ( Continue == "y")
{
cout << "Enter the number of shots attempted by the Blackhawks: ";
cin >> numShots;
while (numShots < 0)
{
cout << "Error: The number of shots must be greater than 0. Try again: ";
cin >> numShots;
}
cout << "\nEnter the number of goals scored by the Blackhawks: ";
cin >> numGoal;
while (numGoal < 0 )
{
cout << "Error: The number of goals must be greater than 0. Try again: ";
cin >> numGoal;
}
ShootingPercentage = numGoal / (double) numShots * 100.0;
cout << setprecision(1) << fixed << "\nThe shooting percentage of the Blackhawks after " << numGames << " game(s) is " <<ShootingPercentage <<endl;
numGames++;
cout << "\nIs there more data? (y or n): ";
cin >> Continue;
}
return 0;
}
I'm not sure I am following. What I am trying to do is create a program that will calculate multiple shooting percentages rather than just one at a time. Example:
Shots attempted: 10
Goals Scored: 5
Percentage: 50%
I want it to factor in the previous information at the start and also calculate that alongside new information that is input.
Edit: I was able to solve it by creating two new variables (TotalGoals and TotalShots) setting them =0; and then placing
TotalGoals += numGoal;
TotalShots += numShots;
before my ShootingPercentage equation.
is there a way to go about adding code that can calculate the difference from when the program begins to when it ends?