Как я могу повторить весь код, пока пользователь не выйдет? - PullRequest
0 голосов
/ 02 октября 2019

Как мне зациклить "Хотели бы вы купить что-нибудь еще?"он находится внизу большей части кода, после ответа «Да», затем он снова вернется к выбору из доступных категорий.

Я снова установлю переключатель после того, как узнаю, как можно зациклить этот

#include <iostream>
using namespace std;

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

int pay1, pay2, pay3, pay4, pay5;
std::string answer;

int main()
{
cout << "Welcome to the Music Shop" << endl <<endl;

cout << "Categories                    Products                Price"<<endl;
cout << "[a] Acoustic Guitar      Fender Acoustic Guitar     P6,900.00"<< endl;
cout << "                         Hartwood Acoustic Guitar   P6,300.00" << endl;

cout << "[b] Electric Guitar      Gibson Electric Guitar     P8,500.00" << endl;
cout << "                         Ibanez Electric Guitar     P25,000.00" << endl;

cout << "[c] Drums                Pearl Drum Kits            P27,000.00" << endl;
cout << "                         Roland Electronic Drums    P24,000.00" << endl;

cout << "[d] Piano                Casio Digital Piano        P19,000.00" << endl;
cout << "                         Roland Piano               P120,000.00" << endl;

cout << "[e] Music Equipments     Guitar String              P113.00" << endl;
cout << "                         Headphones                 P1,600.00" << endl;
cout << "                         Amplifier                  P2,800.00" << endl;
cout << "                         Digital Mixer              P4,750.00" << endl;
cout << "                         Vocal Microphone           P860.00" << endl;
cout << "                         Guitar Tuner               P537.00" << endl;
cout << "                         Guitar Pick                P360.00" << endl<<endl;

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

switch (music)
{
    case 'a':

    cout<< "Acoustic Guitar" << endl;
    cout<< "1. Fender Acoustic Guitar   - P6,900.00" << endl;
    cout<< "2. Hartwood Acoustic Guitar - P6,300.00" << endl <<endl;
    cout<<"Choose from the available Acoustic Guitars:";
    cin>>acoustic;

    if (acoustic == '1'){
        cout<<"Enter your payment:";
        cin>>pay1;
        if (pay1==6900){
            cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl;
        }
            else if (pay1>6900){
                pay1 -= 6900;
                cout<<"Your change is:"<<pay1<<endl;
            }
                else if (pay1<6900){
                    cout<<"You do not have enough money"<<endl;
                }
    }
    else if (acoustic == '2'){
        cout<<"Enter your payment:";
        cin>>pay1;
        if (pay1==6300){
            cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl;
        }
            else if (pay1>6300){
                pay1 -= 6300;
                cout<<"Your change is:"<<pay1<<endl;
            }
                else if (pay1<6300)
                {
                    cout<<"You do not have enough money"<<endl;
                }
    }
    else {
        cout<<"Invalid"<<endl;
    }

    break;

    case 'b':

    cout<< "Electric Guitar" << endl;
    cout<< "1. Gibson Electric Guitar   -P8,500.00" << endl;
    cout<< "2. Ibanez Electric Guitar   -P25,000.00" << endl <<endl;
    cout<<"Choose from the available Electric Guitars:";
    cin>>electric;

        if (electric=='1'){
        cout<<"Enter your payment:";
        cin>>pay2;
        if (pay2==8500){
            cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl;
        }
            else if (pay2>8500){
                pay2 -= 8500;
                cout<<"Your change is:"<<pay2<<endl;
            }
                else if(pay2<8500)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (electric=='2'){
        cout<<"Enter your payment:";
        cin>>pay2;
        if (pay2==25000)
        {
            cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl;
        }
            else if (pay2>25000){
                pay2 -= 25000;
                cout<<"Your change is:"<<pay2<<endl;
            }
                else if (pay2<25000)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else {
        cout<<"Invalid";
    } 
        break;

    case 'c':

    cout<< "Drums" << endl;
    cout<< "1. Pearl Drum Kits          -P27,000.00" << endl;
    cout<< "2. Roland Electronic Drums  -P24,000.00" << endl <<endl;
    cout<<"Choose from the available Drums:";
    cin>>drums;

        if (drums=='1'){
        cout<<"Enter your payment:";
        cin>>pay3;
        if (pay3==27000){
            cout<<"You have succesfully purchased Pearl Drum Kits"<<endl;
        }
            else if (pay3>27000){
                pay3 -= 27000;
                cout<<"Your change is:"<<pay3<<endl;
            }
                else if(pay3<27000)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (drums=='2'){
        cout<<"Enter your payment:";
        cin>>pay3;
        if (pay3==24000)
        {
            cout<<"You have succesfully purchased Rolans Electronic Drums"<<endl;
        }
            else if (pay3>24000){
                pay3 -= 24000;
                cout<<"Your change is:"<<pay3<<endl;
            }
                else if (pay3<25000)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else {
        cout<<"Invalid";
    }
     break;

     case 'd':

    cout<< "Piano" << endl;
    cout<< "1. Casio Digital Piano      -P19,000.00" << endl;
    cout<< "2. Roland Piano             -P120,000.00" << endl <<endl;
    cout<<"Choose from the available Piano:";
    cin>>piano;

        if (piano=='1'){
        cout<<"Enter your payment:";
        cin>>pay4;
        if (pay4==19000){
            cout<<"You have succesfully purchased Casio Digital Piano"<<endl;
        }
            else if (pay4>19000){
                pay4 -= 19000;
                cout<<"Your change is:"<<pay4<<endl;
            }
                else if(pay4<19000)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (piano=='2'){
        cout<<"Enter your payment:";
        cin>>pay4;
        if (pay4==120000){
            cout<<"You have succesfully purchased Roland Piano"<<endl;
        }
            else if (pay4>120000){
                pay4 -= 120000;
                cout<<"Your change is:"<<pay4<<endl;
            }
                else if(pay4<120000)
                {
                    cout<<"You do not have enough money"<<endl;
                }
    }
    else {
        cout<<"Invalid";
    }
     break;

     case 'e':

    cout<< "Music Equipments" << endl;
    cout << "1. Guitar String            -P113.00" << endl;
    cout << "2. Headphones               -P1,600.00" << endl;
    cout << "3. Amplifier                -P2,800.00" << endl;
    cout << "4. Digital Mixer            -P4,750.00" << endl;
    cout << "5. Vocal Microphone         -P860.00" << endl;
    cout << "6. Guitar Tuner             -P537.00" << endl;
    cout << "7. Guitar Pick              -P360.00" << endl <<endl;

    cout<<"Choose from the available Music Equipments:";
    cin>>equip;

        if (equip=='1'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==113){
            cout<<"You have succesfully purchased Guitar String"<<endl;
        }
            else if (pay5>113){
                pay5 -= 113;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<113)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='2'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==1600){
            cout<<"You have succesfully purchased Headphones"<<endl;
        }
            else if (pay5>1600){
                pay5 -= 1600;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<1600)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='2'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==1600){
            cout<<"You have succesfully purchased Headphones"<<endl;
        }
            else if (pay5>1600){
                pay5 -= 1600;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<1600)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='3'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==2800){
            cout<<"You have succesfully purchased Amplifier"<<endl;
        }
            else if (pay5>2800){
                pay5 -= 2800;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<2800)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='4'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==4750){
            cout<<"You have succesfully purchased Digital Mixer"<<endl;
        }
            else if (pay5>4750){
                pay5 -= 4750;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<4750)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='5'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==860){
            cout<<"You have succesfully purchased Vocal Microphone"<<endl;
        }
            else if (pay5>860){
                pay5 -= 860;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<860)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='6'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==537){
            cout<<"You have succesfully purchased Guitar Tuner"<<endl;
        }
            else if (pay5>537){
                pay5 -= 537;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<537)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (equip=='7'){
        cout<<"Enter your payment:";
        cin>>pay5;
        if (pay5==360){
            cout<<"You have succesfully purchased Guitar Pick"<<endl;
        }
            else if (pay5>360){
                pay5 -= 360;
                cout<<"Your change is:"<<pay5<<endl;
            }
                else if(pay5<360)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else {
        cout<<"Invalid";
    }
}

 cout<<"\nWould you like to buy something else?"<<endl;
 cout<<"Yes or No?"<<endl;
 cin>>answer;

 if (answer == "Yes"){
 cout<<"Choose from the available 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; 
 cin >> music;

 switch(music){
 }
}
else if (answer == "No"){
        cout<<"Thank you for visiting our shop come again";
}
}

Нужна петля, чтобы я мог продолжать покупать каждый раз, когда выбираю Да

Ответы [ 2 ]

3 голосов
/ 02 октября 2019

В любом случае, вам понадобится петля.

Есть несколько вариантов, и выбор одного из них - это скорее дело вкуса ...

bool isContinue = true;
while(isContinue)
{
    // ...
    std::string input;
    // get input, convert to upper case for case-insensitive comparison
    isContinue = input == "YES"; 
}

Почти то же самое, но сначала пропускаем (ненужное сравнение):

bool isContinue = false; // initial value is irrelevant, will be overwritten anyway
do
{
    // ...
    isContinue = input == "YES"; 
}
while(isContinue);

или, казалось бы, бесконечный цикл:

for(;;)
{
    // ...
    if(input != "YES")
        break;
}

Последний вариант особенно полезен, если вам нужнорешить прямо в середине тела цикла, нужно ли останавливаться:

for(;;)
{
    // some initial work
    if(doWeNeedToStop)
        break;
    // some work to be done if we did NOT stop
}

Еще интересно: где разместить цикл? Ну, вокруг всего, что вы хотите повторить, так:

int main()
{
    // stuff you want to do only once at programme start,
    // e. g. welcome message
    for(;;)
    {
        // stuff you want to repeat
    }
    // stuff you want to do only once before programme ends,
    // e. g. printing a good-bye message

    return 0;
}
1 голос
/ 02 октября 2019

Очень ужасным, но очень быстрым способом вы можете использовать операторы label и goto;вот doc .

Если вы хотите быть чище, вы можете реализовать цикл do-while, который проверяет ответ;как это:

do {
    // do your stuff
} while (answer == "Yes");

std::cout << "Thank you for visiting our shop come again";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...