Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System not declared in scope?

I know its simple code, How do I fix "System not declared in scope" problem?

#include<iostream>
using namespace std;

int main(void)
{
    system ( "TITLE Calculator" );
    system ( "COLOR 2" );
    char cChar;
    double dfirstnumber;
    double dsecondnumber;
    char cDoagain;

    do
    {
        system("CLS");
        cout << "Please enter the first number you would like to use."<< endl;
        cin >> dfirstnumber;
        cout<< "Please enter the operation you would like to perform." << " (+,-,*,or /)" << endl;
        cin >> cChar;
        cout<< "Please enter the second number you would like to use." << endl;
        cin >> dsecondnumber;

        switch (cChar)
        {
            case '+':
                cout << "The answer is: " << dfirstnumber << "+" << dsecondnumber << "=" <<
                (dfirstnumber + dsecondnumber) << endl;
                break;
            case '-':
                cout << "The answer is: " << dfirstnumber << "-" << dsecondnumber << "=" <<
                (dfirstnumber - dsecondnumber) << endl;
                break;
            case '*':
                cout << "The answer is: " << dfirstnumber << "*" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'x':
                cout << "The answer is: " << dfirstnumber << "x" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'X':
                cout << "The answer is: " << dfirstnumber << "X" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case '/':
                if(dsecondnumber == 0){
                cout<< "That is an invalid operation." << endl;}
                else{
                cout << "The answer is: " << dfirstnumber << "/" << dsecondnumber << "=" <<
                (dfirstnumber / dsecondnumber) << endl;

        }
                break;
                default:
                    cout << "That is an invalid operation." << endl;
                    break;
    }
                cout << "Would you like to start again? (Y/N)" << endl;
                cin >>  cDoagain;
    }while (cDoagain == 'Y' or cDoagain == 'y');
    system("PAUSE");
    return 0;
}

Heres my end message:

C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp||In function 'int main()':| C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp|8|error: 'system' was not declared in this scope||

|=== Build finished: 1 errors, 0 warnings ===|

like image 585
Nilo Cortex Avatar asked Oct 07 '10 21:10

Nilo Cortex


People also ask

How do I fix not declared in this scope?

To resolve this error, a first method that is helpful would be declaring the function prototype before the main() method. So, we have used the function prototype before the main method in the updated code. When we have compiled the code, it throws no exceptions and runs properly.

What do you mean by not declared in this scope?

As from the name we can understand that when the compiler of Arduino IDE is unable to recognize any variable or is unable to process any loop or any instruction having any undeclared variable so it gives the error “not declared in this scope”, which means that code is unable to understand the instruction given in the ...

What is the meaning of printf was not declared in this scope?

Printf() is defined in the stdio. h and implemented in the libc library. Older versions of C did not require a declaration before calling a function.


2 Answers

You need to add:

 #include <cstdlib>

in order for the compiler to see the prototype for system().

like image 81
Paul R Avatar answered Sep 22 '22 13:09

Paul R


Chances are that you've not included the header file that declares system().

In order to be able to compile C++ code that uses functions which you don't (manually) declare yourself, you have to pull in the declarations. These declarations are normally stored in so-called header files that you pull into the current translation unit using the #include preprocessor directive. As the code does not #include the header file in which system() is declared, the compilation fails.

To fix this issue, find out which header file provides you with the declaration of system() and include that. As mentioned in several other answers, you most likely want to add #include <cstdlib>

like image 40
Timo Geusch Avatar answered Sep 21 '22 13:09

Timo Geusch