Expected Primary Expression Before Float

Nov 14, 2013 at 2:45pm
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

float mileage(float startMiles, float endMiles, float gallonsUsed);

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 = mileage(float startMiles, float endMiles, float gallonsUsed);

    // output collected data
    cout << "for a trip with:" << endl;
    cout << "     " << gallonsUsed << "  gallons of gas used" << endl;
    cout << "    and a starting mileage of " << startMiles << endl;
    cout << "     and an ending mileage of " << endMiles << endl;
    cout << "     the mileage per gallon is " << mpg << endl;



    return 0;
}
Nov 14, 2013 at 2:46pm
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.
Nov 14, 2013 at 2:49pm
Well, are you supposed to call the function in int main()? or before it?
Nov 14, 2013 at 3:00pm
What do you mean, "before it"? You can't call anything before main - it's the entry point to your program!
Nov 14, 2013 at 3:25pm
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.
Nov 14, 2013 at 3:28pm
I suspect what you actually saw was a function declaration. That's not a call.

But yes, I'd strongly advise going back to your textbook to be absolutely clear in your mind what the difference between the two things is.
Nov 14, 2013 at 3:28pm
ok. thanks for the help
Nov 14, 2013 at 4:13pm
I just adjusted the code to how I am used to programming. Is this still sufficient? It runs and outputs the desired results.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <iomanip>
using namespace 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.
Last edited on Nov 14, 2013 at 4:17pm
Nov 14, 2013 at 4:35pm
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.
Last edited on Nov 14, 2013 at 4:36pm
Topic archived. No new replies allowed.