如何重复整个代码,直到用户退出?

时间:2019-10-02 08:46:29

标签: c++ loops

我该如何循环播放“您是否还想购买其他商品?”它位于代码的最底部,在回答“是”之后,它将再次从可用类别中进行选择。

我知道如何循环后再重新放置开关

#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 个答案:

答案 0 :(得分:3)

无论如何,您需要循环。

有几种选择,选择哪个只是一个口味问题...

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 :(得分:1)

以一种非常糟糕但非常快速的方式,您可以使用labelgoto语句;这是doc

如果您想变得更清洁,则可以实现一个do-while循环来检查答案;像这样:

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

std::cout << "Thank you for visiting our shop come again";