Tay*_*r M 0 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;
}
Run Code Online (Sandbox Code Playgroud)
以下是我得到的错误:
error C2062: type 'float' unexpected
error C3861: 'getline': identifier not found
error C2065: 'CPRICE' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)
我已经包含了必要的目录,我无法理解为什么这不起作用.
对于您的第一个错误,我认为问题出在此声明中:
const float APRICE = 6.00,
float CPRICE = 3.00;
Run Code Online (Sandbox Code Playgroud)
在C++中,要在一行中声明多个常量,不要重复该类型的名称.相反,只需写
const float APRICE = 6.00,
CPRICE = 3.00;
Run Code Online (Sandbox Code Playgroud)
这也应该修复你的上一个错误,我认为这是因为编译器CPRICE因为你的声明中的错误而变得混乱而导致的错误.
对于第二个错误,要使用getline,您需要
#include <string>
Run Code Online (Sandbox Code Playgroud)
不只是
#include <cstring>
Run Code Online (Sandbox Code Playgroud)
由于getline函数在<string>(新的C++字符串头)而不是<cstring>(旧式C字符串头).
也就是说,我认为你仍然会从中获得错误,因为它movieName被声明为int.尝试将其定义为std::string替代.您可能还希望将其他变量声明为floats,因为它们存储实数.更一般地说,我建议您根据需要定义变量,而不是全部在顶部.
希望这可以帮助!