2013-04-12 42 views
0

我是新來的C++,我寫了我的第一個'真正的'程序(一個簡單的四操作計算器)。當我編譯它時,我得到了12個編譯器錯誤。我能夠解決一些(丟失分號等),但其中7人拒絕離開。錯誤和我的代碼在這裏:錯誤:我的每個函數和變量'沒有在這個範圍內聲明(代碼;; blocks,C++)

的錯誤:

15 error: 'cout' was not declared in this scope 
    15 error: 'endl' was not declared in this scope 
    21 error: 'GetNumber' was not declared in this scope 
    24 error: 'GetOperator' was not declared in this scope 
    29 error: 'nAnswer' was not declared in this scope 
    33 error: 'AskContinue' was not declared in this scope 
    35 error: 'bContinue' was not declared in this scope 

Main.cpp的

//Four-operation calculator 
    //© Olli Utriainen 2013 

    #include <iostream> 
    //Header with functions GetNumber(), GetOperator() and AskContinue() 
    #include "calculator.h" 

    using namespace std; 

    int main() 
    { 
     //Main loop's condition 
     bool bContinue = false; 

     cout << "Welcome to Olli's calculator!" << endl << endl; 

     //Main loop 
     do 
     { 
      cout << "Give first number" << endl; 
      int nNumber1 = GetNumber(); 

      cout << "Give operator (+,-,*,/)" << endl; 
      char cOperator = GetOperator(); 

      cout << "Give second number" << endl; 
      int nNumber2 = GetNumber(); 

      nAnswer = nNumber1 + nNumber2; 

      cout << nNumber1 << " " << cOperator << " " << 
      nNumber2 << " = " << nAnswer << endl; 

      bContinue = AskContinue(); 

     } while (bcontinue); 

     return 0; 
    } 

Headers.h

//Headers for the calculator 
    //© Olli Utriainen 2013 

    #ifndef CALCULATOR_H_INCLUDED 
    #define CALCULATOR_H_INCLUDED 

    //Function for inputting numbers 
    int GetNumber() 

    //Function for inputting a mathematical operator (+,-,*,/) 
    char GetOperator() 

    //Function for asking Continue Yes/No 
    bool AskContinue() 

    #endif // CALCULATOR_H_INCLUDED 

Functions.cpp

//Functions for the calculator 
    //© Olli Utriainen 2013 

    #include <iostream> 

    using namespace std; 

    //Function for inputting numbers 
    int GetNumber() 
    { 
     int nNumber 

     cin >> nNumber; 

     return nNumber 
    } 

    //Function for inputting a mathematical operator (+,-,*,/) 
    int GetOperator() 
    { 
     char cOperator 
     bool bValidOperator = false; 

     //Do-while loop for checking if given operator 
     //is valid (+,-,*,/). If invalid, bValidOperator 
     //is changed to false ->loop will break and the 
     //given operator is returned 
     do 
     { 
      cin >> cOperator; 

      if (cOperator == '+' || '-' || '*' || '/') 
       { 
        bValidOperator = true; 
       } 
      else 
       { 
        bValidOperator = false; 
        cout << "Invalid operator" << endl; 
       } 


     } while (!bValidOperator) 

     return cOperator; 

    } 

    //Function for asking Continue Yes/No 
    bool AskContinue() 
    { 
     cout << "New calculation? Y/N" << endl; 

     bool bContinue = false; 
     if (cin >> 'Y' || 'y') 
      { 
       bContinue = true; 
      } 
     if (cin >> 'N' || 'n') 
      { 
       bContinue = false; 
      } 
     return bcontinue; 
    } 
+1

您應該添加大量的';' – RiaD

+0

AskContinue()也被破壞 – RiaD

+1

在學習C++時,編寫一小部分程序,確保編譯和運行正確,這是一個非常好的做法,然後重複。這裏的總體問題是,您繼續向不正確的代碼添加不正確的代碼。 –

回答

2

標題中函數聲明的分號?哦,實際上有很多分號不齊。確保在需要時插入它們,然後重試。

1

大量的錯字; 缺少分號的分號;

在calculator.cpp,你應該#包括 「calculator.h」

在布爾AskContinue

() 它應該是:

{ 
cout << "New calculation? Y/N" << endl; 
bool bContinue = false; 
char choice; 
cin >> choice; 
if ((choice == 'Y')||(choice == 'y')) 
{ 
    bContinue = true; 
} 
    if ((choice == 'N')||(choice == 'n')) 
{ 
    bContinue = false; 
} 
    return bContinue; 
} 

這些是我可以現在才發現。

相關問題