|
-
Dec 26th, 2001, 02:24 AM
#1
Thread Starter
Hyperactive Member
Problem with 'float' data type
Hi,
I am learning C and i had to write a program for an exercise. However i tried many ways but still couldn't get the result to be correct. I found out the it's a problem with float numbers. Here is the code i wrote, PLS help me to get it to work, it's probably only a small silly mistake of mine but i just can't get it out.
thnx in advance
--------------------------------------------------------------------------------
/* Calculate miles / gallon obtained for each tank. */
#include <stdio.h>
int main()
{
int miles = 0, totalMiles, gallons = 0, totalGallons;
float overallAverage, temp;
printf( "Enter the gallons used (-1 to end): " );
scanf( "%d", &gallons);
printf( "Enter the miles driven: " );
scanf( "%d", &miles );
while ( gallons != -1) {
totalMiles += miles;
totalGallons += gallons;
if ( miles != 0 )
if ( gallons != 0 ) {
temp = ( float ) miles / gallons;
printf( "The miles / gallon for this tank was %.2f \n\n", temp);
}
printf( "Enter the gallons used (-1 to end): " );
scanf( "%d", &gallons);
if ( gallons != -1 ) {
printf( "Enter the miles driven: " );
scanf( "%d", &miles);
}
}
overallAverage = ( float ) totalMiles / totalGallons;
printf( "\nTHe overall average miles/gallon was %.2f", overallAverage );
return 0;
}
------------------------------------------------------------------------------------
I've also uploaded the output (incorrect) of this program.
thanx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|