Thread: Trying to write a program that will display a fuel economy conversion table

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Trying to write a program that will display a fuel economy conversion table

    So we have to write a C Program that will display a fuel economy conversion table as follows:

    Litres Miles
    per per
    100 KM Gallon
    4.0 70.6
    4.1 68.9
    4.2 67.3
    4.3 65.7
    4.4 64.2
    4.5 62.8
    4.6 61.4
    4.7 60.1
    4.8 58.9
    4.9 57.6
    5.0 56.5
    5.1 55.4
    5.2 54.3
    5.3 53.3
    5.4 52.3
    5.5 51.4
    5.6 50.4
    5.7 49.6
    5.8 48.7
    5.9 47.9
    6.0 47.1
    6.1 46.3
    6.2 45.6
    6.3 44.8
    6.4 44.1
    6.5 43.5
    6.6 42.8
    6.7 42.2
    6.8 41.5
    6.9 40.9
    7.0 40.4
    7.1 39.8
    7.2 39.2
    7.3 38.7
    7.4 38.2
    7.5 37.7
    7.6 37.2
    7.7 36.7
    7.8 36.2
    7.9 35.8
    8.0 35.3



    And this is my code that will NOT compile in any IDE that I use:

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    float imoerialGallonperlitres (float litres) {
    
    
        float gallon;
        gallon=litres*0.219969;
        return gallon;
    }
    float MilesperImperialGallon (float gallon) {
    
    
        float miles;
        miles=100/gallon*0.621371;
        return miles;
    }
    int main(void) {
    
    
        float litres=4.0;
        float miles;
        float imperialgallon;
        printf("Litres Miles\n");
        printf("per    per\n");
        printf("100 KM Gallon\n");
        printf("");
        printf("");
        printf("");
    
    
        for(int i=0; i<41;i++){
        imperialgallon=imoerialGallonperlitres (litres) ;
        miles=MilesperImperialGallon (imperialgallon) ;
        printf("%.1f  %.1f\n", litres, miles);
        litres=litres+0.1;
        }
        return EXIT_SUCCESS;
    }


    Could someone please tell me what I'm doing wrong? My friends can't seem to figure it out either. I have an assignmentdue tonight and this is getting very frustrating.



  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    /*
    main.c|33|error: 'for' loop initial declarations are only allowed in C99 mode
    */
    Code:
    for(int i=0; i<41;i++)
    Are you compiling with respect to C99?

    If not, then declare 'i' outside of the "for()" loop.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your code is correct (in C99 and above). What for compilers are using your IDE's and on which OS?
    I have compiled with GCC on Debian/GNU Linux. Here is the output:
    Code:
    $ gcc -Wall -o foo foo.c 
    foo.c: In function ‘main’:
    foo.c:28:2: warning: zero-length gnu_printf format string [-Wformat-zero-length]
    foo.c:29:2: warning: zero-length gnu_printf format string [-Wformat-zero-length]
    foo.c:30:2: warning: zero-length gnu_printf format string [-Wformat-zero-length]
    foo.c:32:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
    foo.c:32:2: note: use option -std=c99 or -std=gnu99 to compile your code
    The 3 warnings (line 28-30) are from printf-function with empty string.
    The error came from declaration of variable 'i' inside the for-statment.
    This is allowed since C99. I think you work on Windows.

    I can compile the code without problems like …

    Code:
    gcc -std=c99 -Wall -o foo foo.c
    gcc -std=c11 -Wall -o foo foo.c
    … and it works as expected.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    You're right! I tried both on the school linux computers and on my mac and it didn't work. I declared "i" outside the for statement and it's now working on both. Thanks a bunch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2013, 07:15 PM
  2. Replies: 13
    Last Post: 05-02-2012, 04:17 PM
  3. Having trouble with temperature conversion table
    By svisger in forum C Programming
    Replies: 2
    Last Post: 03-08-2011, 08:41 PM
  4. C fuel program
    By BarryKamp in forum C Programming
    Replies: 4
    Last Post: 05-29-2007, 10:56 AM
  5. Replies: 0
    Last Post: 03-15-2002, 02:07 PM

Tags for this Thread