C++ 使用嵌入式if和else if语句C++;
这是我的代码,我正在努力整理,准备上学,在嵌入的if和elseif语句中,当我试图编译时,它会显示错误 错误:应在“else”之前使用主表达式 期望“;”先 每一行都会出现这两种情况C++ 使用嵌入式if和else if语句C++;,c++,new-operator,C++,New Operator,这是我的代码,我正在努力整理,准备上学,在嵌入的if和elseif语句中,当我试图编译时,它会显示错误 错误:应在“else”之前使用主表达式 期望“;”先 每一行都会出现这两种情况 // This is a program to Calculate the volume of the sphere using the radius (a03.cpp) // Written by: Jimmy Scott // Date: 2/1/12 // Sources: Stackoverflow.com
// This is a program to Calculate the volume of the sphere using the radius (a03.cpp)
// Written by: Jimmy Scott
// Date: 2/1/12
// Sources: Stackoverflow.com (else and if statements)
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char vehicle;
float total;
char over20;
char old;
int adults;
int oldpass;
char height;
int youngpass;
int bicycles;
float length;
cout<<"Fare Calculator by Jimmy Scott";
cout<<"\n\n\n Are You Bringing a vehicle on The Ferry ?";
cin>>vehicle;
if (vehicle == 'y')
{
cout<<"\n\n"<<"Is the Driver over 65 Years of age or disabled?";
cin>>old;
cout<<"\n\n"<<"Passengers going along with the driver"<<"\n\n";
cout<<"Adults (19-64 Years old):";
cin>>adults;
cout<<"\n\n"<<"Senior Citizens or disabled:";
cin>>oldpass;
cout<<"\n\n"<<"Young passengers 5-18 years old: ";
cin>>youngpass;
cout<<"\n\n"<<"Is your Vehicle over 7ft, 6in in height? ";
cin>>height;
cout<<"Vehicle length in feet: ";
cin>>length;
if (old == 'y')
{
total= 44.60;
}
else if (old == 'n');
{
total= 51.20;
}
else if (length < 20) and (height == 'y');
{
total= 102.4;
}
else if (length > 20) and (length < 30);
{
total= 76.80;
}
else if (length > 20) and (length < 30) and (height = 'y');
{
total= 153.60;
}
else if (length > 30) and (length < 40);
{
total= 204.80;
}
}
else
{
cout<<"\n\n"<<"How many Senior Citizens or disabled are in your group?";
cin>>oldpass;
cout<<"\n\n"<<"How many adults are in your group?:";
cin>>adults;
cout<<"\n\n"<<"How many in your group are youths (5-18):";
cin>>youngpass;
cout<<"\n\n"<<"How many in your group have Bicycles:";
cin>>bicycles;
total=oldpass * 6.55;
total= total + (adults * 13.15);
total= total + (youngpass * 10.55);
total= total + (bicycles * 4.00);
}
cout<<"\n\n"<<"your total fare cost is : $"<<total;
cout<<"\n\n\n"<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
//这是一个使用半径(a03.cpp)计算球体体积的程序
//作者:吉米·斯科特
//日期:2012年2月1日
//来源:Stackoverflow.com(else和if语句)
#包括
#包括
#包括
使用名称空间std;
int main()
{
炭载体;
浮动总额;
20岁以上;
焦老;
int成人;
国际奥德帕斯;
焦高;
英特扬帕斯;
国际自行车;
浮子长度;
不能删除if-else()语句后的所有“;”。如果您创建了许多条件,也可以添加“()”。删除if-else()语句后的所有“;”。如果您创建了许多条件,也可以添加“()”。
执行条件语句时,不要直接在测试后面加分号,因为这会停止条件语句,并执行与所需不同的操作。此外,由于它会终止if-else组,您将在下一个“else”语句中生成错误
您还需要用括号括起条件测试
下面是一个正确的else if语句示例:
else if ((length > 30) and (length < 40))
{
total= 204.80;
}
else if((长度>30)和(长度<40))
{
总数=204.80;
}
Update:我最初说使用& &而不是“and”。正如HONK所说的,“C++中的一个有效的操作符做了与& &我更喜欢用C的可移植性相同的事情。
< P>几件事:
执行条件语句时,不要直接在测试后面加分号,因为这会停止条件语句,并执行与所需不同的操作。此外,由于它会终止if-else组,您将在下一个“else”语句中生成错误
您还需要用括号括起条件测试
下面是一个正确的else if语句示例:
else if ((length > 30) and (length < 40))
{
total= 204.80;
}
else if((长度>30)和(长度<40))
{
总数=204.80;
}
更新:我最初说用和&而不是“和”。C++中的一个有效的操作符做了与& &我更喜欢用C的可移植性相同的事情。
<代码>和,<代码>或<代码> >代码>不是>代码>是他们所说的完美的关键词。我认为它们优于代码> & & <代码>代码<> < <代码> >或<代码> <代码>(两者都是因为它们更难出错,而且是可读性)。抱歉,我觉得这是C而不是C++,但很明显是C++。我会更新我的答案。谢谢修复,+1ED。但是我没有你的可移植性点,如果你想写C,不要写C++。(模板和STL也不能很好地移植,而且剩下的也不多了)因为在C和C++中,我和它都有相同的含义,所以我不需要考虑它的不同。对于可移植性,有时我需要在C++中使用一些C代码,并且使用它。当然,逆向不会发生太多,但是我不会被使用& & <代码>和<代码>,<代码>或<代码> >代码> > <代码>是非常好的关键字WHIC。h照他们说的做。我认为他们优于&&
,|
或!
(两者都是因为它们更难出错和可读性)抱歉,我觉得这是C而不是C++,但很明显是C++。我会更新我的答案。谢谢你的修复,+1ED。但是我没有你的可移植性,但是,如果你想写C,不要写C++(模板和STL也不好用,然后,剩下的就不多了)。因为在C++和C++中,我和它都有相同的含义,所以我不必考虑它们之间的差异。至于可移植性,有时我需要在C语言中使用一些C代码并使用它。