如何正确使用标志控制循环,以便特定任务只执行一次?

How can I properly use a flag controlled loop so that a specific task is performed only once?

我正在制作一个程序,要求用户输入两个有理数,根据用户选择加法还是减法,我的程序将执行任务,然后必须选择继续加法或减法或退出。

到目前为止,我已经完成了大部分编码,但在编写标志控制循环时遇到了问题。对于我的程序,将出现一个显示菜单,询问用户是要减去、添加还是退出。

如果用户选择添加,屏幕将会清空,顶部会出现一个标题 "Addition of rational numbers"。这个标题应该只出现一次,之后我的程序会要求用户输入一个有理数。

由于程序应该添加两个有理数,在用户输入第一个有理数后,我的程序将要求用户输入另一个有理数。

所以我的程序总共会问这个问题两次。

问题: 我遇到的问题是,一旦标题 "Addition of rational numbers" 出现并且我的程序第二次向用户询问数字,标题将再次出现。我尝试使用标志控制循环,但无论我尝试什么,它都会在每次询问问题时跳过标题或重复它。以下是我所指的代码:

void GetRational(char state, int& num, int& den)
{
    char c;
    bool once;
    once = true;

    while (once)
    {
        if (state == 'A' || state == 'a')
        {
            cout << "Addition of rational numbers" << endl;
            once = false;
        }
        else if (state == 'S' || state == 's')
        {
            cout << "Subtraction of rational numbers" << endl;
            once = true;
        }
        else break;
    }

    while (1)
    {
        cout << "\nPlease enter a fraction (n/d): ";
        cin >> num >> c >> den;
        if (den == 0)
            cout << "\nDenominator must not be 0";
        else break;
    }
}

尝试将 bool once 移到函数 GetRational(...) 之外,这可能是您多次调用 GetRational(...) 并且每次 bool once 都可能被重新初始化的情况.所以片段看起来像这样:

bool once = true;
void GetRational(char state, int& num, int& den)
{
    char c;

    while (once)
    {
        if (state == 'A' || state == 'a')
        {
            cout << "Addition of rational numbers" << endl;
            once = false;
        }
        else if (state == 'S' || state == 's')
        {
            cout << "Subtraction of rational numbers" << endl;
            once = true;
        }
        else break;
    }

    while (1)
    {
        cout << "\nPlease enter a fraction (n/d): ";
        cin >> num >> c >> den;
        if (den == 0)
            cout << "\nDenominator must not be 0";
        else break;
    }
}

正如戴维斯洛在评论中指出的那样:

The problem with globals, especially globals that aren’t constants, is that any part of the program can mess around with them. That makes it very hard to track down which line of your program is causing a bug. It’s less of an issue if you split your program into modules and you can at least guarantee that only that one file could have mucked it up.

因此,另一种选择是使用 once 作为静态变量 如果 您不想使用全局变量。

Static variables when used inside function are initialized only once, and then they hold their value even through function calls.

使用静态变量,代码片段如下所示:

void GetRational(char state, int& num, int& den)
{
    // will only be initialized one, at the first time call of function GetRational(...)
    static bool once = true; 
    char c;

    while (once)
    {
        if (state == 'A' || state == 'a')
        {
            cout << "Addition of rational numbers" << endl;
            once = false;
        }
        else if (state == 'S' || state == 's')
        {
            cout << "Subtraction of rational numbers" << endl;
            once = true;
        }
        else break;
    }

    while (1)
    {
        cout << "\nPlease enter a fraction (n/d): ";
        cin >> num >> c >> den;
        if (den == 0)
            cout << "\nDenominator must not be 0";
        else break;
    }
}

详细了解 static 关键字:here

您可以试试这个代码。使用 if 循环。

bool once="true";
     void GetRational(char state, int& num, int& den)
        {
            char c;


                if(once=="true")
               { 
                if (state == 'A' || state == 'a')
                {
                    cout << "Addition of rational numbers" << endl;
                }
                else if (state == 'S' || state == 's')
                {
                    cout << "Subtraction of rational numbers" << endl;

                }
                else { }
          }

            while (1)
            {   
                once="false";
                cout << "\nPlease enter a fraction (n/d): ";
                cin >> num >> c >> den;
                if (den == 0) 
                    cout << "\nDenominator must not be 0";
                else break;
            }
        }

要让 GetRational 在下次调用时记住 once 的值,请将变量声明为

static bool once = true;

static 变量只会初始化一次,即在第一次调用 GetRational 之前。

然后,在你检查之后,在你从GetRationalreturn之前,设置

once = false;

这将在对 GetRational 的调用之间持续存在。