为什么我要输入两次?

时间:2013-10-08 04:55:08

标签: c++ cin

我试着为学校写一个简单的程序,我有点问题。我必须输入两次才能正常工作。 例如我输入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;            
}

1 个答案:

答案 0 :(得分:4)

因为你有一套缺失的大括号。

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

    cin>>miles;

虽然它是缩进的,但是“cin&gt;&gt;里程数”;语句总是被执行,它不以else为条件。

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

    cin>>miles;
}

会解决它。