2010-10-07 5 views
15

Conosco il suo codice semplice, Come posso risolvere il problema "Sistema non dichiarato nell'ambito"?Sistema non dichiarato nell'ambito?

#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 il mio messaggio finale:

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 ===|

+0

cosa è 'system (" TITLE Calculator "); Sistema ("COLORE 2"); si suppone che stia facendo, cos sistema non è una funzione incorporata –

risposta

11

Le probabilità sono che non hai inserito il file di intestazione che dichiara system().

Per poter compilare codice C++ che utilizza funzioni che non si dichiarano (manualmente), è necessario inserire le dichiarazioni. Queste dichiarazioni sono normalmente memorizzate nei cosiddetti file di intestazione che si inseriscono nell'unità di traduzione corrente utilizzando la direttiva preprocessore #include. Poiché il codice non è #include il file di intestazione in cui è dichiarato system(), la compilazione ha esito negativo.

Per risolvere questo problema, scoprire quale file di intestazione fornisce la dichiarazione di system() e includerlo. Come già detto in molte altre risposte, molto probabilmente desidera aggiungere #include <cstdlib>

+0

Cosa intendi con questo? –

+0

Vedi risposta modificata sopra –

47

È necessario aggiungere:

#include <cstdlib> 

in modo che il compilatore per vedere il prototipo per system().

+0

Grazie mille! questo ha fatto il trucco –

+2

Se si usa C è #include Doug

+0

@Doug: certo, ma la domanda è codificata 'C++' –