簡體   English   中英

if else語句中的預期表達式錯誤

[英]expected expression error on if else 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;

您需要在代碼中添加適當的{}

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

使用括號:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM