Problem with struct member vars

So I thought I had a solid understanding of structs, but I'm running into some problems. I have a struct called charTraits which holds 3 int values for HP, defense, and attack. Depending on which character the player chooses, I want to create a variable of this type with different values assigned to the member variables. But when I try to modify these values later on (within the same function), I get a compiler error saying my struct variable is undeclared. It has clearly been declared within the same function, and has even been accessed successfully only a few lines above. What is the problem? I would just use individual variables instead of a struct but this project calls for the use of a struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (charChoice == 1)
{
	charName = "Elsa";
	charTraits stats; // Declaring the variable of type charTraits
	stats.hitPoints = 90; // Assigning values to its member vars
	stats.defense = 80;
	stats.attack = 120;

	cout << string( 80, '\n' )
		 << "You have chosen Elsa. Your stats are as follows:\n\n"
		 << "    Health: " << stats.hitPoints << ".\n" // Works fine
		 << "    Defense: " << stats.defense << ".\n" // Works fine
		 << "    Attack: " << stats.attack << ".\n\n" // Works fine
		 << "Enter 'y' to continue...  ";
	cin >> continuePlay;
}


And then later on in the function:

1
2
3
4
5
6
7
if(battleResult == 0)
{
	cout << string( 80, '\n' )
		 << "Victory!\n\n"
		 << "HP: " << stats.hitPoints << endl // This breaks the program
		 << "Enter 'y' to continue...  ";
}


Those are the specific pieces of code causing the problem. I would post the entire code but it would be too long. Let me know if you'd like to see anything else.
The charTraits object "stats" is out of scope in the second if control structure, and therefore, is not seen or considered by the compiler.
In other words, your problem has to do with the fact that the life of the stats object is bound to the localized scope of your first if control structure. As soon as the control structure ends, all variables that were created within it will cease to exist. This is standard stack behavior.

A simple work around would be to create the stats object in a more globalized scope, so that the life time of the stats object is not bound to the control structures.

Here's an example:

1
2
3
4
5
6
7
8
9
10
void test() {
	if(/*something*/) {
		Object object;
		object.foo();
	}//object ceases to exist

	if(/*something else*/) {
		object.bar();//error! undeclared identifier
	}
}


Would be fixed by doing:

1
2
3
4
5
6
7
8
9
10
11
void test() {
	Object object;

	if(/*something*/) {
		object.foo();
	}

	if(/*something else*/) {
		object.bar();
	}
}
Last edited on
The braces of the if statement create a new scope. Local variables are destroyed at the end of the scope in which they are defined.
From what I see you create 'stats' in a scope, and try to access it in another scope defined outside the first. When the first scope ends 'stats' doesn't exist anymore.
I see, so assigning it must be done outside the scope but modifying its values can happen in more specific scopes? Thanks for your responses.
Topic archived. No new replies allowed.