-
guidence to a program C
I need guidence to solve a problem i'm having with a program (Exercise), When I enter -1 why does the "Enter miles used" show up. It should just print " The overall average miles / gallon was" and if no data is entered print "No Data entered"
PLEASE I dont want the answer just direction so I can learn for meself.
Code:
#include <stdio.h>
/* fuction main begins program execution*/
int main(void)
{
/* intialization phase */
int counter = 0; /* number of tanful entered/ intialize loop counter */
int miles = 0; /* miles value */
float total = 0; /* sum of tankful/ intialize total */
float gallons; /* gallons value */
float tankful; /* number with decimal point for average */
float average;
/* process phase */
/* get gallons used from user */
printf("Enter gallons used, -1 to Exit: "); /* prompt for input */
scanf("%f", &gallons); /* read gallons from user */
printf(" Enter miles used: "); /* prompt for input */
scanf("%d", &miles); /* read miles from user*/
tankful = miles / gallons; /* calculate average */
printf("The miles / gallons for this tank was %f", tankful);
/* loop while sentinel value not yet read from user */
while(gallons != -1) {
total = total + tankful; /* add average to total */
counter = counter + 1; /* increment counter */
/* get next gallonful from user */
printf("\nEnter gallons used, -1 to Exit: "); /* prompt for input */
scanf("%f", &gallons); /* read gallons from user */
printf(" Enter miles used: "); /* prompt for input */
scanf("%d", &miles); /* read miles from user */
tankful = miles / gallons;
printf("The mile / gallons for this tank was %f", tankful);
} /* end while */
/* termination phase */
/* if user entered at least one tankful*/
if(counter != 0) {
/* calculate average of all the tankfuls entered*/
average = (float) total / counter; /* avoid truncation*/
/* display average with six digits of precision*/
printf("The overall average miles / gallon was %f\n", average);
} /* end if */
else { /* if no tankfuls were entered, output message*/
printf("\nNo data entered\n");
} /* end else */
return (0); /* indicate program ended ok */
} /* end fuction main */
Thank for your time
Nurofen
-
because the program goes to the next line of code.
You should start the while statement just after the first scanf.
that should work
-
When I do that the output is:
"Enter gallons used, -1 to Exit: " 12.8
and the it repeats to
"Enter gallons used, -1 to Exit: "
it should be
"Enter gallons used, -1 to Exit: " 12.8
" Enter miles used: " 287
"The miles / gallons for this tank was " 22.421875
"Enter gallons used, -1 to Exit: "
Thanks for your time
-
sorry people but I've tried but with no joy.
Can someone show me what i'm doing wrong please.
Thanking you in advance
Nurofen
-
You need to put everything that you want to repeat in the while loop -- so the while loop should start after reading the gallons used and go through to the next reading in of the gallons used.
-
Code:
printf("Enter gallons used, -1 to Exit: ");
scanf("%f", &gallons);
while(gallons != -1) {
total = total + tankful;
counter = counter + 1;
printf("\nEnter gallons used, -1 to Exit: ");
scanf("%f", &gallons);
printf(" Enter miles used: ");
scanf("%d", &miles);
tankful = miles / gallons;
printf("The mile / gallons for this tank was %f", tankful);
}
when i do this the output is:
"Enter gallons used, -1 to Exit: " 12.8
and the it repeats to
"Enter gallons used, -1 to Exit: "
it should be
"Enter gallons used, -1 to Exit: " 12.8
" Enter miles used: " 287
"The miles / gallons for this tank was " 22.421875
"Enter gallons used, -1 to Exit: "
Nurofen
-
You don't start the while loop with the prompt for information, you just did the prompt for information. You need to end the while loop with the prompt for information (so that the new data is ready to check when you get back to the top). So move the printf and scanf from the top of the loop to the bottom.
-
gallons != -1
not very good idea for float values
-
Well, -1 can be represented exactly in a float; so I guess it's up to the user, when it says "-1 to exit" to not type -1.153.
-
-
I still can't get it to work, what i'm I doing wrong?
-
try using '\n' in all ur printf stts.
EDIT:Also the o/p u have mentioned is what is to be expected as you are asking for
gallons twice(once before the while and once within it).
-
I tried \n in printf stts no joy.
I see I have changed the part (thank you )
when i enter -1 why does it as for miles used?
i would like it to go
Code:
printf("\nThe overall average miles / gallon was %f\n", average);
-
try changing your code to:
Code:
printf("Enter gallons used, -1 to Exit: ");
scanf("%f", &gallons);
while(gallons != -1) {
total = total + tankful;
counter = counter + 1;
printf(" Enter miles used: ");
scanf("%d", &miles);
tankful = miles / gallons;
printf("The mile / gallons for this tank was %f", tankful);
printf("\nEnter gallons used, -1 to Exit: ");
scanf("%f", &gallons);
}
-
Thank you so much it works...
Can you please explain why it works that why if you wouldn't mind and not all the othe ways
Thank you again
stevesmithx