C++错误"else",没有前面的"if"

C++ error 'else' without a previous 'if'

本文关键字:前面 if 错误 else C++      更新时间:2023-10-16

我是C++的新手,一直在进行分配

好的,这是运行的,但它不进行计算当窗口打开时,我输入A、B或C然后是单词的长度它只是说pay=0

这是新代码:

#include <iostream>


using namespace std;

char authorLevel;
int numberOfWords, payTotal;
int fixedPayAmount;

int main()
{

cout << "Enter Author Level(A,B,or C):";
char authorLevel;
cin >>authorLevel;
cout << "Enter Length(in words):";
int numberOfWords;
cin >>numberOfWords;
cout << "Pay is: $" << payTotal << endl; 
int payTotal;

cout << "Fixed Pay is:$" << fixedPayAmount << endl;
int fixedPayAmount;
//Calculations for C Level Author

if (authorLevel == 'C')
{
//If the Number of words is <7500 multiply by 0.08     
if (numberOfWords <=7500)
{
payTotal= numberOfWords * 0.08;                     
}
//If the numberOfWords is >7500 to <=8000 pay is fixed 600
if (numberOfWords >7500 || numberOfWords <= 8000)
{
fixedPayAmount= 600;
}
//If the numberOfWords is >8000 to <=17500 multiply by 0.075
if (numberOfWords >8000 || numberOfWords <=17500)     
{
payTotal= numberOfWords * 0.075;
}
//If the numberOfWords is >17500 to <= 19000 fixed $1313
if (numberOfWords >17500 || numberOfWords <= 19000)
{
fixedPayAmount=1313;
}
//If the numberOfWords is >=19000 multiply 0.07
if (numberOfWords >=19000)
{
payTotal= numberOfWords * 0.07;
}
}
else if (authorLevel== 'A')
{

//If the Number of words is <7500 multiply by 0.14     
if (numberOfWords <=7500)
{
payTotal= numberOfWords * 0.14;
}
//If the numberOfWords is >7500 to <=8000 pay is fixed $1050
if (numberOfWords >7500 || numberOfWords <= 8000)
{
fixedPayAmount= 1050;
}
//If the numberOfWords is >8000 to <=17500 multiply by 0.13125
if (numberOfWords >8000 || numberOfWords <=17500)     
{                                               
payTotal= numberOfWords * 0.13125;
}
//If the numberOfWords is >17500 to <= 19000 fixed $2297.75
if (numberOfWords >17500 || numberOfWords <= 19000)
{
fixedPayAmount=2297.75;
}
//If the numberOfWords is >=19000 multiply 0.1225
if (numberOfWords >=19000)
{
payTotal= numberOfWords * 0.1225;
}
}

else if (authorLevel== 'B')
{
//If the Number of words is <7500 multiply by 0.1  
if (numberOfWords <=7500)
{
payTotal= numberOfWords * 0.1;
}
//If the numberOfWords is >7500 to <=8000 pay is fixed $750
if (numberOfWords >7500 || numberOfWords <= 8000)
{
fixedPayAmount= 750;
}
//If the numberOfWords is >8000 to <=17500 multiply by 0.09375
if (numberOfWords >8000 || numberOfWords <=17500)     
{
payTotal= numberOfWords * 0.09375;
}
//If the numberOfWords is >17500 to <= 19000 fixed $1641.25
if (numberOfWords >17500 || numberOfWords <= 19000)
{
fixedPayAmount=1641.25;
}
//If the numberOfWords is >=19000 multiply 0.0875
if (numberOfWords >=19000)
{
payTotal= numberOfWords * 0.0875;
}
}

return 0;
}

在第一个if之后(在行的末尾)有一个分号。去掉它,你就完了。

if (authorLevel =   'C');

应该是

if (authorLevel ==   'C')

正如其他人所指出的,还有其他额外的分号和错误。小心!

您必须删除';'在的末尾(也将"="替换为"=")

if (authorLevel == 'C');

结束时

if (numberOfWords <=7500);

解释

;标志着语句的结束。所以在这里它立即结束if。下面的{...}不仅在条件为true时执行,而且始终。因为如果您在代码中的任何位置放置{...}块,而不在其前面放置条件,那么它将始终执行。

因此,当您编写if (condition); { statements; }时,程序将评估条件表达式,然后它将始终无条件地执行{ statements }。如果在{...}之前删除该;,则会按预期工作,并且只有在条件为true时才执行{...}块。

=是赋值运算符,不用于比较。因此authorLevel = 'C'将把authorLevel的值设置为C。在if内部,分配的结果被用作条件。赋值的结果是所赋值的值,因此在本例中为'C'。由于'C'既不是null也不是0,因此它将被视为true。因此,错误的操作员会同时导致几个问题,要小心

使用==运算符进行比较。

这是"if"语句末尾的分号,例如

if (authorLevel =   'C');

这相当于:

if (authorLevel =   'C')
{
;
}

换句话说,这意味着"if"语句后面的块不是"if"声明的一部分,并且不是有条件执行的。

您可以在代码中重复执行此操作,但正是我作为示例使用的语句导致了您告诉我们的编译器错误。

编辑:"="也是赋值运算符。几乎可以肯定,您打算使用"==",这是相等比较运算符。

在第一个if:的末尾有一个分号

if (authorLevel =   'C');

您生成了一个if语句,该语句除了一个空语句外,没有附加任何代码。接着是一个不相关的代码块,后面有一个else

您需要删除分号。