找不到标识符

Identifiers not found?

本文关键字:标识符 找不到      更新时间:2023-10-16

我在这个非常简单的程序中不断地出错,我不知道为什么。帮助

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

我已经包含了必要的目录,我不明白为什么这不起作用。

对于您的第一个错误,我认为问题出在这个声明中:

 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。您可能还想将其他变量声明为floats,因为它们存储的是实数。更普遍地说,我建议根据需要定义变量,而不是全部放在顶部。

希望这能有所帮助!