C++ if-else语句应为表达式错误
有人能解释一下为什么在“else”之后会出现预期的表达式错误吗?我以为我做的每件事都是对的 多谢各位C++ if-else语句应为表达式错误,c++,if-statement,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 in
#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;
#包括
使用名称空间std;
int main()
{
double x;//每周工作小时数
双z;//grosspay
双w;//预扣金额
双n;//净工资
int y;//受抚养人的数量
您需要在代码中添加适当的{}
if(x<=40){
if(y<3) {
//^^some code
}else{
//^^some code
}
}
if(x您需要在代码中添加适当的{}
if(x<=40){
if(y<3) {
//^^some code
}else{
//^^some code
}
}
if(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 //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;
}
}
if(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 //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;
}
}
if(xy)对于if{}else{},您需要在{}
中包含多个语句
。使用有助于缩进的体面编辑器可以防止此错误。w=0.06*…
行和以下所有内容最终都会出现凹痕,显然超出了if
,并且在进入else
之前,您就知道有什么地方出错了。z=16.78x;
是一个语法错误;是吗您的意思是z=16.78*x;
?您需要在{}
的if{}else{}中包含多个语句
。使用有助于缩进的体面编辑器可以防止此错误。w=0.06*…
行和以下所有内容最终都会出现凹痕,显然超出了if
,并且在进入else
之前,您就知道有什么地方出错了。z=16.78x;
是一个语法错误;是吗你是说z=16.78*x;
?