Need Help! Brand New

Is there a better way to do this? The following are my instructions. The problem I am having is after I instruct 'y' its not showing my enter goals scored in the program but its allowing me to enter the number. the calculations are correct but the program has to be done to where the user has to hit yes for 5 games where game 2 totals add to game 1 and game 3 totals add to game 2 and so forth. Hopefully someone understands what I am trying to ask. Thank you for your help. Do I have at least the right idea?

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
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iostream>
#include <iomanip>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main ()
{
		int goals, num2;                                           //Processing data requirements should be integer
	    int shotsAttempted, shot2;                                  //Processing data requirements should be integer
		float percent = 100.0 * goals / shotsAttempted;      //Calculations should be float or double
		char answer,
		y, n;	   	   	   	   	   	   	   	   	   	    // is there more data? Y or N
		
		cout << " Enter goals scored: ";
		cin >> goals;
		
		if (goals < 0)
		{
		cout << "Goals entered must be greater than 0.....Please re-enter goals: "<<endl;
		cin >> goals;
		}

		
		cout << " Enter shots attempted: ";
	    cin >> shotsAttempted;
		if (shotsAttempted < 0)
		{ 
		cout << " Shots attempted must be greater than 0......Please re-enter shots attempted: "<<endl;
		cin >> shotsAttempted;
		}
		
	    percent = 100.0 * goals / shotsAttempted;
		cout << setprecision (1) << fixed << percent;
		cout << "\n The shooting percentage is:  " << percent;
		
		// Ask the user if more data is needed to be entered after the first calculation.

		cout << "\n Is there more data? y or n? \n";
		cin >> answer;
		
		if( answer == y)

 	 	cout << " \n Enter goals scored: ";
		cin >> num2; 
		
		cout << " Enter shots attempted: ";
		cin >> shot2;
		
		percent = 100.0 * (goals + num2) / (shotsAttempted + shot2);
		cout << setprecision (1) << fixed << percent;
		cout << " \n The shooting percentage after two games is: " << percent;

	

				
return 0;
}

closed account (E3h7X9L8)
if(answer == 'y')

using y instead of 'y' is like comparing your answer to an unknow variable, note that you dont use "y" , you use "" for multiple characters and '' for only one character

EDIT: oh and put
1
2
3
4
5
cout << " \n Enter goals scored: ";
		cin >> num2; 
		
		cout << " Enter shots attempted: ";
		cin >> shot2;
in { } bracers, or else after the if statement processes your answer it will only do cout << " \n Enter goals scored: ";
Last edited on
What would a good loop be for this? Apparently I was supposed to add a loop and not sure if I did or not.
Last edited on
What's to stop a user from entering zero when prompted on lines 23 and 32? Use loops which only terminate when a user enters valid input.

Additionally:

1.) Place lines 11 - 42 in a loop, which terminates when there's no more data.
2.) Line 13 is not an equation, it's an assignment (and a bad one at that, too. You're using unintialized variables. The assignment is pointless to begin with since you effectively discard any garbage value percent holds later on when you perform the actual calculation).
3.) goals / shotsAttempted is an integer division in case it matters.
4.) There's a discrepancy on lines 20 and 22. Zero is not greater than zero.

Consider this pseudo-code:

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
int main() {

	bool do_it_again = false;

	float percent = 0.0f;

	do {

		int num_goals = 0;
		while (/*input is invalid*/) {
			//ask for user input

			//num_goals might be invalid if it's less than zero.
		}

		int num_shots = 0;
		while (/*input is invalid*/) {
			//ask for user input

			//num_shots might be invalid if it's zero or less than num_goals.
		}

		//calculate percentage
		//print results
		//ask user if there is more data
		//if there is more data, set do_it_again to true.

	} while (do_it_again);

	return 0;
}
Last edited on
What are you talking about for number 4 on line 20 and 22. It's how its supposed to be if a user enters a negative number. I don't think you read that part of the program right.

1
2
3
if (goals < 0)
		{
		cout << "Goals entered must be greater than 0.....Please re-enter goals: "<<endl;


In other words:

If goals is less than zero, tell the user the number of goals must be greater than zero.
What if the number of goals is zero? Zero is neither greater nor less than zero.
Last edited on
ah ok thanks. This is what I have now. I totally had to redo everything just have to figure out my errors. in the calculations department. and i know there is a couple more errors. I 'm learning.



#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int numGoal, numShots, numGames;
totalGoals = 0;
totalShots = 0;
double ShootingPercentage;
string Continue = "y";
numGames = 1;

while ( Continue == "y")
{

cout << "Enter the number of goals attempted: ";
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 = numGoal / (double) numShots * 100.0;


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;

}

Topic archived. No new replies allowed.