没有先前'if'的C ++错误'else'

时间:2014-03-04 18:03:03

标签: c++ if-statement output

我是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;
}

4 个答案:

答案 0 :(得分:8)

在第一个if之后(在行尾)有一个分号。删除它,你就完成了。

if (authorLevel =   'C');

应该是

if (authorLevel ==   'C')

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

答案 1 :(得分:2)

你必须删除';'在末尾(并且还用'=='替换'=')

if (authorLevel == 'C');

结束时
if (numberOfWords <=7500);

<强>解释

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

因此,当您编写if (condition); { statements; }时,程序将评估条件表达式,之后它将始终无条件地执行{ statements }。如果您在;之前移除了{...},那将按预期工作,并且仅在条件为真时执行{...}块。

=是赋值运算符,不用于比较。因此authorLevel = 'C'会将authorLevel的值设置为C。在if内,赋值的结果用作条件。赋值的结果是赋值,因此在本例中为'C'。由于'C'既不是null也不是0,它将被视为真。因此误操作操作员会同时导致一些问题,请小心。

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

答案 2 :(得分:2)

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

if (authorLevel =   'C');

这相当于:

if (authorLevel =   'C')
{
  ;
}

换句话说,这意味着“if”语句之后的块不是“if”语句的一部分,并且没有条件执行。

你在代码中重复这样做,但这是我用作一个例子的声明,它特别导致你告诉我们的编译错误。

编辑:此外,“=”是赋值运算符。你几乎肯定要使用“==”,这是相等比较运算符。

答案 3 :(得分:1)

您的第一个if结尾处有一个分号:

if (authorLevel =   'C');

除了空语句外,您已生成if语句,其中附有无代码。然后,您可以通过一个不相关的代码块来跟踪它,该代码块有else跟踪它。

您需要删除分号。