Got bored, and whats better than spending your time learning. So I'd thought I'd create something fun,

#include <iostream>

using namespace std;


int main()
{
    char array[5] = "John"; //chosen hangman
    char array2[5]; //populates with *

    char key; //ets a key from the user
    int life = 5;
    int correct = 0; // flag to 1 if char is in .
    int location = 10; //used to determine where the location is

     for (int i = 0 ; i < 5 ; i++)
         {
            array2[i] = '*'; //makes the *
         }

     for (int j = 0 ; j < 4 ; j++)
         {
            cout << array2[j]; //prints the *'s
         }

   while (life !=0) //if lives are greater than 0 carry on else don't
   {
    cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl;

     cout << "guess is :  "  ;
     cin >>key; // grab key from user


         for( int guess = 0 ; guess < 5; guess++)
            if (array[guess] == key)
            {
              correct = 1; //if the element is found flag a 1 else flag a zero
              location = guess;
            }
           cout << location;
             if (correct ==1)
             {
               cout << "Congradulations, you've found a letter"<<endl;
             }
               else
               {
                life --;
                cout << "Woah, you just lost a life! , You now have only "<< life << " lives remaining" << endl;
               }
     }//while loop


    
  
   return 0;
}

but now I've managed to dig a little hole for myself, once the user as found the chosen char, I want the '*' to be replaced by the found char... Any ideas?

Just print array2 for each iteration of the loop. When the user chooses a letter, do a search in array, and if you find it, replace the corresponding index in array2 with the letter.

based from what you said, I built this:

char array3[6] = "Hello";
    char array4[6] = "*****";
char keycode = 'H';
    {
       for (int k = 0; k < 5; k++)
       {
       if (array3[k] = keycode)
       {
          array3[k] = array4[k];
       }
    }
    }

    cout << array4;

but doesnt do the job , any other ideas?

>any other ideas?
Sure, but the last idea works just fine for me:

#include <iostream>
#include <string>

using namespace std;

class HangMan {
public:
  HangMan ( const string& init );
public:
  int guess ( const char c );
  bool found() const { return mask.find ( '*' ) == string::npos; }
  void show() const { cout<< mask <<endl; }
private:
  string word;
  string mask;
  int guesses;
};

HangMan::HangMan ( const string& init )
  : word ( init ), mask ( init.length(), '*' ), guesses ( 0 )
{}

int HangMan::guess ( const char c )
{
  string::size_type i = 0;

  while ( ( i = word.find ( c, i ) ) != string::npos ) {
    mask[i] = word[i];
    ++i;
  }

  return ++guesses;
}

int main()
{
  HangMan h ( "hello" );

  for ( ; ; ) {
    char c;

    h.show();

    cout<<"Guess a letter: ";
    cin.get ( c ); cin.ignore();

    if ( h.guess ( c ) == 10 ) {
      cout<<"You lose"<<endl;
      break;
    } else if ( h.found() ) {
      cout<<"You win!"<<endl;
      break;
    }
  }
}

hat was well written, but understood none, hehe
This is what I have:

#include <iostream>

using namespace std;


int main()
{
    char array[5] = "John"; //chosen hangman
    char array2[5]; //populates with *

    char key; //gets a character from the user
    int life = 5;

     for (int i = 0 ; i < 5 ; i++)
         {
            array2[i] = '*'; //makes the *
         }

     for (int j = 0 ; j < 4 ; j++)
         {
            cout << array2[j]; //prints the *'s
         }

  while (life !=0) //if lives are greater than 0 carry on else don't
            {
                  cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl;

                  cout << "guess is :  "  ;

                  cin >>key; // grab key from user

                for (int k = 0; k < 5; k++) //takes guess and checks it over
                 {
                      if (  key == array[k])
                      {
                          array2[k] = array[k];
                      }
                 }
            
                if (key!= array[0] && key!=array[1] && key!=array[2] && key!=array[3] && key!=array[5])
                {
                   life --;
                   cout << "You've just lost a life, you now have "<< life << "remaining\n"<<endl;;
                }
    

               for (int h = 0; h < 4; h++)
                   cout <<  array2[h] ;
            }
if (life == 0)
{
cout << "Your out of lives, do you wish to play again?" <<endl;
}

   return 0;
}

However theres no way to terminate it.... when all the letters are correct, Eg. Entering "John" will be correct letter combination but it wont terminate, not sure what to do for it.

Your problem is here:

if (key!= array[0] && array[1] && array[2] && array[3] && array[5])

It's best to think of everything separated by && as a unique expression. What your code is actually doing is:

if (key!= array[0] && 
    array[1] != 0 &&
    array[2] != 0 &&
    array[3] != 0 &&
    array[5] != 0)

Of course, the array[5] appears to be a typo. It should be 4. ;) A loop would be better anyway, especially when you move on to words with different lengths:

int i;

for ( i = 0; i < 5; i++ ) {
  if ( key == array[i] )
    break;
}

if ( i == 5 )
  --life;

arr cool, thanks didnt know the break function exisited! You learn something new everyday, thats the whole point of this exerice!

But I'm struggling on bringing it out of the while once the word has sucessfuly been found

>But I'm struggling on bringing it out of the while once the word has sucessfuly been found
You can use the same trick:

for ( i = 0; i < 5; i++ ) {
  if ( array2[i] == '*' )
    break;
}

if ( i == 5 ) {
  cout<<"You win!"<<endl;
  break;
}
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.