Ally
Ally

Reputation: 1

Why am I getting "Error: expected primary-expression before '<<' token."

I keep getting an error and I don't know what I'm missing. This is only my first programming class, so it could be something really simple that I'm not seeing. Help? :)

#include <iostream> 
using namespace std; 

const float METERS_PER_INCH = 39.3700787;
const float METERS_PER_FOOT = 3.2808399;
const float METERS_PER_YARD = 1.09361; 

int main ()
{
    int yards;
    int feet;
    int inches; 
    int totalMeters;

    cout << "Enter a length in meters: "; 
    cin >> totalMeters; 
    cout << endl;

    cout << "The total length is " << endl; 
    << totalMeters * METERS_PER_YARD << "yards;"
    << yards * METERS_PER_FOOT << "feet;" 
    << feet * METERS_PER_INCH << "inches;" 
    << endl; 

    return 0;
}

Upvotes: 0

Views: 1242

Answers (1)

Eli Bendersky
Eli Bendersky

Reputation: 273376

Change your last cout to:

cout << "The total length is " << endl
<<  totalMeters * METERS_PER_YARD << "yards;"
<< yards * METERS_PER_FOOT << "feet;" 
<< feet * METERS_PER_INCH << "inches;" 
<< endl; 

The difference is the ; removed after the first endl.

With the semicolon there, the first line of the cout is a statement on its own, so the compiler looks for a new statement on the next line. Statements cannot just start with << because it's a binary operator - it expects an expression on its left-hand-side as well.

Upvotes: 2

Related Questions