int hangman::checkstatus()
{
   for ( int i = 0; i < 5; i++ )
   {
      if (encryption[i] == '*' )
      {
      return i;
      cout << i;
      break; 
      }
   }

   if ( i == 4 ) 
   {
   cout<<"You win!"<<endl;

   }
   return 0;
}

Hello guys, this function always returns a '0' and can't figure out why...... Perhaps someone could shread some light into the situation? It's ment to check if an element in the array is = to '*' and if so return that number, else it will assume i == 4 (gone through the array and now hit the end) and return a '0' in which case main as an if statment asking this.........

here is main

int main()
{
char guess;   
hangman game1;

cout << "Welcome to hangman alpha ver 1.2   " ;
cout <<"You start off with : "<<game1.getlives() << " lives \n " << endl;

cout << game1.checkstatus();
   
while((game1.getlives() !=0 ) && (game1.checkstatus() > 0))
{
   cout << game1.getencrption() <<endl;

   cin >> guess ;

   game1.getletter(guess);


}
   


   return 0;
}

well have fun! its been bugging me for hours!

int hangman::checkstatus()
{
   for ( int i = 0; i < 5; i++ )
   {
      if (encryption[i] == '*' )
      {
      return i;
      [b]cout << i;[/b]
      [b]break;[/b] 
      }
   }

   if ( i == 4 ) 
   {
   cout<<"You win!"<<endl;

   }
   return 0;
}

Maybe you can tell me how one can return (jump) out of this function and then execute:

..
      cout << i;
      break; 
..

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.

hello, I thought I best post the entire code:

#include <iostream>

#include <cstring>

using namespace std;

class hangman
{
public:
   hangman();

   char * getname();
   char * getencrption();
   void getletter(char letter);

   void looselife();

   int getlives();
   int checkstatus();

private:

   char name[20];
   char encryption[20];
   int lives;
   int total;

};

hangman::hangman()
{
   strcpy(name,"John");
   strcpy(encryption, "****");
   lives = 5;
   total = 4;
}

char * hangman::getname()
{
   return name;
}

char * hangman::getencrption()
{
   return encryption;
}

void hangman::looselife()
{
   --lives;
   cout << "You now have "<< lives << " lives remaining "<<endl;
}

void hangman::getletter(char letter)
{
   for (int i = 0 ;i < 4 ; i++)
   {
      if( letter ==name[i] )
      {
        encryption[i] = name[i];

      break;
      }     
   }

   if (i == 4)
      {
         cout << "not found" <<endl;
         looselife();
      }
}

int hangman::getlives()
{  
   return lives;   
}

int hangman::checkstatus()
{
   int i;

   for (  i = 4; i > 0; i--)
   {
      if (encryption[i] == '*' )
      {
      return i;
   
      break; 
      }
   }

   if ( i == 0 ) 
   {
   cout<<"You win!"<<endl;
   }
   return 0;
}

int main()
{
char guess;   
hangman game1;

cout << "Welcome to hangman alpha ver 1.2   " ;
cout <<"You start off with : "<<game1.getlives() << " lives \n " << endl;

cout << game1.checkstatus();
   
while(game1.getlives() !=0 ) 
{
   cout << game1.getencrption() <<endl;

   cin >> guess ;

   game1.getletter(guess);


}
   if (game1.getlives() == 0)
   {
      cout << "You've lost the game!" <<endl;
   }

   return 0;
}

it still doesnt work and can't figure it out! If a player guess correctly it doesnt finish the game.....

This line (line 66 ) won't compile for me because i only exists in the scope of the for loop above it..

if (i == 4)

Also, line 88 - the break doesn't make any sense - you'll never get there - once you do the return, the entire function ends.

This line (line 66 ) won't compile for me because i only exists in the scope of the for loop above it..

if (i == 4)

Also, line 88 - the break doesn't make any sense - you'll never get there - once you do the return, the entire function ends.

I think they're really waiting for you to post back the correct working code ;-)

unlikely. I've got enough questions waiting for others to answer...

int hangman::checkstatus()
{
   int i;

   for (  i = 4; i > 0; i--)
   {
      if (encryption[i] == '*' )
      {
      return i; 
      }
   }

   if ( i == 0 ) 
   {
   cout<<"You win!"<<endl;
   }
   return 0;
}

Hello , here's the fixed verision..... however since an array gets its indexs at '0' then I dont think implmenting it this way is gonna work....anyone get any beter ideas?

This important part looks like it got lost.

while ( game1.getlives() != 0 && game1.checkstatus() )

This is how I might write checkstatus.

int hangman::checkstatus()
{
   int i;
   for ( i = 0; encryption[i] != '\0'; ++i )
   {
      if ( encryption[i] == '*' )
      {
         return 1;
      }
   }
   cout<<"You win!"<<endl;
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.