Output error

Hello. When I compile and run my program, and input numbers when prompted, the end result is always 0. It is supposed to run the numbers through an equation, and at the end, output a number. No matter what numbers have been inputted, the output is always a 0.

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

using namespace std;

int main()
{

int numGoals;
float numMinute, goalsAgainst, goalAllow, output;

cout << "Enter the number of goals that have been allowed: ";
cin >> numGoals;

if (numGoals < 0 )
{
	cout << "Error: The number of goals cannot be negative. Try again: ";
	cin >> numGoals;
}
	cout << "Enter the number of minutes of ice time: ";
	cin >> numMinute;
if (numMinute < 0)
{
	cout << "Error: The number of minutes cannot be negative. Try again: ";
	cin >> numMinute;
}
cout << "The Goals Against Average is " << setprecision(2) << goalsAgainst;

goalsAgainst = ((numGoals * 60) / numMinute);


return 0;
}
code executes from top to bottom.
you assign a value to `goalsAgainst' after you print it.
when you declare a variable, the variable by default will hold nothing which means 0 (or NULL).
compiler goes line by line, so what you did is first, you printed goalsAgainst (while it has no value) and then you assigned some data values into goalsAgainst and program goes off.
just switch last 2 lines and it should be fine. You assign a value first AND then print it.
Topic archived. No new replies allowed.