2013-10-08 175 views
-3

我想爲學校寫一個簡單的程序,我有點問題。我必須輸入兩次才能正常工作。 例如我有輸入3兩次,它給我90 .. 我能做些什麼來解決它。 此刻爲什麼我必須輸入兩次?

#include <iostream> 
#include <string> 
using namespace std; 

int main(){ 
    string Cartype, rateoption; 
    double total, miles, days; 
    const double CdailyRate=30; 
    const double PdailyRate=40; 
    const double FdailyRate=50; 
    const double CmileRate=0.25; 
    const double PmileRate=0.35; 
    const double FmileRate=0.45; 

    cout<<"Thank you for choosing Car Rite Rental for your rental needs!\n" 
    <<"\a Before we get started calculating your total owed please remember\n" 
    <<"that here at Car Rite Rental we havea MINIMUM PAYMENT OF $30.\n\n" 
    <<"Please enter the type of car you have rented: \n\n" 
    <<"[please enter corresponding letter] \n" 
    <<"C-Chevrolet\n"<<"P-Pontiac\n"<<"F-Ford\n"; 
    cin>>Cartype; 
    cout<<"Please choose your payment option from the following: \n\n" 
    <<"[please enter corresponding number] \n" 
    <<"D-Daily Rate\n"<<"M-Mileage Rate\n"; 
    cin>>rateoption; 

    if(rateoption=="D"||rateoption=="d"){ 
     cout<<"Please enter the number of days you have rented this vehicle: \n"; 
     cin>>days; 
    } 
    else 
     cout<<"Please enter the number of miles traveled in your rental car:\n"; 

     cin>>miles; 

    if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d"){ 
     total=CdailyRate*days; 
     cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n"; 
    } 

    return 0;    
} 
+0

請格式化您的代碼;如果我們無法閱讀它,那麼我們無法幫助您(加上編寫可讀代碼是必要技能)。 –

回答

4

是否編碼看正確的,因爲你有一個失蹤的大括號。

else 
    cout<<"Please enter the number of miles traveled in your rental car:\n"; 

    cin>>miles; 

儘管它是縮進的,「cin >>英里」;語句總是執行,它不是以else爲條件的。

else { 
    cout<<"Please enter the number of miles traveled in your rental car:\n"; 

    cin>>miles; 
} 

會解決它。

+0

謝謝你。這很簡單。我感覺很傻 – user2857087

+0

@ user2857087大家都這麼做哈哈 – OMGtechy

相關問題