Problem with the math / averages lines 46-48

some thing is very wrong I have at this two days changing variables around and trying to get this right. I am not asking anyone to do the work for mr it is homework, but plz, plz, plz, point me in the right direction. here is my program:
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
 #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    int count=1, log, fors, begodo, begodos, endodo, endodos, gal, pergal;
    double odototal;
    float pergals, gals;


    cout << "How many entries / fill ups do you wish to log ? " << endl;
    cin >> log;
    cout << endl;

    while ( count <= log )
    {
    cout << "Enter odometer reading at beginning of your trip: " << endl;
    cin >> begodo;
    cout << "Enter odometer reading at the end of your trip: " << endl;
    cin >> endodo;
    cout << "Enter the gallons needed at your fill up: " << endl;
    cin >> gal;

    begodos += begodo;
    endodos += endodo;
    gals += gal;

    count++;
    }

    cout << "You'll need to enter price-per-gallon of 3 service stations." << endl;
    for (fors = 0; fors <= 4; fors++)
    {
    cout << "Price per gallon (using the decimal ) ?" << endl;
    cin >> pergal;


    pergals += pergal;
    fors++;
    }
    odototal = endodo - begodo;

    cout << "Your cars average mile per gallon is: " << odototal / gals << endl;
    cout << "You bought gas at an average of: " << pergals / fors << endl;
    cout << "The cost for your car to go one mile is: " << endl;


    return 0;
}
Last edited on
First, picking better names for your variables will help to make it clear to you and other programmers what you are trying to do. Use complete words for your variable names. Modern computers have plenty of space that you don't need to abbreviate. Instead of begodo, use beginning_odometer_value or beginningOdometerValue depending on your preference. At the moment, I can't tell what begodos and endodos are supposed to be. Use longer names, and I think you'll see that the operations you are performing don't make sense.

Second, adding odometer readings does not result in a meaningful number. Subtracting odometers readings gives you the distance traveled.

Third, begodos, endodos, and pergals are not initiated with values. So, for example, when the line begodos += begodo is run, it will still have a random value. You need to give every variable a starting value (in your case, usually zero).

Fourth,
1
2
3
4
5
cout << "You'll need to enter price-per-gallon of 3 service stations." << endl;
for (fors = 0; fors <= 4; fors++)
{
    // ...
}

This will result in 5 requests for gas prices.

Fifth, in C++, you don't have to declare all your variables at the top of the function. It is much better to declare variables right when you first use them. For example, instead of
1
2
3
4
5
double odototal;

// ... lots of code

odototal = endodo - begodo;


You can just write
 
double odototal = endodo - begodo;

This will help avoid the uninitialized variable problem in point 3.
Hi,

The first stop is to compile the program with all the warnings on - I did this with cpp.sh - the Gear icon top right of the code. Turn on all 3 warning levels. This is the result:

 In function 'int main()': 
41:22: warning: 'pergals' may be used uninitialized in this function [-Wmaybe-uninitialized] 
46:68: warning: 'gals' may be used uninitialized in this function [-Wmaybe-uninitialized]


So a golden rule in coding is to always initialise your variables to something. In C++ one can often delay the declaration until there is a sensible value to assign to it. Otherwise assign a tell-tale value upon declaration that will show an obviously erroneous number. For example set PricePerGallon to 1000.0 That way, if you fail to assign a sensible value it should be obvious in the output.

Declaring and assigning just before needed also avoids the declaration of multiple variables on 1 line, and aids understanding.

Why are some of your variables float and some double? double is the default - stick to that.

Also, I would recommend using meaningful variable names, a lot of people have this misconception that they need to abbreviate everything when coding. Good variable and function names leads to code that is self documenting and tells a story of what is happening.

Lines 27 to 29 & 41: I find this a little counter intuitive, why can't you subtract the odometer readings, then add to a sum?

With this:

34
35
36
cout << "You'll need to enter price-per-gallon of 3 service stations." << endl;
    for (fors = 0; fors <= 4; fors++)
    {


The idiomatic for loop looks like this:

34
35
36
37
cout << "You'll need to enter price-per-gallon of 3 service stations." << endl;
    const unsigned int NumOfStations = 3;
    for (fors = 0; fors < NumOfStations; fors++)
    {


I made another const variable instead of having a "magic" number in the code. As mentioned earlier, choose good variable names, you can do better than fors My NumOfStations could be used later on line 47 rather than fors

Also there is no need to increment the fors counter again at the end of the loop on line 42.

Good Luck !!
Thank you guys, I got it to work now.
Topic archived. No new replies allowed.