Can someone help me out with this program pleases?

This is a discussion on Can someone help me out with this program pleases? within the C Programming forums, part of the General Programming Boards category; Im trying to have all the miles added together and all the gallons added together then divide. For this code ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Can someone help me out with this program pleases?

    Im trying to have all the miles added together and all the gallons added together then divide. For this code I have to have it add as many as I want until I input -1 in for gallons. I have this code correct for the most part its just that its not adding all the miles and all the gallons up. I have counter and counter1 counting how many times I've entered a mile and gallon.


    Code:
    #include <stdio.h>
    
    int main () 
    {
    	float miles;
    	float gallons;
    	int total;
    	float tmiles;
    	float tgallons;
            float taverage;
    	float average;
    	int counter;
    	int counter1;
    
    	total = 0;
    	counter = 0;
    	counter1 = 0;
    
    	printf("Enter the gallons used (-1 to end): ");
    	scanf("%f", &gallons);
    
    	printf("Enter the miles driven:");
    	scanf("%f", &miles);
    
    	average = miles / gallons;
    
    	printf("The miles / gallons for this tank was %f\n\n", average);
    
    	while(gallons != -1) {
    		tgallons = total + gallons;
    		tmiles = total  + miles;
    
    		printf("Enter the gallons used (-1 to end): ");
    		scanf("%f", &gallons);
    
    
    		if(gallons == -1)
    			break;
    
    		counter = counter + 1;
    
    		printf("Enter the miles driven:");
    		scanf("%f", &miles);
    
    		counter1 = counter1 + 1;
    
    		average = miles / gallons;
    
    		printf("The miles / gallons for this tank was %f\n\n", average);
    
    		counter++;
    	}
    
    	if(tgallons != 0) {
    
    		taverage = tmiles / tgallons;
    
    		printf("The overall average miles / gallons was %f\n", taverage);
    	}
    
    	return 0;
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all it looks to me like you haven't initialized the tgallons and tmiles to zero.

    The math here is also wrong:
    Code:
    		tgallons = total + gallons;
    		tmiles = total  + miles;
    You probably want to change total to the corresponding tX.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    What do u mean by change the total to the corresponding tx? You mean like actually change total and replace it with a different variable?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Nm thanks I got it. Ill be sure to post more questions if i need help with my next program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21