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

时间:2012-02-05 18:44:48

标签: c++ new-operator

继承我的代码我试图整理上学,在嵌入式if和else if语句中,当我尝试编译时显示错误。

错误:在“else”之前期望primary-expression          预期';'在其他之前

这两个都出现在每一行。

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

}                

2 个答案:

答案 0 :(得分:7)

有几件事:

当您执行条件时,不要直接使用分号进行测试,因为这会停止条件语句,并会执行与您想要的不同的操作。此外,由于它终止了if-else组,因此您将在下一个'else'语句中生成错误。

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

以下是正确的else if语句的示例:

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

更新:我最初说要使用&amp;&amp;而不是'和'。正如honk所说'和'在c ++中是一个有效的运算符,与&amp;&amp;相同。我更喜欢使用&amp;&amp;与c的便携性。

答案 1 :(得分:1)

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