1

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.

  • 2
    What value does `count` have when/if the stream extraction `while (readL >> id >> pass)` fails on inception? Don't know? Neither does your code, and that's the problem. And fwiw, that while loop is messed up anyway, Assuming the stream *did* open and the values *did* extract correctly, `count` will always reflect the *last* pair read. Unless it matches, the result will always be false. – WhozCraig Mar 19 '22 at 22:56
  • Your loop is broken, it will only set `count` to `true` if the absolute last `sId` is equal to `suserId`. Every iteration before that, the value gets overwritten by the next iteration. It looks like you need to end your loop when a match is found. – François Andrieux Mar 19 '22 at 23:00
  • `main();` note that it's against the rules of the language to call int main(). Related: [https://stackoverflow.com/questions/2532912/call-main-itself-in-c](https://stackoverflow.com/questions/2532912/call-main-itself-in-c) – drescherjm Mar 19 '22 at 23:42

2 Answers2

5

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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
2

Maybe you should just initialize it with

bool count = false;
okatarismet
  • 276
  • 2
  • 5
  • In that case, I am always getting LOGING error despite the fact that I entered registered Username and password – Denys Topchaniuk Mar 19 '22 at 22:57
  • 3
    @DenysTopchaniuk That is a different error, unrelated to the one you are asking about here. Often, fixing a problem exposes the next problem, but it doesn't mean the fix for the original problem is bad. – François Andrieux Mar 19 '22 at 22:58
  • its an error from code itself. not debugging error. In case I initialize it with "false" from the start. In second IF statement it's always goes to else statement. `else { cout << "\nLOGING error\n\nPlease check your username and password\n\n\n"; main(); }` – Denys Topchaniuk Mar 19 '22 at 23:01