2012-02-16 98 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

相关问题