返回

c++-设置标志,但不知怎么地设置为True

发布时间:2022-05-10 14:57:05 245
# golang

全部的在切换到指针之前,我一直在与C++打交道,试图了解在本地函数中实例化多个对象是如何工作的。

我这里有一个逻辑错误,我无法理解。不知何故,DEBUG_标志被设置为true,即使在文本文件中它在0和1之间翻转。

我已经进行了很多次代码调试,但这里缺少了一些东西,可以解释DEBUG_标志是如何设置为true的,即使在文本文件中它显示为false。

#include 
#include 
#include 
#include 
using namespace std;

bool isDebugFlagSet(int);
void setDebugFlag(int);

static bool DEBUG_FLAG;

int main() {

    char c;
    int iDebugCount = 0;
    int i = 1;
    bool bFlag = false;
    
    for (;;)
    {
        //load debug flag from file
        bFlag = isDebugFlagSet(iDebugCount);

        //Options
        cout << "\n";
        cout << "\tOption - 1\n";
        cout << "\tOption - 2\n";
        cout << "\tOption - 3\n";
        cout << "\tOption - 4\n";
        cout << "\tExit - 5\n";
        cout << "\tEnable Debug Settings - 6\n";
        cout << "\nPlease enter your choice: ";
        cin >> c;

        //validation
        if (!c == '1' || 
            !c == '2' || 
            !c == '3' || 
            !c == '4' || 
            !c == '5' || 
            !c == '6')
        {
            cin.clear();
            main();
        }

        //dir choice
        switch (c)
        {
        case '1':
            cout << "\nOption 1 selected\n";
            break;
        case '2':
            cout << "\nOption 2 selected\n";
            break;
        case '3':
            cout << "\nOption 3 selected\n";
            break;
        case '4':
            cout << "\nOption 4 selected\n";
            break;
        case '5':
            cout << "\nOption 5 selected\n";

            //if exiting need to make sure debug information is written to file
            setDebugFlag(iDebugCount);

            //exit
            return 0;
        case '6':
            setDebugFlag(iDebugCount);
            //counter to detect how many times user has selected debug logic
            iDebugCount++;
            if (DEBUG_FLAG == true) 
            {
                cout << "\nDebug Settings Enabled\n";
            }
            else 
            {
                cout << "\nDebug Settings Disabled\n";
            }
        default:
            c = '0';
        }
        //Debug
        if (!DEBUG_FLAG == NULL && 
            bFlag == true) 
        {
            cout << "\n*** DEBUG - COUNTER DATA " << i << " ***\n";
        }
        i++;
    }
    return 0;
}


bool isDebugFlagSet(int nFlagCounter) 
{
    /*
    * GOAL: check if debug flag is set
    */
    //open reading file
    string sLine;
    string strFlag;
    ifstream fRead("C:\\Users\\P\\AppData\\Local\\Playground\\FlagSettings.txt");
    //attempt to read file. if nothing exists and no data is there
    //we default the flag to false
    if (fRead.is_open() && !fRead == NULL)
    {
        while (getline(fRead, sLine))
        {
            if (DEBUG_FLAG == true) 
            {
                cout << "*** DEBUG - FILE DATA " << sLine << " ***\n";
            }

            else if (!DEBUG_FLAG ? true : false) 
            {
                setDebugFlag(nFlagCounter);
            }
            DEBUG_FLAG = sLine.at(0);
        }
    }
    fRead.close();
    return DEBUG_FLAG;
}

void setDebugFlag(int nCounter) 
{
    ofstream fWrite("C:\\Users\\P\\AppData\\Local\\Playground\\FlagSettings.txt");
    if (fWrite.is_open())
    {
        if (nCounter % 2 == 0) {
            fWrite << false;
        }
        else
        {
            fWrite << true;
        }
    }
    fWrite.close();
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像