i need help with my c programm...Plzzzz

This is a discussion on i need help with my c programm...Plzzzz within the C Programming forums, part of the General Programming Boards category; Hi folks i have a lil problem coz of my c progie....... Hears the question its from C How to ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Unhappy i need help with my c programm...Plzzzz

    Hi folks i have a lil problem coz of my c progie.......

    Hears the question its from C How to programmm 3/e from Deitel & Deitel

    3.17 ) Develop a program that will input the miles driven and gallons used for each tankful.The program should calculate and display the miles per gallon obtained for each tankful.After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.


    Below is the output screen :_ ---
    |



    Enter the gallons used ( -1 to end ): 12.8
    Enter the miles driven: 287
    The miles/ gallon for this tank was 22.421875

    Enter the gallons used ( -1 to end ): 10.3
    Enter the miles driven: 200
    The miles/ gallon for this tank was 19.417475

    Enter the gallons used ( -1 to end ): 5
    Enter the miles driven: 120 The miles/ gallon for this tank was 24.000000

    Enter the gallons used ( -1 to end ): -1

    The overall average miles/gallon was 21.601423
    |

    And below is the program that i made and my luck it dint work the way it should:-

    #include <stdio.h>

    int main()
    {
    int miles, cnt =0;
    float avg, mg= 0, gall;


    while ( gall != -1 ) {
    printf ("\nEnter the gallons used (-1 to end): ");
    scanf ("%f", &gall);
    if ( gall == -1 )
    break;
    printf ("\nEnter the miles driven: ");
    scanf ("%d", &miles );
    mg =( float )miles/gall;
    printf ("The miles / gallon for this tank was %f\n", mg );
    cnt = cnt + 1;
    }

    if ( gall == -1 ){
    avg = (float)mg/cnt;
    printf ("The overall average miles / gallon was %f", avg ); }


    return 0;
    }

    Thanks for reading my boring ?question?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First off, if you're going to post code, please use code tags in future.


    >...it dint work the way it should
    OK, so it didn't do what you wanted. So tell us what it is or isn't doing to your requirements, my crystal ball is a bit blury this time of night!

    So, anyway, I run it and I'm going to guess you're complaining that it isn't totaling correctly. This is probably to do with the fact you are not storing the mg value, you just overwrite it each loop. Look here:

    >avg = (float) mg / cnt;
    cnt tells us how many times we did the loop, but mg holds the data from only the last iteration.

    I'll leave the coding for you to work out......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HexDump Programm....
    By Kdar in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 05:13 AM
  2. programm will not start
    By keeper in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 06:02 AM
  3. printing with a c programm
    By -11Reaper11- in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2006, 10:31 AM
  4. windowsAPI programm
    By datainjector in forum Windows Programming
    Replies: 8
    Last Post: 03-11-2003, 10:08 PM
  5. I am looking for a programm!
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 11:51 AM

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