2012-02-16 85 views
0

我在這個非常簡單的程序中不斷收到錯誤,我找不到原因。幫幫我!標識符未找到?

//This program will calculate a theater's revenue from a specific movie. 
#include<iostream> 
#include<iomanip> 
#include<cstring> 
using namespace std; 

int main() 
{ 
    const float APRICE = 6.00, 
      float CPRICE = 3.00; 

    int movieName, 
     aSold, 
     cSold, 
     gRev, 
     nRev, 
     dFee; 

    cout << "Movie title: "; 
    getline(cin, movieName); 
    cout << "Adult tickets sold: "; 
    cin.ignore(); 
    cin >> aSold; 
    cout << "Child tickets sold: "; 
    cin >> cSold; 

    gRev = (aSold * APRICE) + (cSold * CPRICE); 
    nRev = gRev/5.0; 
    dFee = gRev - nRev; 

    cout << fixed << showpoint << setprecision(2); 
    cout << "Movie title:" << setw(48) << movieName << endl; 
    cout << "Number of adult tickets sold:" << setw(31) << aSold << endl; 
    cout << "Number of child tickets sold:" <<setw(31) << cSold << endl; 
    cout << "Gross revenue:" << setw(36) << "$" << setw(10) << gRev << endl; 
    cout << "Distributor fee:" << setw(34) << "$" << setw(10) << dFee << endl; 
    cout << "Net revenue:" << setw(38) << "$" << setw(10) << nRev << endl; 

    return 0; 
} 

這裏是我得到的錯誤:

error C2062: type 'float' unexpected 
error C3861: 'getline': identifier not found 
error C2065: 'CPRICE' : undeclared identifier 

我已經包含了必要的目錄,我不明白爲什麼這是行不通的。

+0

我建議每個聲明只聲明一個變量;例如'... int aSold; int bSold; ...'。 – 2012-02-16 09:35:55

回答

6

爲了您的第一個錯誤,我認爲這個問題是在此聲明:

const float APRICE = 6.00, 
     float CPRICE = 3.00; 

在C++中,在一行聲明多個常數,你不要重複類型的名稱。相反,只寫

const float APRICE = 6.00, 
      CPRICE = 3.00; 

這也應該解決您的最後一個錯誤,我相信這是由編譯器引起感到困惑的是CPRICE是因爲你的宣言誤差的恆定。

對於第二個錯誤,使用getline,你需要

#include <string> 

不僅僅是

#include <cstring> 

由於getline功能是<string>(新的C++字符串頭),而不是<cstring>(舊式C字符串標題)。

這就是說,我認爲你仍然會從中得到錯誤,因爲movieName被聲明爲int。嘗試將其定義爲std::string。您可能還想聲明其他變量爲float,因爲它們存儲的是實數。更一般地說,我建議在你需要的時候定義你的變量,而不是全部放在最上面。

希望這會有所幫助!

+0

+1用於根據需要進行聲明。一個變量應該始終有最嚴格的範圍,並且在聲明後立即進行初始化。 – 2012-02-16 07:29:23