Help required from generous & patient souls!

This is a discussion on Help required from generous & patient souls! within the C Programming forums, part of the General Programming Boards category; Hello all. I have coded this program to help calculate the cost of running a car once all individual costs ...

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

    Help required from generous & patient souls!

    Hello all.

    I have coded this program to help calculate the cost of running a car once all individual costs are input. Having found the total it then works out the cost per KM.

    This is the program as it stands...

    #include <stdio.h>
    #include <float.h>

    int option;
    float litre_rate;
    float fuel_rate;

    float kilo;
    float pet_kilo;
    float total;

    void menu();
    void exit();
    void program();

    void program()
    {


    printf("\nPlease input the price of a litre of petrol in pounds: ");
    scanf("%f",&litre_rate);
    printf("\n %f",litre_rate);

    printf("\nPlease input the number of litres of fuel used per kilometre: ");
    scanf("%f",&fuel_rate);
    printf("\n %f",fuel_rate);

    printf("\nPlease input the number of kilometres a week you travel: ");
    scanf("%f",&kilo);
    printf("\n %f",kilo);

    pet_kilo=litre_rate*fuel_rate;
    total=kilo*pet_kilo;

    if (total<0)
    {
    printf("\nYou appear to have made an error in your calculation as the result returned is less than 0.");
    printf("\nTotal =");
    printf("%f",total);
    printf("\n");
    menu();

    }
    else
    {
    printf("\nIt will cost you: ");
    printf("%f",total);
    printf(" pounds a week.");
    printf("\n");
    menu();
    }

    }

    void menu()
    {
    printf("----------Car cost calculation program---------\n");
    printf("\nPlease select an option:");
    printf("\n1 - Run program");
    printf("\n2 - Exit");
    printf("\n");
    scanf("%d",&option);

    if (option<2)
    {

    program();
    }
    if (option>1)
    {
    exit();
    }

    }

    void exit()
    {

    }


    int main(void)
    {
    menu();
    }


    The trick is that I need to modify it so that it shows the difference in cost per KM by increasing or decreasing the cost of the fuel by a certain percentage.

    I know this is a tall order but can anyone assist in modifying it? I normally wouldnt ask...but I think my brains have gone to sleep :P

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    For a start I would not let the user input invalid info, ie -ve Km or price. Stop them at that point and make them enter a valid value.

    ie
    Input=0;//prime loop
    do
    {
    //print question (including valid range ie $0.01 -> ????)
    //get input
    }while(Input<=0)

    to print out the differing values. Ask the user for a percentage when asking for the other input. Write the three (low, normal and high) instead of just the normal one. (I would use an array of a structure for the values)

    Percent_Diff=Input_Percent / 100;//if the user is asked for a number between 1 and 100.

    pet_kilo_low=(litre_rate * (1 - Percent_Diff) ) * fuel_rate;
    pet_kilo_norm=litre_rate * fuel_rate;
    pet_kilo_high=(litre_rate * (1 + Percent_Diff) ) * fuel_rate;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  4. Writing to a Binary File
    By Lah in forum C Programming
    Replies: 1
    Last Post: 10-27-2003, 12:41 AM
  5. Student desperately needing help!
    By pinkpanther1586 in forum C Programming
    Replies: 4
    Last Post: 10-26-2003, 07:46 AM

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