if else语句中的预期表达式错误

时间:2013-06-15 00:11:53

标签: c++ if-statement

有人可以解释为什么在“else”之后显示预期的表达式错误?我以为我做得很对。

谢谢

#include <iostream>
using namespace std;

int main()
{
    double x; // number of hours work per week
    double z; //grosspay
    double w; //withholding amounts
    double n; //net pay
    int y; // number of dependents


cout<<"how many hours do you work in a week?"<<endl;
cin >> x;


    if(x<=40)
        if(y<3)
            z = 16.78*x;
            w = 0.06*x+0.14*x+0.05*x+10;
            n = z-w;
         cout<< "the grosspay is"<< z <<endl
             <<" the withholding amount is"<< w <<endl
             <<" the netpay is" << n <<endl;
        else---------------------------------------------------------expected expression error
            z= 16.78x;
            w= 0.06*x+0.14*x+0.05*x+10+35;
            n=z-w;
        cout<< "the grosspay is"<< z <<endl
            <<" the withholding amount is"<< w <<endl
            <<" the netpay is" << n <<endl;
    if(x>40)
        if(y<3)
            z= 16.78*40+(x-40*16.78);
            w= 0.06*x+0.14*x+0.05*x+10;
            n=z-w;
        cout<< "the grosspay is"<< z <<endl
            <<" the withholding amount is"<< w <<endl
            <<" the netpay is" << n <<endl;

2 个答案:

答案 0 :(得分:2)

您需要在代码中添加适当的{}

if(x<=40){
    if(y<3) {
    //^^some code 
    }else{
   //^^some code
     }
}

答案 1 :(得分:0)

使用括号:

   if(x<=40)
   {
       if(y<3)
       {
           z = 16.78*x;
           w = 0.06*x+0.14*x+0.05*x+10;
           n = z-w;
           cout << "the grosspay is"<< z <<endl
             <<" the withholding amount is"<< w <<endl
             <<" the netpay is" << n <<endl;
        }
        else //no more error
        {
            z= 16.78x;
            w= 0.06*x+0.14*x+0.05*x+10+35;
            n=z-w;
            cout<< "the grosspay is"<< z <<endl
            <<" the withholding amount is"<< w <<endl
            <<" the netpay is" << n <<endl;
        }
    }
    if(x>40)
    {
        if(y<3)
        { 
            z= 16.78*40+(x-40*16.78);
            w= 0.06*x+0.14*x+0.05*x+10;
            n=z-w;
            cout<< "the grosspay is"<< z <<endl
              <<" the withholding amount is"<< w <<endl
              <<" the netpay is" << n <<endl;
        }
    }
相关问题