I have played around with this loop some and the loop works fine but the math is coming out all wrong just cant get it.

Pages: 12
Not sure what change to make in the math I have changed variables around just can't get it.

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

using namespace std;

int main()
{
    int fillup, bodo, eodo, counter;
    double pergal;
    float galtot, eodotot, bodotot, total, filltot;
    char x;



    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;

    eodo += eodotot;
    bodo += bodotot;
    total = eodotot - bodotot;

    fillup+=filltot;
    }


    for (counter=0; counter<3; counter++)
    {
        cout << " Price per gallon at the gas station ";
        cin >> pergal;

        pergal+=galtot;
    }

    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;

    }

Perhaps you should increase your compiler warning level, then fix any and all warning messages.

In function 'int main()': 15:14: warning: 'x' is used uninitialized in this function [-Wuninitialized] 26:20: warning: 'eodotot' may be used uninitialized in this function [-Wmaybe-uninitialized] 27:20: warning: 'bodotot' may be used uninitialized in this function [-Wmaybe-uninitialized] 30:20: warning: 'filltot' may be used uninitialized in this function [-Wmaybe-uninitialized] 39:23: warning: 'galtot' is used uninitialized in this function [-Wuninitialized] 47:58: warning: 'total' may be used uninitialized in this function [-Wmaybe-uninitialized]

Last edited on
Ok yes I notice now its incomplete and has no return 0; let me redo it quick I was playing around with it
I dont not understan it has been declared it is initialized? I am not getting those errors ?
I dont not understan it has been declared it is initialized?

No just declaring a variable doesn't initialize the variable. A variable must be assigned a value before you try to use it in a calculation.

I am not getting those errors ?

They are not errors, but warnings that should be fixed. If you're not getting any warnings you need to increase your compiler warning levels. Read your compiler documentation to find out how to increase the warning levels.
oh ok well in that case they are being assigned values the math is wrong
when you input the values there is no error something is wrong not what your showing me
oh ok well in that case they are being assigned values the math is wrong

No they're not being assigned values!

Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int fillup, bodo, eodo, counter;
...
    float galtot, eodotot, bodotot, total, filltot;
    char x;

    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;

    eodo += eodotot;


Where is x assigned a value? Where is eodotot assigned a value?

when you input the values there is no error something is wrong not what your showing me

Yes there are some problems that I may not be showing you, however until you fix the issues I pointed out you won't be able to tell where any of these other problems may be located.

By the way do you understand what the operator+= is actually doing? Looking at the above code it doesn't appear that you do.
been awhile since I didi loop but doesnt += give the value accumulated during toop to that i Instead of writing over it? there isnt a problem with the code it is in the math nt? or the ints
Last edited on
closed account (E0p9LyTq)
miah612000 wrote:
Ok yes I notice now its incomplete and has no return 0;


return 0; is not required in main(), and only main():

http://en.cppreference.com/w/cpp/language/main_function
4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.

All other functions that have a return value are required to have at least one return statement.

Confusing, isn't it. ;)
totally nothing to do with the question butok
been awhile since I didi loop but doesnt += give the value accumulated during toop to that i Instead of writing over it?

What is this "it" you're talking about?

What variable is getting added to what variable?

Look at this snippet:
eodo += eodotot;
Do you realize that this is equivalent?
eodo = eodo + eodotot;
Is that really what you want?

there isnt a problem with the code it is in the math nt?

What?

There is a problem with the code, I haven't even looked at the math because until the problems with the code are fixed figuring out if there is a problem with the math is near impossible.

not sure what your saying I dont have eodo = eodo + edotot it is a loop and I dont want it written over so eodo is += to eodotot now my math is wrong the question is the math i have tried moving the logic statements and giving int float values I am not sure what to do everything works but the math
i should be giving accumulated value to eodotot ?
every tme the loop runs eodo is added to eodotot to accumulate is that what I want? obviously now do you see?
not sure what your saying I dont have eodo = eodo + edotot

You have eodo += edotot which is the same as eodo = eodo + edotot.

FIX YOUR UNINITIALIZED VARIABLES FIRST.

edotot is never assigned a value, remember in C++ the value on the left side of the operator (lvalue) is assigned the value on the right side of the equation (rvalue). And remember in order to use the operator += the lvalue must be initialized before this operation.



sir even if I call them 0 when I call variables the unitialized error is gone and still wrong can you / do you know the answe to my problem sounds like your farting around with something you do not know how to do ?
very glad you reported because you are an idiot and nothing in 25 replies has had anything to do with why the variables do not add you should not to be platying aroud on threads
Since you seem to think I don't know what I'm talking about, good luck.

By the way I don't know who reported that post, but it wasn't me. I wish you the best of luck, if you did actually fix the problems with the uninitialized variables and the issue with the operator+= that I've already mentioned your program has quite a few other logic errors that you will need to find and solve, so again good luck.

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.
Last edited on
Pages: 12