Hey Z, been a bit since I've done C, so be gentle if this is wrong as I
did not try running it...
You may want to consider second example using do..while gallons > 0 loop
as the better method when n is not known.
void main(void)
{
float gallons, miles, mpg;
int n, counter;
/* To calculate MPG for each of n tanks */
n=3
for (counter=1; counter <= n; counter++)
{
printf("\nEnter the number of gallons used for tank#%i: ", counter);
scanf("%f", &gallons);
printf("Enter the number of miles driven: ");
scanf("%f", &miles);
mpg = miles / gallons;
printf("*** The miles per gallon for this tank is %f", mpg);
}
}
void main(void)
float gallons, miles, mpg;
int n, counter;
/* To calculate MPG while gallons > 0 */
do
{
printf("\nEnter the number of gallons used for tank#%i (0 to end):
", counter);
scanf("%f", &gallons);
If(gallons > 0)
{
printf("Enter the number of miles driven: ");
scanf("%f", &miles);
mpg = miles / gallons;
printf("*** The miles per gallon for this tank is %f", mpg);
}
}
while(gallons > 0);
}
Zulfiqar Naushad wrote:
I have some basic question regarding some C program
that I have made just for fun (I am trying to teach
myself C)
Please have a look at the program below:
If you compile it then the error will be self
apparent, the first time it calculates the MPG it
returns zero, the second mpg calculation is actually
for the first one and so on :).
#include <stdio.h>
/*Main program body*/
void main(void)
{
float gallons, miles, mpg;
int counter;
for (counter=1; counter <=3; counter++)
{
mpg = miles / gallons;
if (counter == 1)
printf("Enter the number
of gallons used for tank#%i: ", counter);
else
printf("\nEnter the number
of gallons used for tank#%i: ", counter);
scanf("%f", &gallons);
printf("Enter the number of miles
driven: ");
scanf("%f", &miles);
printf("*** The miles per gallon
for this tank is %f", mpg);
}
}
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com