# Help required from generous & patient souls!

Printable View

• 06-30-2002
Kremlin
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
• 06-30-2002
novacain
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;