使用嵌入式if和else if语句C++

问题描述:

继承人我的代码即时通讯尝试整理学校,在嵌入式if和else if语句,它显示错误,当我尝试编译。使用嵌入式if和else if语句C++

错误:期望“else”之前的primary-expression expected';'之前

这两个都出现在每一行。

// 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: *.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;       

}     

消除所有';'在if else()语句之后。如果你做了很多条件,还要加上'()'。

几件事情:

当你做一个条件,不要直接用分号按照试验,因为这将停止条件语句,并会做一些事情,你要哪个不同。此外,由于它会终止if-else组,因此您将在下一个“else”语句中生成错误。

您还需要用括号括起您的条件测试。

下面是if语句正确其他的例子:

else if ((length > 30) and (length < 40)) 
{ 
    total= 204.80; 
} 

更新:我最初说用& &,而不是“和”。正如honk所说,'和'是一个有效的C++操作符,与& &的功能相同。尽管如此,我更愿意使用& &来实现可移植性。

+3

''''''''''''''''不是完美的关键词,它们会按照自己的意思行事。我认为它们优于'&&',''''或'!'(因为它们很难出错并且易读)。 – 2012-02-05 18:51:13

+0

对不起,我不知何故认为这是c而不是C++,但它显然是C++。我会更新我的答案。 – 2012-02-05 18:56:40

+0

感谢您的修正+ 1ed。虽然我不明白你的可移植性,但如果你想写C,不要编写C++(模板和STL也不能很好地移植,然后剩下的就不多)。 – 2012-02-05 19:07:14