-
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.
-
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.
-
If not doing C++;just change this
Code:
double mileage_calculator(double &d)
to this
Code:
double mileage_calculator(double d)