ActiveGaming ActiveGaming - 1 month ago 20
C++ Question

Uninitialized Local Variable "name" Used

I keep getting this error in my code, uninitialized local variable "name" used. I set a variable like this


int name = 0;


then later on in the code,


int name = name + 1;


I am trying to set name to 0 then later on add 1 to it then go back to the beginning and still have it equaling name + 1. the problem is it keeps putting it back to 0.

To make it easier here is the part I am talking about.

int main()
{
int w = 0;
int choice;

cout << "1: Register\n2: Login\nYour Choice:"; cin >> choice;

if (choice == 1)
{
string username, password;
cout << "Select A Username: "; cin >> username;
cout << "Select A Password: "; cin >> password;

ofstream file;
file.open("data\\" + username + ".txt");
file << username << endl << password;
file.close();

main();
}
else if (choice == 2)
{
bool status = IsLoggedIn();

if (!status)
{
int w = w + 1;
cout << "Unsuccesfull Login!" << endl;
system("PAUSE");

if (w == 3)
{
return 0;
}
else if (w == 2)
{
main();
}
else if (w == 1)
{
main();
}
else if (w == 0)
{
main();
}
}
else
{
cout << "Succesfully Logged In!" << endl;
system("PAUSE");
return true;
}
}
}`

Answer

C++ complains when you try to declare two variables with the same name in the same scope.

Basically int name; creates the variable name. You can then address it by name without int.

You probably mean int name = 0; [...] name = name + 1;.