Could anyone explain why does it show an expected expression error after "else"? I thought I was doing everything right.
thank you
#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;
You need to add proper {}
in your code.
if(x<=40){
if(y<3) {
//^^some code
}else{
//^^some code
}
}
Use brackets:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.