C++错误"else",没有前面的"if"
C++ error 'else' without a previous 'if'
我是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
您需要删除分号。
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 使用if-else将数字转换为单词
- if 语句仅在前面有调试 cout 行(C 中的多线程)时才通过
- 函数中不必要的弹出指令,前面有if语句