Trouble With Loops

Hello,

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?

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

using namespace 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;
		
}

closed account (48T7M4Gy)
If you want to save the results from day to day then yopu need to save the results in a data file - ie persistence.

To save a series of results in memory for processing (volatile memory) then use arrays, or vectors.

In both cases you will likely need to use for/while loopsto run through the data series at the input and output phases of your program.

scores[count]
total = 0

for ( round = 1 to count)
total = total + score[round]

average = total/count
Last edited on
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%

Shots attempted: 11
Goals Scored: 6
Percentage: 52.4%

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?
Last edited on
Topic archived. No new replies allowed.