I would imagine that you initialize encryption with asterisks, right? So let's walk through the execution with that assumption in mind:
for ( int i = 0; i < 5; i++ )
You create i and set it to 0. Since i is less than 5, the body of the loop is executed.
if (encryption[i] == '*' )
Since encryption was filled with asterisks, this test is true as the first character is indeed '*'. The body of the condition is executed:
return i;
0 is returned.
You should also be aware that this is incorrect:
if ( i == 4 )
Since i was declared in the for loop, it only exists until the end of the loop. So any conforming compiler will fail to compile this function because i doesn't exist at this point.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
This important part looks like it got lost.
while ( game1.getlives() != 0 && game1.checkstatus() )
This is how I might writecheckstatus.
int hangman::checkstatus()
{
int i;
for ( i = 0; encryption[i] != '\0'; ++i )
{
if ( encryption[i] == '*' )
{
return 1;
}
}
cout<<"You win!"<<endl;
return 0;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314