保持程序处于主循环

Keeping the program on the main loop

本文关键字:于主 循环 程序      更新时间:2023-10-16

我有一个简单的程序,它可以进行多次转换,但每次操作后都会脱离循环。如何在每个函数完成执行后调整程序以返回主循环。

#include <iostream>
#define TRUE 1
using namespace std;
int main(){
    char *str = new char[25];
    cout<< "What do you want to convert?" <<endl;
    cin>> str;
    while (TRUE){

        if (strcasecmp(str, "TEMPERATURE") == 0)
        {
            convert_temp(str);
        }
        if (strcasecmp(str, "WEIGHT") == 0)
        {
            convert_weight(str);
        }
        if (strcasecmp(str, "DISTANCE") == 0)
        {
            convert_distance(str);
        }
    }
    return EXIT_SUCCESS;
}
void convert_temp(char* str){
        cout<< "Press 1 for C to F | Press 2 for F to C" <<endl;
            int temp = 0;
            cin >> temp;
            if(temp == 1){
                int c, f = 0;
                cout<< "Enter the value" <<endl;
                cin >> c;
                f = (9/5)*c - 32;
                cout<< "The temperature is " << f << " degree F"<< endl;
            }else if (temp == 2){
                int c, f = 0;
                cout << "Enter the value"<<endl;
                cin >> f;
                c = (f-32)*(5/9);
                cout << "The temperature is " << c <<  " degree C"<<endl;
            }
            break;
}
void convert_weight(char* str){
    cout<< "Press 1 for POUNDS to KGS | Press 2 for KGS to POUNDS" <<endl;
            int temp = 0;
            cin >> temp;
            if(temp == 1){
                int k, p = 0;
                cout<< "Enter the value" <<endl;
                cin >> p;
                k = p/2.2046;
                cout<< "Your weight is " << k << " Kgs"<< endl;
            }else if (temp == 2){
                int k, p = 0;
                cout << "Enter the value"<<endl;
                cin >> k;
                p = k*2.2046;
                cout << "Your weight is " << p <<  " Pounds"<<endl;
            }
            break;
}
void convert_distance(char* str){
        cout<< "Press 1 for KM to MILES | Press 2 for MILES to KM" <<endl;
            int temp = 0;
            cin >> temp;
            if(temp == 1){
                int k, m = 0;
                cout<< "Enter the value" <<endl;
                cin >> k;
                m = 32*k;
                cout<< "The distance is " << k << " Miles"<< endl;
            }else if (temp == 2){
                int k, m = 0;
                cout << "Enter the value"<<endl;
                cin >> m;
                k = 10*m;
                cout << "The distance is " << k <<  " Kms"<<endl;
            }
            break;
}

因此,我的问题是如何用对主循环的调用来替换break语句。我不想按顺序运行函数,将所有函数插入主循环。

您的代码应该更像这样:

#include <iostream>
#include <string>
void convert_temp();
// Other prototypes elided
int main() {
    std::string str;
    for(;;) { // I use this in my coding standard to mean 'forever' instead of while(true)
        std::cout << "What do you want to convert?" << std::endl;
        std::cin >> str;
        if (str == "TEMPERATURE") {
            convert_temp();
        }
        if (str == "WEIGHT") {
            convert_weight();
        }
        if (str == "DISTANCE") {
            convert_distance();
        }
        if (str == "EXIT") {
            break;
        }
    }
    return 0;
}
void convert_temp() {
    std::cout << "Press 1 for C to F | Press 2 for F to C" << std::endl;
    int temp = 0;
    std::cin >> temp;
    std::cout << "Enter the value" << std::endl;
    int c, f = 0;
    if (temp == 1) {
        cin >> c;
        f = (9/5)*c - 32;
        std::cout << "The temperature is " << f << " degree F" << std::endl;
    } else if (temp == 2) {
        std::cin >> f;
        c = (f-32)*(5/9);
        std::cout << "The temperature is " << c <<  " degree C" << std::endl;
    }
}
// Other function definitions elided

注意,作为C++,我已经将std::string用于主选择的输入。

main()函数现在循环询问"您想转换什么?"每次。

该参数不再传递给任何函数,因为它们知道自己在做什么,不需要使用该值。

示例convert_temp()函数已被稍微简化,并且break;已被移除,因为这在这样的普通函数中没有任何作用。

还要注意输入"EXIT"是如何退出程序的。

您可以使用exit(0);退出整个程序。(无论如何,中断循环都会导致进程关闭)

如果您希望继续主循环,则不需要在代码中使用break语句。中断表示退出当前控制指令(对于/开关)。您可以用continue;替换break语句,但在这种情况下,这相当于没有continue语句。