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.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
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.