使用 while 循环切换语句

Switch statements with while loops

本文关键字:语句 循环 while 使用      更新时间:2023-10-16
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;
}

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

谢谢

switch之前删除if语句,因为在这种情况下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;
}

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