I am running into errors when I am compiling code in CodeBlocks, and I am not sure what I am doing wrong. I am creating this simple program for a class, but it is pulling up an error saying on line 27 (I have bolded it in the code): "error: expected primary-expression before 'float' for all 3 variables. Someone help me with this? I'm completely lost.
That's not the correct syntax for calling a function. I strongly recommend you go back to your textbook and learn how to call a function properly, because it's absolutely fundamental to being able to program.
I guess I'm very confused then because in my instructor's sample code, "float mileage" was called before main. I'm going to have to read this textbook again.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
float startMiles;
float endMiles;
float gallonsUsed;
float mpg;
// floating point output format
cout << fixed << showpoint << setprecision(2);
// collect data
cout << "Enter starting mileage: " << endl;
cin >> startMiles;
cout << "Enter ending mileage: " << endl;
cin >> endMiles;
cout << "Enter number of gallons of gas used: " << endl;
cin >> gallonsUsed;
// calculate mileage
mpg = (endMiles - startMiles)/gallonsUsed;
// output collected data
cout << "for a trip with:" << endl;
cout << " " << gallonsUsed << " gallons of gas used" << endl;
cout << "starting mileage of: " << startMiles << endl;
cout << "ending mileage of: " << endMiles << endl;
cout << "the mileage per gallon is: " << mpg << endl;
return 0;
}
I just didn't understand what he was doing by declaring a variable like that up above main. I couldn't see what he wanted to do with it, so I just removed the mileage variable and set the mpg to what mileage would have been.
Well, it looks like it'll work fine, although it no longer calls a function to do the calculation. If the purpose of the exercise was to learn about functions, then it's probably not much use now ;)
I just didn't understand what he was doing by declaring a variable like that up above main. I couldn't see what he wanted to do with it, so I just removed the mileage variable and set the mpg to what mileage would have been.
mileage isn't a variable - it's a function. That bit above main is declaring a function called mileage, and declaring which arguments it takes and what it returns.