Hey guys, I'm new at this and was wondering if someone could help. I need to calculate the average amount of gallons used per km driven but I don't know how to do it. What do I need to include in my code(below) to include an average?
I'd appreciate any advice you could provide to me :)

#include <iostream>
    using namespace std;
int main()
{
 
    
    double gallons;
    double average;// not used yet
    double distance;
    double distancegallon;
    
    
   	 cout<<"Please enter the amount of  gallons used( or 0 to quit) "<<endl;
  	  cin>>gallons;
    
	  while(gallons != 0)
 		 {
		   cout<<"Please enter distance in KM"<<endl;
		   cin>>distance;
    
   		distancegallon = distance/gallon;

		cout<<"KM/ Gallon is:"<<distancegallon<<endl;   
				           

		 }		
				  
				  
      
    return 0;
}

think about how would you calculate the average galles per k with pencil & paper. Given the distance and gallons its just simply average = distance/gallons If you want the average over several tanks of gas then sum up all the gas you bought then do the division. This is just 5th grade (or earlier) math.

think about how would you calculate the average galles per k with pencil & paper. Given the distance and gallons its just simply average = distance/gallons If you want the average over several tanks of gas then sum up all the gas you bought then do the division. This is just 5th grade (or earlier) math.

Thanks for replying, I know about the average.. I just don't know how to get the sum of all the tanks used - what code to use ?

>> while(gallons != 0)
That while statement won't work because you never change the value of gallons, that makes it an infinite loop.

>> just don't know how to get the sum of all the tanks used
just sum them up as you enter them

int total = 0;
int gallons = 0;
do {
    cout << "Enter gallons\n";
    cin >> gallons
    cin.ignore(); // flush keyboard of the '\n' key
    total += gallons;
}while( gallons > 0);

When you fill your car with gas you have to record both the number of gallons and the distance traveled for the previous tank of gas. I would start out by filling the gas tank and setting the trip odometer to 0. The next time I get gas I would record the number of gallons purchased and the distance indiated on the trip odometer. Divide the two and I get the gallons/miles or galles/kilo or whatever measurement you want to use. Or you could just keep track of all the gallons then when the trip is finished do the division with the sum.

Now to do the same in the program, you didn't post the exact program requirements -- what you posted in ambiguous. Can you enter just two amounts -- total gallons and total distance ? Or do you have to enter gallons and distance for each tank of gas ?

use a loop to keep gathering data

int sum =0;
int tank =0;
while (cin.get() !="")
cin >> tank;
int sum =sum+tank;

I'm not sure about the "" though...

JRM: sorry but the loop you posted won't work either. I'll leave it up to you to figure out why

Ok, thanks. I fixed it. It ended up being essentially what you had posted.

The compiler didn't like the "" test. I had nothing to test anyway without the "do"
The error said that it forbade comparison between an integer and a POINTER.
How did it come to interpret it that way?

The sum worked fine even though i didn't perform the nuance of flushing the return.
How did i get away with that? Was it the smart g++ comliler or is it a potential bug waiting to to bite if I don't flush the buffer?

int sum =0;
int tank =0;

do
{
cin >> tank;
sum =sum+tank;
}
while (tank !=0);

>>How did it come to interpret it that way?
"" is an empty literal string. The compiler is calling it a char pointer rather than a char array.

>>while (cin.get() !="")

get() is an overloaded istream method that can be used to get either a single char or a C style string, depending on the parameter sent it. You didn't give it a parameter so it defaults to reading in a single char. You can't compare a single char to a char array/string. You can't compare two C style strings with == or !=. You can compare two char with == or !=. But the real question, which you figured out and corrected in the second post, is why use get() at all?

>>is it a potential bug.

Yes. Consider the following stripped down version of the original snippet.

int sum = 0;
while()
  int sum = sum+tank;

Each time through the loop you declare another variable called sum on the left hand side of the = operator in the third line above. Each time through you would have ignored/overwritten the value of the variable from the previous loop, or after the loop is completed the number of variables called sum would equal the number of times you went through the loop.

But even more confusing is what is the variable sum on the right hand side of the = operator? Is it the variable declared outside the while loop or the variable declared on the left hand side of the = operator? If it's the variable outside the loop, then how does it change if you assign a new value to a new variable each time through the loop. If it's the variable on the left hand side of the assignment operator then how can you add something to something that doesn't have a meaningful value yet and then assign it itself anyway?

If you got the right answer using the original snippet then somehow the compiler came up with the correct assumptions along the way this time around, but I wouldn't expect it to do so every time.

BTW: You used the correct syntax in the second post.

This article has been dead for over six months. Start a new discussion instead.