Results 1 to 6 of 6

Thread: Problem with 'float' data type

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484

    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

  2. #2
    Junior Member
    Join Date
    Sep 2001
    Location
    HCMC
    Posts
    21
    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!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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.

  4. #4
    Junior Member
    Join Date
    Sep 2001
    Location
    HCMC
    Posts
    21
    Actually I don't know what your problem, Can you tell more about that?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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

  6. #6
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Try changing your code to:

    PHP Code:
    #include <stdio.h> 

    int main() 

        
    int miles =0totalMiles =0gallons =0totalGallons =0
        
    float overallAverage =0temp =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 != && gallons != ) { 
                
    temp = ( float ) miles gallons
                
    printf"The miles / gallon for this tank was %.2f \n\n"temp); 
            } 
        } while( 
    gallons != -);

        
    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 3F3.0F;
    printf("X/Y = %f\n", (float) );
    // Might not output 1 because of rounding errors 
    -MoMad
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width