|
-
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
-
Dec 26th, 2001, 05:17 AM
#2
Junior Member
You need to add 2 more lines to your program before While stement. like this:
.
.
.
totalMiles =0;
totalGallons =0;
while ( gallons != -1){
.
.
.
Or you can init that 2 variables when you declare them.
Let free now!
-
Dec 26th, 2001, 06:38 AM
#3
Thread Starter
Hyperactive Member
actually, it was my mistake, my stupid compiler doesn't support the float data type, i switched to working with borland c compiler. BUt now i had another problem with loops , it's posted just above this thread.
-
Dec 26th, 2001, 08:19 AM
#4
Junior Member
Actually I don't know what your problem, Can you tell more about that?
-
Dec 26th, 2001, 08:43 AM
#5
Thread Starter
Hyperactive Member
i just wanna konw what's wrong with that piece of code and how to get it to work without being trapped in the loop.
thnx
-
Dec 30th, 2001, 08:22 PM
#6
Fanatic Member
Try changing your code to:
PHP Code:
#include <stdio.h>
int main()
{
int miles =0, totalMiles =0, gallons =0, totalGallons =0;
float overallAverage =0, temp =0;
do {
printf( "Enter the gallons used (-1 to end): " );
scanf( "%d", &gallons);
printf( "Enter the miles driven: " );
scanf( "%d", &miles );
totalMiles += miles;
totalGallons += gallons;
if ( miles != 0 && gallons != 0 ) {
temp = ( float ) miles / gallons;
printf( "The miles / gallon for this tank was %.2f \n\n", temp);
}
} while( gallons != -1 );
overallAverage = ( float ) totalMiles / totalGallons;
printf( "\nTHe overall average miles/gallon was %.2f", overallAverage );
return 0;
}
Floats are Standard C/C++ datatypes so I dont see why a compiler will not support them. Thats a bunch of crap I tell you. Anyways, the only float bug I know of is:
PHP Code:
float x = 3F, y = 3.0F;
printf("X/Y = %f\n", (float) x / y );
// Might not output 1 because of rounding errors
-MoMad
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
|