没有找到标识符?

发布于 2025-01-06 07:07:16 字数 1622 浏览 0 评论 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

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

I keep getting errors in this really simple program and I can't figure out why. Help!

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

And here are the errors I'm getting:

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

I've included the necessary directories, I can't understand why this isn't working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2025-01-13 07:07:16

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

 const float APRICE = 6.00,
       float CPRICE = 3.00;

在 C++ 中,要在一行中声明多个常量,您不要重复类型的名称。相反,只需编写

 const float APRICE = 6.00,
             CPRICE = 3.00;

This 也应该可以修复您的最后一个错误,我相信这是由于编译器因声明中的错误而混淆 CPRICE 是一个常量而引起的。

需要

#include <string>

对于第二个错误,要使用 getline,您不仅

#include <cstring>

因为 getline 函数位于 (新的 C++ 字符串header)而不是 (旧式 C 字符串标头)。

也就是说,我认为您仍然会收到错误,因为 movieName 被声明为 int。尝试将其定义为 std::string 。您可能还想将其他变量声明为浮点型,因为它们存储实数。更一般地说,我建议根据需要定义变量,而不是全部放在顶部。

希望这有帮助!

For your first error, I think that the problem is in this declaration:

 const float APRICE = 6.00,
       float CPRICE = 3.00;

In C++, to declare multiple constants in a line, you don't repeat the name of the type. Instead, just write

 const float APRICE = 6.00,
             CPRICE = 3.00;

This should also fix your last error, which I believe is caused by the compiler getting confused that CPRICE is a constant because of the error in your declaration.

For the second error, to use getline, you need to

#include <string>

not just

#include <cstring>

Since the getline function is in <string> (the new C++ string header) and not <cstring> (the old-style C string header).

That said, I think you'll still get errors from this, because movieName is declared as an int. Try defining it as a std::string instead. You might also want to declare your other variables as floats, since they're storing real numbers. More generally, I would suggest defining your variables as you need them, rather than all up at the top.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文