C++ C++;在';之前应为主表达式';代币
我收到一条错误消息,声明“预期的主表达式”;“标记”,并突出显示我的百分比公式。我试图重新排列我的代码,但问题似乎不在其中C++ C++;在';之前应为主表达式';代币,c++,compiler-errors,C++,Compiler Errors,我收到一条错误消息,声明“预期的主表达式”;“标记”,并突出显示我的百分比公式。我试图重新排列我的代码,但问题似乎不在其中 const int TOTALSUITES = 120; for (int floor = 10; floor <= 16; floor++) { if ( floor != 13) { do { cout << "Please enter the number of suites occupied on
const int TOTALSUITES = 120;
for (int floor = 10; floor <= 16; floor++) {
if ( floor != 13) {
do {
cout << "Please enter the number of suites occupied on the floor " << floor << ":";
cin >> numOccupied;
if ((numOccupied <0) || (numOccupied >20 )) {
goodChoice = false;
cout << "\n\t\t**ERROR" << numOccupied << " ***\n\n";
cout << "*** Choice must be from [0-20]***\n";
}
else {
goodChoice = true;
}
totalOccupied += numOccupied;
} while (!goodChoice);
}
}
percentage = (totalOccupied / TOTALSUITES) * 100% ;
cout << endl;
cout << "Hotel has " << TOTALSUITES << endl;
cout << "Number of occupied suites is " << totalOccupied << endl;
cout << "The percentage of occupied suites is " << percentage << endl;
system("pause");
return 0;
const int TOTALSUITES=120;
for(int floor=10;floor100%
不是100%。100%
试图以错误的方式使用。要乘以100%,只需使用1
,这是不需要的,因为任何乘以1的值都是1本身
percentage = (totalOccupied / TOTALSUITES) * 100% ;
这是无效语法。请将其更改为
percentage = (totalOccupied / TOTALSUITES);
假设您的TotalAccounted
不是浮点,您可能也应该这样做:
percentage = (static_cast<float>(totalOccupied) / TOTALSUITES);
percentage=(静态_cast(总占用)/TOTALSUITES);
<代码> > p> <代码> %>代码>实际上是C++中的模数运算符,需要两个参数。
100%
因此在语法上无效
假设您希望%
代表“除以100”运算符,那么在您的情况下,最简单的方法就是从代码中删除100%
请注意,如果totalaccusperated
也是int
或无符号的
,则totalaccusperated/TOTALSUITES
将在整数算术中执行。通过将其中一个参数提升为双精度
,或将术语与1.0
相乘,可以修复此问题。此处使用的模运算符是二进制运算符。。。
这就是你要做的
percentage = (totalOccupied / TOTALSUITES)* 100;
//然后,在那个点上,你有cout百分比…做这个
cout<<"the percentage of occupied suites is"<<percentage<<"%";
我可能已经为此挣扎了一段时间了。T.我非常感谢你,我更愿意看到翻倍(并且愿意投票),但我明白你的意思我在cout语句中使用的符号只是用来显示百分号,对code@SunnySmile24如果我的答案或你认为合适的任何其他答案,则标记为最合适的答案,即在“向上投票”和“向下投票”按钮下方打勾…以便其他人知道其答案已在哪一行中回答错误在哪里?