我收到变量“count”的错误没有被初始化。但它应该在读取和验证文件中的数据后进行初始化。 C++

发布于 2025-01-15 11:13:35 字数 1690 浏览 3 评论 0原文

我正在开发登录/注册系统。到目前为止,我收到的错误是变量“count”在未初始化的情况下被使用。

bool count;
string userId, password, id, pass;
system("cls");
cout << "\t\t\n Please enter the username and password\n\n";
cout << "Username:";
cin >> userId;
cout << "Password:";
cin >> password;
//reads info from the file
ifstream readL("record.txt");
while (readL >> id >> pass) {
    if (id == userId && pass == password) {
        count = true;
    }
    else {
        count = false;
    }
}
readL.close();

if (count == true) {
    cout << userId << " your LOGIN is successfull.\n\n";
    main();
}
else {
    cout << "\nLOGING error\n\nPlease check your username and password\n\n\n";
    main();
}

我有代码的第二部分,相同的系统在这里工作。 `

case 1: {
        bool count;
        string suserId, sId, spass;
        cout << "\n\nEnter the username that you remember:";
        cin >> suserId;
        //reads the file
        ifstream f2("records.txt");
        while (f2 >> sId >> spass) {
            if (sId == suserId) {
                count = true;
            }
            else {
                count = false;
            }
        }
        f2.close();
        if (count == true) {
            cout << "\n\n\tYour account is found!\n\nYour password is " << spass << endl << endl;
            main();
        }
        else {
            cout << "\n\n\tSorry your account is not found." << endl << endl;
            main();
        }
        break;
}

`

唯一的区别是,在第一种情况下,它在 while if 语句中读取两个变量,在第二种情况下,仅读取用户名。 但即使我在第一种情况下只读取用户名,错误仍然出现。

I am working on Login/Registration system. So far I am getting the error that the variable "count" is used without being initialized.

bool count;
string userId, password, id, pass;
system("cls");
cout << "\t\t\n Please enter the username and password\n\n";
cout << "Username:";
cin >> userId;
cout << "Password:";
cin >> password;
//reads info from the file
ifstream readL("record.txt");
while (readL >> id >> pass) {
    if (id == userId && pass == password) {
        count = true;
    }
    else {
        count = false;
    }
}
readL.close();

if (count == true) {
    cout << userId << " your LOGIN is successfull.\n\n";
    main();
}
else {
    cout << "\nLOGING error\n\nPlease check your username and password\n\n\n";
    main();
}

I have second part of the code and same system works here. `

case 1: {
        bool count;
        string suserId, sId, spass;
        cout << "\n\nEnter the username that you remember:";
        cin >> suserId;
        //reads the file
        ifstream f2("records.txt");
        while (f2 >> sId >> spass) {
            if (sId == suserId) {
                count = true;
            }
            else {
                count = false;
            }
        }
        f2.close();
        if (count == true) {
            cout << "\n\n\tYour account is found!\n\nYour password is " << spass << endl << endl;
            main();
        }
        else {
            cout << "\n\n\tSorry your account is not found." << endl << endl;
            main();
        }
        break;
}

`

The only difference that in the first case it reads two variables during the while if statement, in second only Username.
But even if I am going to read only Username in the first case error is still appearing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随波逐流 2025-01-22 11:13:35

您的 C++ 编译器足够聪明,可以判断出如果文件无法打开或为空,则 while 循环永远不会执行一次,并且 count 保持未初始化状态,直到其值为止在循环后进行检查。这就是你的 C++ 编译器告诉你的。

仅仅因为输入文件存在或不为空,并且其内容有效(因为其中的垃圾也会导致初始尝试读取它失败)并不重要。从逻辑上讲,当使用 count 的值时,它可能未初始化,因此编译器会进行诊断。

PS 由于不同的原因,while 循环的逻辑也存在致命缺陷。但这与您询问的编译器诊断无关。

Your C++ compiler is smart enough to figure out that if the file could not be opened or is empty, the while loop never executes even once, and count remains uninitialized until its value is checked after the loop. That's what your C++ compiler is telling you.

Just because the input file exist or is not empty, and its contents are valid (because garbage in it will also result in the initial attempt to read it fail) is immaterial. It is logically possible for count to be uninitialized when its value gets used, hence your compiler's diagnostic.

P.S. the while loop's logic is also fatally flawed, for a different reason. But that's unrelated to the compiler diagnostic you asked about.

情感失落者 2025-01-22 11:13:35

也许你应该用以下命令初始化它

bool count = false;

Maybe you should just initialize it with

bool count = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文