使用while循环切换语句

时间:2018-04-06 04:41:56

标签: c++ while-loop switch-statement

do{ 
        cout << "Select an option from the Menu:  ";
        cin >> choice;
         // Validate the menu selection
            while ((choice < 1) || (choice > 3)){
                cout << "Incorrect input!, please enter an option from 1 to 3."<<endl;
                cout<<"Enter your choice: ";
                cin >> choice;
            }
        // Processing the users choice
            if (choice != 3){ 
        // Compute conversions
                switch (choice){
                        case 1:
                            cout<<""<<endl; 
                            cout<<"You have selected to convert Fijian Dollars to Vanuatuan Vatu."<<endl;
                            cout<<"Enter the amount you wish to convert: ";
                            cin >> amount;
                            conversion = amount * FJD_to_Vatu_Rate;
                        break;

                        case 2: 
                            cout<<""<<endl;
                            cout<<"You have selected to convert Fijian Dollars to Samoan Tala."<<endl;
                            cout<<"Enter the amount you wish to convert: ";
                            cin >> amount;
                            conversion = amount * FJD_to_Tala_Rate;
                        break;

                        case 3:
                            cout<<"Here the history will be shown"<<endl;
                            cout<<""<<endl;
                            cout<<"Do You want to perform another conversion? (Y/N) ";
                            cin >> repeat;
                                if (repeat == 'Y'){
                                    (This is what i want to know)
                                 }
                            cout<<"Thank you for using this program, goodbye!"<<endl;
                            return 0;

            }
        // Display the monthly charges
            cout << fixed << showpoint << setprecision(2);
            cout << "The converted amount is: " << conversion << endl;
            cout <<""<<endl;
        }
    } while (choice != 3);
    cout<<"Thank you for using this program, goodbye!"<<endl;
return 0;
}

以上是我的货币转换器代码的一部分,任何人都可以告诉我在“这是我想知道的”部分中该做什么,以便程序要求用户输入一个选择它可以处理它并使用选择来使用其他一种情况进行计算。

由于

2 个答案:

答案 0 :(得分:0)

if之前删除switch语句,因为在这种情况下永远不会执行case 3:

do{ 
        cout << "Select an option from the Menu:  ";
        cin >> choice;
         // Validate the menu selection
            while ((choice < 1) || (choice > 3)){
                cout << "Incorrect input!, please enter an option from 1 to 3."<<endl;
                cout<<"Enter your choice: ";
                cin >> choice;
            }
        // Processing the users choice
        // Compute conversions
                switch (choice){
                        case 1:
                            cout<<""<<endl; 
                            cout<<"You have selected to convert Fijian Dollars to Vanuatuan Vatu."<<endl;
                            cout<<"Enter the amount you wish to convert: ";
                            cin >> amount;
                            conversion = amount * FJD_to_Vatu_Rate;
                        break;

                        case 2: 
                            cout<<""<<endl;
                            cout<<"You have selected to convert Fijian Dollars to Samoan Tala."<<endl;
                            cout<<"Enter the amount you wish to convert: ";
                            cin >> amount;
                            conversion = amount * FJD_to_Tala_Rate;
                        break;

                        case 3:
                            cout<<"Here the history will be shown"<<endl;
                            cout<<""<<endl;
                            cout<<"Do You want to perform another conversion? (Y/N) ";
                            cin >> repeat;
                                if (repeat == 'Y'){
                                    (This is what i want to know)
                                 }
                            cout<<"Thank you for using this program, goodbye!"<<endl;
                            return 0;

                        // Display the monthly charges
                        cout << fixed << showpoint << setprecision(2);
                        cout << "The converted amount is: " << conversion << endl;
                        cout <<""<<endl;    
                }
    } while (choice != 3);
    cout<<"Thank you for using this program, goodbye!"<<endl;
    return 0;
}

答案 1 :(得分:0)

要重新启动do-while循环,请使用continue。从逻辑上讲,你应该首先打印历史记录,然后进行问题\检查。

相关问题