Cole Gauvey
Cole Gauvey

Reputation: 11

Variable not declared in this scope on Eclipse IDE C++

I'm using Eclipse C++ to learn programming, and for the life of me i can't get a program to fully compile due to variables not being declared in this scope. I admit i'm horrible and new to programming but can anyone offer any assistance? For example,

/*
datatypes.cpp
Aug 25, 2014
gauvey42684
*/
#include <iostream>
using namespace std;
int main()
{
    char letter = 'a';
    short age = 10;
    int count = 575;
    long numStars = 985467528;
    float pi = 3.14;
    double price = 89.65
    string season = "summer";

    cout<<"letter"<<letter<<endl;
    cout<<"age"<<age<<endl;
    cout<<"count"<<count<<endl;
    cout<<"Number of Stars in the Sky"<<numStars<<endl;
    cout<<"pi: "<<pi<<endl;
    cout<<"price: $"<<price<<endl;
    cout<<season<<season<<endl;
return 0;
}

In this line of code, the variable season at the last cout is not declared in this scope.

As another example,

/*
C++lesson.cpp
Aug 22, 2014
gauvey42684
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// variables
    double mpg, distance, gallons, pricepergallon, totalcost

        //output using insertiuon operator
        cout<<"Enter mpg"<<endl;

        //input using extraction operator
        cin>>mpg;

        cout<<"Enter Distance in miles"<<endl;
        cin>>distance;
        cout<<"Enter Price for one gallon of gas"<<endl;
        cin>>pricepergallon;
        //calculate gallons needed
        gallons=distance/mpg;
        //calculate total cost
        totalcost=gallons * pricepergallon;
        cout<<"Total Trip Cost: $"<<fixed<<setprecision(2)<<totalcost<<endl;
return 0;
}

In this program totalcost can't be declared in this scope either. Any suggestions?

Upvotes: 1

Views: 2443

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263297

Syntax errors tend to confuse compilers.

When I compiled the above, the first message I got was about the missing semicolon. Specifically, the compiler (g++ 4.8.2) complained:

c.cpp: In function ‘int main()’:
c.cpp:16:5: error: expected ‘,’ or ‘;’ before ‘string’
     string season = "summer";
     ^
c.cpp:24:11: error: ‘season’ was not declared in this scope
     cout<<season<<season<<endl;
           ^

The first error is a syntax error, caused by the missing ; on the previous line. The compiler became so confused (more accurately, it lost the ability for its internal data structures to model your source file) that it didn't know whether to expect a comma or a semicolon. It wasn't even able to recognize the line

string season = "summer";

as a variable declaration -- which is why it reported it later as an undeclared identifier.

Something very similar happens in your second example. You forgot the semicolon on

double mpg, distance, gallons, pricepergallon, totalcost

There are two lessons here:

First, when posting a question about a compilation failure, always copy-and-paste the exact error message produced by your compiler into the question. Don't summarize what the messages say, show us. In this case, you omitted vital information.

Second, if your compiler reports a syntax error, go directly to the line reported in the message. Study that line of code and a few lines leading up to it. Look for missing punctuation, misspellings, etc. The error message for a syntax error may or may not be informative -- but it should tell you exactly where the compiler detected the error. (Having some understanding of how compilers parse source code can be helpful, but it's not really necessary.) If there are other error messages after the first syntax error, don't worry too much about understanding them; fix the syntax first and recompile.

(A syntax error message, depending on the compiler, may or may not include the word "syntax". Typically it will tell you what token or tokens the compiler expected to see at a particular point. Other errors are semantic errors, things like undeclared identifiers, applying an operator with the wrong operand type, etc.)

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

You forgot to place semicolons after statements

double price = 89.65

in the first program and

double mpg, distance, gallons, pricepergallon, totalcost

in the second program.

Also you have to include header <string> in the first program.

Also it seems in the first program there should be

cout << "season: " << season << endl;

instead of

cout<<season<<season<<endl;

Upvotes: 0

Related Questions