首页 > 解决方案 > 在收据中显示所有用户的选择

问题描述

我是编程新手,最近我创建了一个程序,该程序应允许用户从菜单中选择任意数量的项目。一旦用户完成,它应该显示总成本和他们订购的物品,但它只显示用户选择的最后一个物品。金额是正确的,总计很好,但它不会显示所有项目。下面是代码:


#include<iostream>
#include<unistd.h>
#include<ctime>
using namespace std;

int main()
{
    string name, address, numphone, choice;
    int quantityF1, quantityF2, quantityF3, quantityF4, quantityF5, quantityF6, quantityF7, quantityF8, quantityB1, quantityB2, quantityB3, quantityB4, quantityB5;
    int totalQF1=0, totalQF2=0, totalQF3=0, totalQF4=0, totalQF5=0, totalQF6=0, totalQF7=0, totalQF8=0, totalQB1=0, totalQB2, totalQB3=0, totalQB4=0, totalQB5=0;
    int paymentMethod, quantity;
    float priceF1, priceF2, priceF3, priceF4, priceF5, priceF6, priceF7, priceF8, priceB1, priceB2, priceB3, priceB4, priceB5; 
    float totalF1=0, totalF2=0, totalF3=0, totalF4=0, totalF5=0, totalF6=0, totalF7=0, totalF8=0, totalB1=0, totalB2=0, totalB3=0, totalB4=0, totalB5=0;
    float totalPrice; 
    float total, subtotal, totalPayment;
    char addorder;
    time_t tt;
    struct tm * ti;
    time(&tt);
    ti=localtime(&tt); 
    
    cout<<"\n";
    cout<<"=========================WELCOME TO ABANG JAGO DELIVERY ORDER========================="<<endl;
    cout<<"\n";
    cout<<"\n";
    cout<<"Enter your name: ";
    getline(cin, name);
    cout<<"Enter your address: ";
    getline(cin, address);
    cout<<"Enter your number phone: ";
    getline(cin, numphone);
    cout<<"\n\n";
    
    cout<<"***********************************START YOUR ORDER***********************************\n\n";
    cout<<"Hi "<<name<<"!\n\n";
    
    cout<<"What would you like to order?\n\n";
    
    cout<<"-----------------------------------------MENU-----------------------------------------\n\n";
    cout<<"|===============FOOD===============|====RM====|==========BEVERAGE==========|====RM====|\n";
    cout<<"|F1 Fried Chicken - 2 pcs          |  RM11.99 |B1 Sprite                   |  RM4.55  |\n";
    cout<<"|F2 Burger ayam berapi             |  RM12.35 |B2 Coke                     |  RM4.32  |\n";
    cout<<"|F3 Burger daging berasap          |  RM12.35 |B3 Milo                     |  RM7.11  |\n";
    cout<<"|F4 Ais cream pusing               |   RM6.80 |B4 Iced lemon tea           |  RM5.94  |\n";
    cout<<"|F5 Pie                            |   RM3.88 |B5 Mineral water            |  RM1.00  |\n";
    cout<<"|F6 Kentang putar                  |   RM6.48 |                            |          |\n";
    cout<<"|F7 Coleslaw                       |   RM7.41 |                            |          |\n";
    cout<<"|F8 French fries                   |   RM5.67 |                            |          |\n\n\n";
    
    while(addorder!='N')
    {
        cout<<"Enter your choice (one order at a time): ";
        cin>>ws;
        getline(cin,choice);
        cout<<"\n";
    
        if(choice == "F1")
        {
            cout<<"You choose fried chicken - 2 pcs.\n";
            cout<<"Enter how much set do you want: ";
            cin>>quantityF1;
            totalQF1 = totalQF1 + quantityF1;
            priceF1 = 11.99 * totalQF1;
            totalF1 = totalF1 + priceF1;
        }
        else
            if(choice == "F2")
            {
                cout<<"You choose burger ayam berapi.\n";
                cout<<"Enter how much burger do you want: ";
                cin>>quantityF2;
                totalQF2 = totalQF2 + quantityF2;
                priceF2 = 12.35 * totalQF2;
                totalF2 = totalF1 + priceF2;

            }
        else
            if(choice == "F3")
            {
                cout<<"You choose burger daging berasap.\n";
                cout<<"Enter how much burger do you want: ";
                cin>>quantityF3;
                totalQF3 = totalQF3 + quantityF3;
                priceF3 = 12.35 * totalQF3;
                totalF3 = totalF2 + priceF3;
            }
        else
            if(choice == "F4")
            {
                cout<<"You choose ais cream pusing.\n";
                cout<<"Enter how much ais cream do you want: ";
                cin>>quantityF4;
                totalQF4 = totalQF4 + quantityF4;
                priceF4 = 6.80 * totalQF4;
                totalF4 = totalF3 + priceF4;
            }
        else
            if(choice == "F5")
            {
                cout<<"You choose pie.\n";
                cout<<"Enter how much pie do you want: ";
                cin>>quantityF5;
                totalQF5 = totalQF5 + quantityF5;
                priceF5 = 3.88 * totalQF5;
                totalF5 = totalF4 + priceF5;    
            }
        else
            if(choice == "F6")
            {
                cout<<"You choose kentang putar.\n";
                cout<<"Enter how much kentang putar do you want: ";
                cin>>quantityF6;
                totalQF6 = totalQF6 + quantityF6;
                priceF5 = 6.48 * totalQF6;
                totalF5 = totalF5 + priceF6;
            }
        else
            if(choice == "F7")
            {
                cout<<"You choose coleslaw.\n";
                cout<<"Enter how much coleslaw do you want: ";
                cin>>quantityF7;
                totalQF7 = totalQF7 + quantityF7;
                priceF7 = 7.41 * totalQF7;
                totalF7 = totalF6 + priceF7;
            }
        else
            if(choice == "F8")
            {
                cout<<"You choose french fries.\n";
                cout<<"Enter how much french fries do you want: ";
                cin>>quantityF8;
                totalQF8 = totalQF8 + quantityF8;
                priceF8 = 5.67 * totalQF8;
                totalF8 = totalF7+ priceF8;
            }
        else
            if(choice == "B1")
            {
                cout<<"You choose sprite.\n";
                cout<<"Enter how much sprite do you want: ";
                cin>>quantityB1;
                totalQB1 = totalQB1 + quantityB1;
                priceB1 = 4.55 * totalQB1;
                totalB1 = totalF8 + priceB1;
            }
        else
            if(choice == "B2")
            {
                cout<<"You choose coke.\n";
                cout<<"Enter how much coke do you want: ";
                cin>>quantityB2;
                totalQB2 = totalQB2 + quantityB2;
                priceB2 = 4.32 * totalQB2;
                totalB2 = totalB1 + priceB2;
            }
        else
            if(choice == "B3")
            {
                cout<<"You choose milo.\n";
                cout<<"Enter how much milo do you want: ";
                cin>>quantityB3;
                totalQB3 = totalQB3 + quantityB3;
                priceB3 = 7.11 * totalQB3;
                totalB3 = totalB2 + priceB3;
            }
        else
            if(choice == "B4")
            {
                cout<<"You choose iced lemon tea.\n";
                cout<<"Enter how much iced lemon tea do you want: ";
                cin>>quantityB4;
                totalQB4 = totalQB4 + quantityB4;
                priceB4 = 5.94 * totalQB4;
                totalB4 = totalB3 + priceB4;
            }
        else
            if(choice == "B5")
            {
                cout<<"You choose mineral water.\n";
                cout<<"Enter how much mineral water do you want: ";
                cin>>quantityB5;
                totalQB5 = totalQB5 + quantityB5;
                priceB5 = 1.00 * totalQB5;
                totalB5 = totalB4 + priceB5;
            }
        else
            cout<<"Enter code in the menu only.";
        cout<<"\n\n";
    
        cout<<"Would you like to order anything else? (Y/N): ";
        cin>>ws;
        cin>>addorder;
        cout<<"\n";
        
    totalPrice = totalF1 + totalF2 + totalF3 + totalF4 + totalF5 + totalF6 + totalF7 + totalF8 + totalB1 + totalB2 + totalB3 + totalB4 + totalB5;
    }
    cout<<"\n";
    cout<<"Proceed to Checkout. "<<endl;
    cout<<".";
    cout<<"\n.";
    cout<<"\n.";
    cout<<"\n.";
    cout<<"\n."<<endl;
    cout<<"------------------------------------PAYMENT METHOD------------------------------------\n";
    cout<<"1) Cash\n";
    cout<<"2) Credit card\n\n";
    cout<<"Choose your payment method (1/2): ";
    cin>>paymentMethod;
    
    if(paymentMethod == 1)
    {
        cout<<"\n";
        cout<<"PAYMENT METHOD: CASH\n";
        subtotal = (totalPrice * 0.10) + totalPrice;
        cout<<"SUBTOTAL = RM"<<subtotal<<endl;
    }
    else
        if(paymentMethod == 2)
        {
            cout<<"\n";
            cout<<"PAYMENT METHOD: CREDIT CARD\n";
            subtotal = (totalPrice * 0.10) + (totalPrice * 0.02) + totalPrice ;
            cout<<"SUBTOTAL = RM"<<subtotal<<endl;
        }
    
    totalPayment = (subtotal * 0.06) + subtotal + 10;
    
    cout<<"\n\n";
    cout<<"----------------------THANK YOU FOR YOUR ORDER, GENERATING BILL----------------------\n\n";
    for(int a = 1; a < 8; a++)
    {
        sleep(2);
        cout<<"...";
    }
    
    system("cls");
    cout<<"=====================================================\n";
    cout<<"           ABANG JAGO DELIVERY ORDER RECEIPT\n";
    cout<<"=====================================================\n";
    cout<<"\n";
    cout<<"\tDay, date & time: "<<asctime(ti)<<endl;
    cout<<"\n";
    cout<<"\tCustomer name: "<<name<<endl;
    cout<<"\tCustomer address: "<<address<<endl;
    cout<<"\tCustomer number phone: "<<numphone<<endl;
    cout<<"\n";
    cout<<"-----------------------------------------------------\n";
    cout<<"\n";
    cout<<"\t"<<choice<<endl;           cout<<"\t"<<quantity<<endl;
    cout<<"\n";
    cout<<"-----------------------------------------------------\n";
    cout<<"\n";
    cout<<"\t"<<totalPrice<<endl;
    cout<<"\tSUBTOTAL = RM"<<subtotal<<endl;
    cout<<"\tDELIVERY CHARGE = RM10"<<endl;
    cout<<"\tTOTAL BILL (incl 6% SST) = RM"<<totalPayment<<endl;
    cout<<"\n";
    cout<<"*****************************************************\n";
    cout<<"     Thank You for Your Order, Please Come Again!\n";
    cout<<"*****************************************************\n";
    
}

标签: c++arraysvector

解决方案


嗯,这很正常,choice每次客户用getline(cin,choice);.

您可能想要查看的是数组或向量。在您的书/教程中可能不会遥遥领先;)

您还可以查看https://www.learncpp.com/cpp-tutorial/arrays-part-i/https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/


推荐阅读