[英]C++ expected primary-expression before ';' token
I get an error message that states "expected primary-expression ';' 我收到一条错误消息,指出“预期的主表达式';' token" and it highlights my percentage formula.
令牌”,它突出显示了我的百分比公式。 I have tried to rearrange my code, but it seems the problem is not in it.
我试图重新排列我的代码,但似乎问题不在其中。
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;
% used here is the modulo operator which is a binary operator... so this is what you have to do... 这里使用的%是模运算符,它是二进制运算符...所以这是您要做的...
percentage = (totalOccupied / TOTALSUITES)* 100;
//and then where you have cout percentage at that point...do this //然后在该点有cout百分比的地方...执行此操作
cout<<"the percentage of occupied suites is"<<percentage<<"%";
100%
is not 100 percent. 100%
不是100%。 100%
is trying to use the %
operator in an incorrect manner. 100%
试图以错误的方式使用%
运算符 。 To multiply by 100% you just use 1
which is not needed as anything times 1 is itself. 要乘以100%,您只需要使用
1
即可,因为1本身就是任何东西。
percentage = (totalOccupied / TOTALSUITES) * 100% ;
This is not valid syntax. 这是无效的语法。 Change it to this.
更改为此。
percentage = (totalOccupied / TOTALSUITES);
Assuming your totalOccupied
is not a float, you should probably do this as well: 假设您的
totalOccupied
不是浮点数,则可能也应该这样做:
percentage = (static_cast<float>(totalOccupied) / TOTALSUITES);
%
is actually the modulus operator in C++, requiring two arguments. %
实际上是C ++中的模运算符,需要两个参数。
100%
is therefore not syntactically valid. 因此
100%
在语法上无效。
Assuming that you want %
to stand-in for a "divide by 100" operator, the simplest thing to do in your case is to drop the 100%
from your code. 假设您希望
%
代表“除以100”运算符,那么最简单的方法是从代码中删除100%
。
Note that totalOccupied / TOTALSUITES
will be carried out in integer arithmetic if totalOccupied
is also an int
or unsigned
. 注意,如果
totalOccupied
也是int
或unsigned
totalOccupied
以整数算术方式执行totalOccupied / TOTALSUITES
。 Fix that by promoting one of the arguments to a double
, or pre-multiply the term with 1.0
. 通过将其中一个参数提升为
double
或将该术语预乘1.0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.