创建带有商品名称,数量和总价的收据

问题描述 投票:-1回答:1

在用户输入No或停止购买后,如何创建收据以打印所选商品的所有名称,数量及其总价?

我使用了switch,因此我很难找到有关收据的任何参考。

#include <iostream>
using namespace std;

 char acoustic, electric, drums, piano, music, equip, Yes, No, y, name;

int pay, Qty;
std::string again;

int main()
{

{
cout << "Welcome to the Music Shop" << endl <<endl;
cout << "                                                                         ===========================                            "<<endl;
cout << "                                                                         |       Categories        |                            "<<endl;
cout << "                                                                         |  [a] Acoustic Guitar    |                            "<<endl;
cout << "                                                                         |  [b] Electric Guitar    |                            "<<endl;
cout << "                                                                         |  [c] Drums              |                            "<<endl;
cout << "                                                                         |  [d] Piano              |                            "<<endl;
cout << "                                                                         |  [e] Music Equipments   |                            "<<endl;
cout << "                                                                         ===========================                            "<<endl;


do {    
cout << "                                                                     Choose from the available categories:";
cin >> music;
cout << ""<<endl;

switch (music)
{
    case 'a':

    cout << "                                                             =============================================== "<<endl;
    cout<< "                                                             |                Acoustic Guitars             |" << endl;
    cout<< "                                                             |                                             |" << endl;
    cout<< "                                                             |           Product                 Price     |" << endl;
    cout<< "                                                             | [1] Fender Acoustic Guitar      P6,900.00   |" << endl;
    cout<< "                                                             | [2] Hartwood Acoustic Guitar    P6,300.00   |" << endl;
    cout << "                                                             =============================================== "<<endl<<endl;
    cout<<"                                                                Choose from the available Acoustic Guitars:";
    cin>>acoustic;

    if (acoustic == '1'){
        cout<<"Enter the quantity:";
        cin>>Qty;
        cout<<"("<<Qty<<")"<<"Fender Acoustic Guitar was added to your cart"<<endl;
        cout<<"Would you like to add more item? (Yes/No)"<<endl;
        cin>>again;

    }
    else if (acoustic == '2'){
        cout<<"Enter the quantity:";
        cin>>Qty;
        cout<<"("<<Qty<<")"<<"Hartwood Acoustic Guitar was added to your cart"<<endl;
        cout<<"Would you like to add more item? (Yes/No)"<<endl;
        cin>>again;
    }
    else {
        cout<<"Invalid"<<endl;
    } 

    break;

    case 'b':

    cout << "                                                             =============================================== "<<endl;
    cout<< "                                                             |                Electric Guitars             |" << endl;
    cout<< "                                                             |                                             |" << endl;
    cout<< "                                                             |           Product                 Price     |" << endl;
    cout<< "                                                             | [1] Gibson Electric Guitar      P8,500.00   |" << endl;
    cout<< "                                                             | [2] Ibanez Electric Guitar      P25,000.00  |" << endl;
    cout << "                                                             =============================================== "<<endl<<endl;
    cout<<"                                                                Choose from the available Electric Guitars:";
    cin>>electric;

        if (electric=='1'){           
        cout<<"Enter the quantity:";
        cin>>Qty;
        cout<<"("<<Qty<<")"<<"Gibson Electric Guitar was added to your cart"<<endl;
        cout<<"Would you like to add more item? (Yes/No)"<<endl;
        cin>>again; 
    }
        else if (electric=='2'){                
        cout<<"Enter the quantity:";
        cin>>Qty;
        cout<<"("<<Qty<<")"<<"Ibanez Electric Guitar was added to your cart"<<endl;
        cout<<"Would you like to add more item? (Yes/No)"<<endl;
        cin>>again;
    }
    else {
        cout<<"Invalid";
    } 

    break;

    default: 
      cout<<"Invalid"<<endl;
 }

 }
 while (again == "Yes" || again == "yes");


  cout<<"***********************************Music Shop***********************************"<<endl;
 cout<<"Name of Item                       Quantity             Total Price       "<<endl;
 cout<<"                                                                                "<<endl;
 return 0;

}
}

用户每次输入yes都可以继续购买,并且在说出no后应该打印收据。

c++
1个回答
-2
投票
#include <iostream>
using namespace std;

char acoustic, electric, drums, piano, music, equip, Yes, No, y, name;

int pay, Qty;
std::string again;

long long tot=0;
int sumA1=0,sumA2;
int sumB1=0,sumB2;
int QtyA1=0,QtyA2=0,QtyB1=0,QtyB2=0;
string   Accoustic1="Fender Acoustic Guitar",Accoustic2="Hartwood Acoustic Guitar ";
string Electric1="Gibson Electric Guitar",Electric2="Ibanez Electric Guitar";

int main()
{

    {
        cout << "Welcome to the Music Shop" << endl <<endl;
        cout << "                                                                         ===========================                            "<<endl;
        cout << "                                                                         |       Categories        |                            "<<endl;
        cout << "                                                                         |  [a] Acoustic Guitar    |                            "<<endl;
        cout << "                                                                         |  [b] Electric Guitar    |                            "<<endl;
        cout << "                                                                         |  [c] Drums              |                            "<<endl;
        cout << "                                                                         |  [d] Piano              |                            "<<endl;
        cout << "                                                                         |  [e] Music Equipments   |                            "<<endl;
        cout << "                                                                         ===========================                            "<<endl;


        do
        {
            cout << "                                                                     Choose from the available categories:";
            cin >> music;
            cout << ""<<endl;

            switch (music)
            {
            case 'a':

                cout << "                                                             =============================================== "<<endl;
                cout<< "                                                             |                Acoustic Guitars             |" << endl;
                cout<< "                                                             |                                             |" << endl;
                cout<< "                                                             |           Product                 Price     |" << endl;
                cout<< "                                                             | [1] Fender Acoustic Guitar      P6,900.00   |" << endl;
                cout<< "                                                             | [2] Hartwood Acoustic Guitar    P6,300.00   |" << endl;
                cout << "                                                             =============================================== "<<endl<<endl;
                cout<<"                                                                Choose from the available Acoustic Guitars:";
                cin>>acoustic;

                if (acoustic == '1')
                {
                    cout<<"Enter the quantity:";
                    cin>>Qty;
                    cout<<"("<<Qty<<")"<<"Fender Acoustic Guitar was added to your cart"<<endl;
                    cout<<"Would you like to add more item? (Yes/No)"<<endl;
                    cin>>again;

                    QtyA1+=Qty;
                    sumA1=sumA1+(6900*Qty);

                }
                else if (acoustic == '2')
                {
                    cout<<"Enter the quantity:";
                    cin>>Qty;
                    cout<<"("<<Qty<<")"<<"Hartwood Acoustic Guitar was added to your cart"<<endl;
                    cout<<"Would you like to add more item? (Yes/No)"<<endl;
                    cin>>again;
                    QtyA2+=Qty;
                    sumA2=sumA2+(6300*Qty);
                }
                else
                {
                    cout<<"Invalid"<<endl;
                }

                break;

            case 'b':

                cout << "                                                             =============================================== "<<endl;
                cout<< "                                                             |                Electric Guitars             |" << endl;
                cout<< "                                                             |                                             |" << endl;
                cout<< "                                                             |           Product                 Price     |" << endl;
                cout<< "                                                             | [1] Gibson Electric Guitar      P8,500.00   |" << endl;
                cout<< "                                                             | [2] Ibanez Electric Guitar      P25,000.00  |" << endl;
                cout << "                                                             =============================================== "<<endl<<endl;
                cout<<"                                                                Choose from the available Electric Guitars:";
                cin>>electric;

                if (electric=='1')
                {
                    cout<<"Enter the quantity:";
                    cin>>Qty;
                    cout<<"("<<Qty<<")"<<"Gibson Electric Guitar was added to your cart"<<endl;
                    cout<<"Would you like to add more item? (Yes/No)"<<endl;
                    cin>>again;
                    QtyB1+=Qty;
                    sumB1=sumB1+(8500*Qty);
                }
                else if (electric=='2')
                {
                    cout<<"Enter the quantity:";
                    cin>>Qty;
                    cout<<"("<<Qty<<")"<<"Ibanez Electric Guitar was added to your cart"<<endl;
                    cout<<"Would you like to add more item? (Yes/No)"<<endl;
                    cin>>again;

                    QtyB2+=Qty;
                    sumB2=sumB2+(25000*Qty);
                }
                else
                {
                    cout<<"Invalid";
                }

                break;

            default:
                cout<<"Invalid"<<endl;
            }

        }
        while (again == "Yes" || again == "yes");


        cout<<"***********************************Music Shop***********************************"<<endl;
        cout<<"Name of Item                       Quantity             Total Price       "<<endl;
        cout<<"                                                                                "<<endl;
        if(QtyA1>0)
            cout<<Accoustic1<<" "<<QtyA1<<" "<<sumA1<<endl;
        if(QtyA2>0)
            cout<<Accoustic2<<" "<<QtyA2<<" "<<sumA2<<endl;
        if(QtyB1>0)
            cout<<Electric1<<" "<<QtyB1<<" "<<sumB1<<endl;
        if(QtyB2>0)
            cout<<Electric2<<" "<<QtyB2<<" "<<sumB2<<endl;

        return 0;

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.