C ++如何在多个if语句中添加值的总和

时间:2017-03-25 16:40:30

标签: c++

我在收集和打印程序中的总和(totalPrice)时遇到问题。

程序反复要求用户输入票证信息,然后计算所有票据的总价格。

如果向其他目的地请求了票证,则程序会通知用户该目的地没有可用的票证。用户可以输入给定的城市名称或全部小写。 往返价格是单程价格乘以1.6。学生可享受20%的折扣。 当用户在询问目的地时输入单词“完成”或“完成”时,程序将输出票证总数及其总价格和退出。

据我所知,一切都运转正常,但总成本的最终输出除外。关于我正在做什么的任何帮助人。我相信这可能是我的逻辑,但我太投入了,看不出我的错误!任何帮助将不胜感激,谢谢:)

using namespace std;

int main() {

    double totalPrice = 0; // the sum of all the values after the do while loop is done.
    int numberOfTicketsSold; // ehhhh counter?
    int counter; // keep track of the number of flights
    double temp = 0; // to hold the values while they're being adjusted
    double discount = 0; // another temp but for discount
    double boston = 350; // constant at 350 per trip
    double london = 600;
    double paris = 700;
    double tokyo = 800;

    string input; // might have to use char instead. Lets try it out anyways. 
    string roundTrip;
    string studentDiscount;

    cout << "**************************************************************" << endl;
    cout << "Enter the information for each ticket." << endl;
    cout << "When no more tickets to enter, enter 'Done' for destination." << endl;
    cout << "**************************************************************" << endl;

    do{

        cout << "Enter the destination city:" ; // Want to collect the string inputted to start if statements
        cin >> input;

        if ( input == "boston" || input == "Boston")
        {
            counter++; // keeps track of how many flights. For last output 
            cout << "\nRound trip? [Y/N]" << endl;
            cin >> roundTrip;
            if (roundTrip == "y" || roundTrip == "Y")
            {
                temp = boston; 
                temp = temp * 1.6;

            }
            else if(roundTrip == "n" || roundTrip == "N")
            {
                temp = boston;
            }

            cout << "\nStudent Discount? [Y/N]";
            cin >> studentDiscount;
            if (studentDiscount == "y" || studentDiscount == "Y")
            {
                discount = temp;
                discount = discount *.20;
                temp = temp - discount;
                totalPrice = temp;

            }
            else if(studentDiscount == "N" || studentDiscount == "n")
            {
             totalPrice = temp;// do nothing right? 
            }

        } 

        else if ( input == "london" || input == "London")
        {

            counter++; // keeps track of how many flights. For last output 
            cout << "\nRound trip? [Y/N]" << endl;
            cin >> roundTrip;
            if (roundTrip == "y" || roundTrip == "Y")
            {
                temp = london; 
                temp = temp * 1.6;

            }
            else if(roundTrip == "n" || roundTrip == "N")
            {
                temp = london;
            }

            cout << "\nStudent Discount? [Y/N]";
            cin >> studentDiscount;
            if (studentDiscount == "y" || studentDiscount == "Y")
            {
                discount = temp; // 600
                discount = discount *.20;
                temp = temp - discount;
                totalPrice = totalPrice + temp;

            }
            else if(studentDiscount == "N" || studentDiscount == "n")
            {
             totalPrice = totalPrice + temp;// do nothing right? 
            }

        } 

        else if ( input == "paris" || input == "Paris")
        {

            counter++; // keeps track of how many flights. For last output 
            cout << "\nRound trip? [Y/N]" << endl;
            cin >> roundTrip;
            if (roundTrip == "y" || roundTrip == "Y")
            {
                temp = paris; 
                temp = temp * 1.6;

            }
            else if(roundTrip == "n" || roundTrip == "N")
            {
                temp = paris;
            }

            cout << "\nStudent Discount? [Y/N]";
            cin >> studentDiscount;
            if (studentDiscount == "y" || studentDiscount == "Y")
            {
                discount = temp;
                discount = discount *.20;
                temp = temp - discount;
                totalPrice = temp;

            }
            else if(studentDiscount == "N" || studentDiscount == "n")
            {
             totalPrice = temp;// do nothing right? 
            }

        } 

        else if ( input == "tokyo" || input == "Tokyo")
        {

            counter++; // keeps track of how many flights. For last output 
            cout << "\nRound trip? [Y/N]" << endl;
            cin >> roundTrip;
            if (roundTrip == "y" || roundTrip == "Y")
            {
                temp = tokyo; 
                temp = temp * 1.6;

            }
            else if(roundTrip == "n" || roundTrip == "N")
            {
                temp = tokyo;
            }

            cout << "\nStudent Discount? [Y/N]";
            cin >> studentDiscount;
            if (studentDiscount == "y" || studentDiscount == "Y")
            {
                discount = temp;
                discount = discount *.20;
                temp = temp - discount;
                totalPrice = temp;

            }
            else if(studentDiscount == "N" || studentDiscount == "n")
            {
             totalPrice = temp;// do nothing right? 
            }

        } 


    } while (input != "done");

        cout << "--------------------------------------------------------------"<< endl;
        cout <<  counter << " tickets sold for a total sum of $" << temp << endl;
        cout << "-------------------------------------------------------------";

}

0 个答案:

没有答案