Thread: GCC Compile Error

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    GCC Compile Error

    I wrote the following program in Visual Studio 2010 Express and its execution resulted in no errors or warnings.

    I am now receiving the following GCC compile error after attempting to compile it in a command-line linux environment:

    gcc -o mpg.o -c mpg.c

    "error: expected â;â, â,â or â)â before â&â token"

    The above error is being generated by lines 43 and 56 of my code (mileage_calculator and gas_calculator function definitions).

    This seems more like a compiler specific issue to me. Would I be able to use some differenct operators to get this to compile? If the issue is not compiler specific and I've written bad code, I would rather struggle and figure it out on my own. I hope this makes sense.


    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    double l_mpg = 0;
    double u_mpg = 0;
    double d_mpg =0;
    double i_gph = 0;
    
    double distance = 0;
    double min = 0;
    double miles = 0;
    double gas = 0;
    char terrain = ' ';
    
    char get_terrain()
    {
        scanf(" %c", &terrain);
        terrain = toupper(terrain);
    
        if(terrain != 'L' && terrain != 'U' && terrain != 'D' && terrain != 'I' && terrain != 'E')
        {
            printf("Bad Input.\n");
            exit(0);
        }
        else
            return terrain;
    }
    
    double get_distance()
    {
        scanf(" %lf", &distance);
    
        if(terrain == 'I')
        {
            min = distance;
            distance = 0;
        }
    
        return distance;
    }
    
    double mileage_calculator(double &d)
    {
        miles = miles + d;
    
        if(miles <= 0)
        {
            printf("Bad Input.\n");
            exit(0);
        }
        else
            return miles;
    }
    
    double gas_calculator(char &t, double &d)
    {
        switch(t)
        {
            case 'L':
                gas = gas + (d / l_mpg);
                break;
            case 'U':
                gas = gas + (d / u_mpg);
                break;
            case 'D':
                gas = gas + (d / d_mpg);
            case 'I':
                gas = gas + (min / 60) * i_gph;
                break;
            default:
                break;
        }
    
        return gas;
    }
    
    int main()
    {
        printf("Enter Fuel Economy: ");
    
        scanf(" %lf %lf %lf %lf", &l_mpg, &u_mpg, &d_mpg, &i_gph);
        
        if(l_mpg < 0 || u_mpg < 0 || d_mpg < 0 || i_gph < 0)
        {
            printf("Bad Input.\n");
            exit(0);
        }
    
        printf("Enter Terrain & Distance: ");
    
        get_terrain();
    
        while(terrain != 'E')
        {
            get_distance();
            
            mileage_calculator(distance);
    
            gas_calculator(terrain, distance);
    
            get_terrain();
        }
    
        printf("Miles=%8.2lf\tGallons=%8.2lf\tMPG=%8.2lf\n", miles, gas, miles/gas);
    
        return 0;
    }
    I am an inexperienced programmer in the learning stages. I am stuck after attempting to research a solution. Your assistance is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    This program is C++, not C. You can tell based on the line

    Code:
    double mileage_calculator(double &d)
    To compile the program with GCC, use the command g++ instead of gcc.
    Or even better, rewrite the program as valid C (assuming you're trying to learn C). In C, pointers are used rather than reference variables.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If not doing C++;just change this
    Code:
    double mileage_calculator(double &d)
    to this
    Code:
    double mileage_calculator(double d)
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  3. 0 compile error but 2 build error. need help please!
    By jibbles in forum C Programming
    Replies: 5
    Last Post: 08-30-2004, 04:30 PM
  4. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM
  5. error C2061 - Compile Error
    By GaGi in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2002, 11:50 PM