找不到标识符?

时间:2012-02-16 07:08:55

标签: c++ string compiler-errors

我在这个非常简单的程序中不断出错,我无法弄清楚原因。救命啊!

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

我已经包含了必要的目录,我无法理解为什么这不起作用。

1 个答案:

答案 0 :(得分: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,因为它们存储实数。更一般地说,我建议您根据需要定义变量,而不是全部在顶部。

希望这有帮助!