2012-02-05 76 views
1

繼承人我的代碼即時通訊嘗試整理學校,在嵌入式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: 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;       

}     

回答

1

消除所有';'在if else()語句之後。如果你做了很多條件,還要加上'()'。

7

幾件事情:

當你做一個條件,不要直接用分號按照試驗,因爲這將停止條件語句,並會做一些事情,你要哪個不同。此外,由於它會終止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