Identifiers not found?

2019-06-09 13:21发布

问题:

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.

回答1:

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!