Trying to get an assignment done for my programming class, and the teacher doesn't explain what needs to change in my code, just where it went wrong. So, she told me it was somewhere in here.
verify will only be true if spells[35] equals x, because you're always overwriting verify with the result of the next check:
1 2 3 4 5 6 7 8 9 10 11
for(int i = 0; i < 36; ++i)
{
if(spells[i] == x) //Even if this evaluates for some i...
{
verify = true;
}
else
{
verify = false; //It will be over written here for the next i, unless i was 35, in which case the right value will //be returned
}
}
To fix this you should use a break statement after you find the spell:
1 2 3 4 5
if(spells[i] == x)
{
verify = true;
break; //This will "break" you out of the for loop, and execute what comes after the loop.
}
PS: Please use code tags for posting source code. Makes it much more readable.
EDIT:
A better way to do this might be to use an array of bools to store the spells the player can use?
Sorry, new to the site, saw you guys helped people with their coding problems.
And I did try that (thank you for pointing it out btw, saved me a huge headache), but the subscript error still comes up when I try to debug it. Maybe the problem isn't in there, but I don't know where else it would be.
.... maybe here?
1 2 3 4 5 6 7
Avatar::Avatar(string q,int w[],int e, int r):name(q), spells(), spell(e), points(r)
{
for( int i=0; i<spell; i++)
{
spells[i] = w[i];
}
}
I'm trying to initialize the spells array with an input array.
Don't put "spells" in the initialization list. How are you allocating memory for spells? Or is it a static array? Also, is "spell" (Singular) guaranteed to give you the size-1 or less of w and spells? Otherwise you'll be writing outside of an array.
Okay, spells out of the initialization list. spells is defined as spells is an array of int. I'm not sure if spell (singular) would be giving the right number or not. For this assignment we were supposed to write header files and .cpp files for her main.
Her instructions for these two parts I'm having problems with:
constructor initializes the name array with a c-string that is passed in as an argument and initializes the number and points members with integer values passed in as arguments
and
spells -- an array of spells that this avatar may use; no more than 36; each spell is identified by an integer
Sorry if this doesn't answer quite what you were asking, I'm still very new to c++.