没有找到标识符?
我在这个非常简单的程序中不断出错,但我不明白为什么。帮助!
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的第一个错误,我认为问题出在这个声明中:
在 C++ 中,要在一行中声明多个常量,您不要重复类型的名称。相反,只需编写
This 也应该可以修复您的最后一个错误,我相信这是由于编译器因声明中的错误而混淆
CPRICE
是一个常量而引起的。需要
对于第二个错误,要使用
getline
,您不仅因为
getline
函数位于
(新的 C++ 字符串header)而不是
(旧式 C 字符串标头)。也就是说,我认为您仍然会收到错误,因为
movieName
被声明为int
。尝试将其定义为std::string
。您可能还想将其他变量声明为浮点型,因为它们存储实数。更一般地说,我建议根据需要定义变量,而不是全部放在顶部。希望这有帮助!
For your first error, I think that the problem is in this declaration:
In C++, to declare multiple constants in a line, you don't repeat the name of the type. Instead, just write
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 tonot just
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 anint
. Try defining it as astd::string
instead. You might also want to declare your other variables asfloat
s, 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!