Need help please :) Newbie trying to learn

This is a discussion on Need help please :) Newbie trying to learn within the C Programming forums, part of the General Programming Boards category; This is how I need the output of the program to look: Welcome to My Mileage calculator This program will ...

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

    Smile Need help please :) Newbie trying to learn

    This is how I need the output of the program to look:

    Welcome to My Mileage calculator

    This program will calculate the miles per gallon for you for three tanks of gas after you have entered the gallons used and the 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 for this tank is 22.4

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

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

    Your overall average miles per gallon for three tanks is 21.5

    Thank you for using My mileage calculator program

    This is what I have written so far. My question is how do I get it to calculate overall average miles per gallon for the all three tanks and also how do I get it to ask for tank 1, tank 2 , tank 3? Mine just says tank 1 on each one.

    Code:
    #include <stdio.h>
    
    void main (void)
    {
    	/*Variable Delclarations*/
    	/*----------------------*/
    	 int   quotient, gallused, milesdriven;
    	 float gallons_used;
    	 float miles_driven;
    	 float avg;
    
    	/*Display program info*/
       /*--------------------*/
    
    
    	 printf ("\nWelcome to Colleen's 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 #1: ");
    	 scanf  ("%f", &gallons_used);
        fflush (stdin);
    
    
    	 printf ("Enter the number of miles driven: ");
    	 scanf  ("%f", &miles_driven);
    	 fflush (stdin);
    
    	 gallons_used = miles_driven / gallons_used;
    
    	 printf ("***The miles per gallon used for this tank is %.1f \n",gallons_used);
    	 fflush (stdin);
    
    	 } // end of loop
    
    	 gallons_used = avg / gallons_used;
    
    	 printf ("\nYour overall average miles per gallon for three tanks is %.1f \n", avg);
    	 fflush (stdin);
    
    
    
    	/*Display Results*/
    	/*---------------*/
    
    
    	 printf ("\nThanks for using Colleen's Mileage calculator program!\n");
    
    
    } //end main
    Last edited by paulntysmom; 03-07-2006 at 12:14 PM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    printf ("\nEnter the number of gallons used for tank #1: ");
    scanf  ("%f", &gallons_used);
    that is inside the for loop, so everytime the loop executes it asks the same thing, tank 1. you need 3 seperate printf prompts.
    Code:
    printf ("\nEnter the number of gallons used for tank #1: ");
    //calculation
    printf ("\nEnter the number of gallons used for tank #2: ");
    calculation
    printf ("\nEnter the number of gallons used for tank #3: ");
    calculation
    same thing for miles driven.
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    I am confused on where I would put the three printf statements to do that? sorry I am really new to programming and trying to teach myself.

    Also how do I calculate the overalls miles per gallon for all three?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    remove the for loop.
    When no one helps you out. Call google();

  5. #5
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Just a couple of other pointers as well.

    In Standard C, main is declared as returning an int:

    You have:
    Code:
    void main(void)
    and it should be:
    Code:
    int main(void)
    Also, fflush(stdin) is an undefined behavior that can give wildly unpredictable results on different platforms. It should be avoided.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by paulntysmom
    Also how do I calculate the overalls miles per gallon for all three?
    first you need to find the total miles for all three and total gallons for all three, then average them. you are using avg the wrong way on your code, and avg is not defined.
    When no one helps you out. Call google();

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    THANKS FOR HELPING ME, I APPRECIATE IT VERY MUCH!

    This is what I have so far, but still confused on calculating all three for the overall average. I can't figure out for the life of me on how to get that to work. I will keep trying though!

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	/*Variable Delclarations*/
    	/*----------------------*/
    	 int   quotient, gallused, milesdriven;
    	 float gallons_used;
    	 float miles_driven;
    	 float avg;
    
    	/*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*/
    	 /*------------------*/
    
    
    	 printf ("\nEnter the number of gallons used for tank #1: ");
    	 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);
    
    	 printf ("\nEnter the number of gallons used for tank #2: ");
    	 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);
    
    	 printf ("\nEnter the number of gallons used for tank #3: ");
    	 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);
    
    	 gallons_used = gallons_used / avg;
    
    	 printf ("\nYour overall average miles per gallon for three tanks is %.1f \n", avg);
    
    
    
    	/*Output final greeting*/
    	/*---------------*/
    
    
    	 printf ("\nThanks for using My Mileage calculator program!\n");
    
    
    } //end main
    Last edited by paulntysmom; 03-07-2006 at 12:19 PM.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    I just got your pm and I think I know what I am doing wrong now I will work on it and let you know

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    use some comments to keep track of your code.
    create an avg function and then you can just call it.

    and you are using avg without it being initalized.
    When no one helps you out. Call google();

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    so...ummm....there was absolultly no problem with the for loop...it just had to be done differently in order to change the number each time.
    Code:
    printf ("\nEnter the number of gallons used for tank #%d: ",gallused);
    And then the very last statement of the for loop should be something along these lines
    Code:
    avg = avg+gallons_used;
    or
    Code:
    avg += gallons_used;
    which is actually the same thing but thats another lesson.

    Then, just before you print the overall average, you need to divide the avg by three.
    Also, don't forget that you need to initiate avg as 0.0 at the beginning of the program.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    33
    Thank you crazedbrit I finally got it to work I appreciate everyones help

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Also, fflush(stdin) is an undefined behavior that can give wildly unpredictable results on different platforms. It should be avoided.
    See the FAQ:
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you ever try to learn too much?
    By Stonehambey in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 06-17-2008, 07:55 AM
  2. Can you actually learn c++ in 21 days?
    By Raeliean in forum C++ Programming
    Replies: 14
    Last Post: 07-27-2005, 03:41 PM
  3. You have to learn C in order to learn C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-16-2004, 10:33 AM
  4. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 08:25 PM
  5. Trying to learn guitar
    By Ben_Robotics in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-10-2003, 03:15 AM

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