Thread: Can't figure out for the life of me what I am doing wrong.

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    33

    Can't figure out for the life of me what I am doing wrong.

    I have been working on this one program for 3 weeks now and can't figure out for the life of me what I am doing wrong.


    I can't calculate the overall average for (total miles divided by total gallons)

    This is what my output is suppose to look like:

    Welcome to my mileage calculator.

    This program will calculate the miles per gallon for ou for three tanks of gas after you have entered the gallons used and miles driven.

    Enter the number of gallons used for tank #1: 12.8
    Enter the number of miles driven: 287.1
    ***The miles per gallon used for this tank is 22.4

    Enter the number of gallons used for tank #1: 10.3
    Enter the number of miles driven: 200.2
    ***The miles per gallon used for this tank is 19.4

    Enter the number of gallons used for tank #1: 5.2
    Enter the number of miles driven: 120.9
    ***The miles per gallon used for this tank is 23.3

    Your overall average miles per gallon for three tanks is 21.5

    Thanks for using my mileage calculator program!

    This is what I have written so far! Please help me! What am I doing wrong? My overall average is coming up to 26.2?????
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	/*Variable Declarations*/
    	/*----------------------*/
    
    	 int   quotient, gallused;
    	 float gallons_used;
    	 float miles_driven;
    	 float total_miles;
    	 float total_gal=0;
    
    
    
    	/*Display program info*/
       /*--------------------*/
    
    
    	 printf ("\nWelcome to my mileage calculator.\n");
    	 printf ("\nThis program will calculate the miles per gallon for you for\n");
    	 printf ("three tanks of gas after you have entered the gallons used and\n");
    	 printf ("miles driven.\n");
    
    	 /*Prompt user for information*/
    	 /*---------------------------*/
    
    	 for (gallused = 1; gallused <= 3; ++ gallused )
    	{
    		printf ("\nEnter the number of gallons used for tank #%d: ",gallused);
    		scanf  ("%f", &gallons_used);
    
    		printf ("Enter the number of miles driven: ");
    		scanf  ("%f", &miles_driven);
    
    		gallons_used = miles_driven / gallons_used;
    
    		printf ("***The miles per gallon used for this tank is %.1f \n",gallons_used);
    
    		total_gal = gallons_used + total_gal;
    		total_miles = total_miles + miles_driven;
    
    	 } // end for loop
    
    	 printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",total_miles / gallons_used);
    
    	/*Display End Comment*/
    	/*---------------*/
        
    	 printf ("\nThanks for using my mileage calculator program!\n");
    
    
    } //end main
    Last edited by paulntysmom; 03-13-2006 at 09:40 PM.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    set total_miles to 0 at the begin and your problem's solved

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    Thanks for the help I set the total_miles to 0 but I am still coming up with 26.2 instead of the required 21.5??? Any idea's why?
    Last edited by paulntysmom; 03-13-2006 at 09:40 PM.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Sorry, spoke out of line

    You want to save gallons_used rather then total_gal and total_miles

    The average at the end will be gallons_used/3.0

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    ok I am confused??? Sorry I am a newbie trying to teach myself.


    You want to save gallons_used rather then total_gal and total_miles

    The average at the end will be gallons_used/3.0


    So I would replace this:

    Code:
    printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",total_miles / gallons_used);
    with this:
    Code:
    printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",gallons_used/3.0);
    Also do I eliminate this all together
    Code:
    		total_gal = gallons_used + total_gal;
    		total_miles = total_miles + miles_driven;
    I tried doing the above but now I am coming up with 7.8 so I know I am still doing something wrong

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	/*Variable Declarations*/
    	/*----------------------*/
    
    	 int   quotient, gallused;
    	 float miles_gallons_used,gallons_used;
    	 float miles_driven;
    	 float total_miles=0;
    	 float total_gal=0;
               
    
    
    	/*Display program info*/
       /*--------------------*/
    
    
    	 printf ("\nWelcome to my mileage calculator.\n");
    	 printf ("\nThis program will calculate the miles per gallon for you for\n");
    	 printf ("three tanks of gas after you have entered the gallons used and\n");
    	 printf ("miles driven.\n");
    
    	 /*Prompt user for information*/
    	 /*---------------------------*/
    
    	 for (gallused = 1; gallused <= 3; ++ gallused )
    	{
    		printf ("\nEnter the number of gallons used for tank #%d: ",gallused);
    		scanf  ("%f", &gallons_used);
    
    		printf ("Enter the number of miles driven: ");
    		scanf  ("%f", &miles_driven);
    
    		miles_gallons_used = miles_driven / gallons_used;
    
    		printf ("***The miles per gallon used for this tank is %.1f \n",miles_gallons_used);
    
    		total_gal = gallons_used + total_gal;
    		total_miles = total_miles + miles_driven;
    
    	 } // end for loop
    
    	 printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",total_miles / 
    
    total_gal);
    
    	/*Display End Comment*/
    	/*---------------*/
        
    	 printf ("\nThanks for using my mileage calculator program!\n");
    
    
    } //end main

    that works and u get 21.5,so check out what u were doing wrong.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    Thank you so much I now understand what I was missing. I am happy to know I was almost there. It's all still a learning process for me. I am a newbie, but hope to someday become a programmer. I love this board!

  8. #8
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by paulntysmom
    Thank you so much I now understand what I was missing. I am happy to know I was almost there. It's all still a learning process for me. I am a newbie, but hope to someday become a programmer. I love this board!
    Hang in there . You're doing the right stuff. You're attacking your problem spots, you're asking for help, and you want to learn. In my personal opinion, those are three key ingredients to becoming a programmer...

    (And of course, you just have to love this stuff too )
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't figure out what is wrong with this
    By Surfndog in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2006, 07:26 AM
  2. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  3. Anything wrong with this(char to short and int)
    By hckr83 in forum C Programming
    Replies: 3
    Last Post: 12-22-2005, 06:27 PM
  4. Replies: 0
    Last Post: 11-11-2001, 01:24 PM
  5. cant figure out whats wrong with this ....
    By ii3ejoe in forum C++ Programming
    Replies: 17
    Last Post: 10-22-2001, 12:47 PM