-
bad math?
Code:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main()
{
int number, number1, number2;
const int AVERAGE = number + number1 + number2 / 3;
cout << "Pick a number!: ";
cin >> number;
cout << "Another number: ";
cin >> number1;
cout << "Last number!!: ";
cin >> number2;
(number + number1 + number2);
cout << "\nThe avereage is: " << (AVERAGE) << "\n";
system("\nPAUSE");
return 0;
}
I get 9333568 eveytime. y?
-
At the time when you initialised AVERAGE, the values of number, number1 and number2 were garbage since they were not initialised. Garbage in, garbage out, so the saying goes, thus AVERAGE contained garbage when you printed it.
What you should do instead is:
Code:
int number, number1, number2;
cout << "Pick a number!: ";
cin >> number;
cout << "Another number: ";
cin >> number1;
cout << "Last number!!: ";
cin >> number2;
int average = (number + number1 + number2) / 3;
cout << "\nThe average is: " << average << "\n";
Note of course that this is integer division, so the result may not be what you expect.
-
-
If you had maximum warning levels turned on, the "variable used without being initialized" warning should have been your first clue.
-
And if you used Visual Studio (2002+), then you'd get a runtime error on the average line :p