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.
CodePudding user response:
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.
CodePudding user response:
Maybe you should just initialize it with
bool count = false;