Ok, so your program has several problems which jlb has already outlined so lets see if we can fix them.
The first and most glaring error is the uninitialized
char x;
on line 11. This code only declares the var it does not give it any initial value. Ao when the compiler reaches the while loop and the Boolean logic statement
x != 'n'
x cannot be compared to n as x has no value. This is also the issue you are running into with these statements. Correct code should look like this
char x = 'y'
1 2
|
eodo += eodotot;
bodo += bodotot;
|
As jlb stated the above code is equivalent to
eodo = eodo + edotot
however eodotot has no value and therefore cannot be added to eodo.
I would suggest reading the section on varibles specifically the parts on declaring and initializing:
http://www.cplusplus.com/doc/tutorial/variables/
Now that we understand the issue you are having with your code we can go about fixing it. All variables should be initialized to whatever the desired starting value is. In this case im assuming its is 0 and as such it should look like this:
1 2 3 4 5
|
int fillup = 0, bodo = 0, eodo = 0;
double pergal 0;
float galtot = 0, eodotot = 0, bodotot = 0, total = 0, filltot = 0;
|
Furthermore, it is common practice and considered good style to initialize the counting vars in for loops in the parameters, as such
for (int counter = 0; counter<3; counter++)
for the loop its self rather than at the top.
Now that we have the var declaration figured out lets move onto the final error. If we run the code we have now the final totals of avg mpg, cost to drive, ect is going to be mixed up. This is due to you use of the
+=
operator. As previously mentioned this is the same as
x + y = x
but you have the vars backwards. for instance in
pergal+= galtot
should be
galtot += pergal
. See this page on opperators in c++ and ready through it to see why:
http://www.cplusplus.com/doc/tutorial/operators/
Now that we have this all fixed the fixxed code looks like this and, at least as far as I can see runs fine.
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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int fillup = 0, bodo = 0, eodo = 0;
double pergal = 0;
float galtot = 0, eodotot = 0, bodotot = 0, total = 0, filltot = 0;
char x = 'y';
while (x != 'n')
{
cout << " Enter beginning odometer reading ";
cin >> bodo;
cout << " Enter the end odometer reading ";
cin >> eodo;
cout << " Enter the gallons required to fill up ";
cin >> fillup;
cout << " Enter y to enter more readings or n to quit ";
cin >> x;
eodotot += eodo;
bodotot += bodo;
total = eodotot - bodotot;
filltot += fillup;
}
for (int counter = 0; counter< 3; counter++)
{
cout << " Price per gallon at the gas station ";
cin >> pergal;
galtot += pergal;
}
cout << setprecision(2) << fixed;
cout << " The average mile-per-gallon is: " << total / filltot << endl;
cout << " The average price per gallon is: " << galtot / 3 << endl;
cout << " The cost to drive one mile is: " << galtot / total / filltot << endl;
cin.get();
cin.get();
}
|
Hope this helped and just reply if I missed an error. :D
On a side note jlb was trying to help and was correct. Try to be nicer to those who try to help you.