Z,
I think this works ...
//-------------------------------------------------------------------------
#include <stdio.h>
/*Main program body*/
void main(void)
{
float gallons, miles, mpg;
int counter;
for (counter=1; counter <=3; 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 \n", mpg);
}
printf("\n\r");
printf("Enter any number to exit ...");
scanf("%f", &miles);
}
//-------------------------------------------------------------------------
The big problem in the version you submitted is that mpg cannot be
calculated correctly
until BOTH gallons AND miles have been initialized. After gallons and miles
have been
entered, you want to calculate mpg and print the result and then repeat the
same process
two more times with different values for gallons or miles.
You don't need the "if/then" to accomplish this pattern.
Good luck with "c". It can be a very useful thing to know if you have a
decent IDE and
compiler. I used this:
http://www.bloodshed.net/devcpp.html
_jim
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Zulfiqar Naushad
Sent: Sunday, February 26, 2006 1:33 AM
To: The Hardware List
Subject: [H] C programming question
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