I would like to make this first function work as the one were i opened it from a file. In this first one i am using arrays the second one uses strings. ignore the masking of the password in the first one i want it to do that and then check if the password typed in is correct.

#include <iostream>
#include <conio.h> 
#include <ctype.h> 
using namespace std;

int main()
{
  int ch;
  char pword[100];
  int i = 0;
  char guess[100];
  int life = 5;
  char key;

  
  puts ("Enter your password");
  fflush(stdout);
  
  while ((ch = getch()) != EOF 
          && ch != '\n' 
          && ch != '\r' 
          && i < sizeof(pword) - 1)
  {
    if (ch == '\b' && i > 0) 
    {
      printf("\b \b");
      fflush(stdout);
      i--;
      pword[i] = '\0';
    }
    else if (isalnum(ch))
    {
      putchar('*');
      pword[i++] = (char)ch;
    }
  }

  pword[i] = '\0';
  
  printf ("\nYou entered >%s<", pword);
  
  cout << "Player 2. Guess the word" << endl;
  cin >> guess;
  
  for (int i = 0 ; i < 5 ; i++)
        {
             pword[i];
        }

    for (int j = 0 ; j < 4 ; j++)
        {
            cout << guess[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 == pword[k])
                      {
                          guess[k] = pword[k];
                      }
                 }
                for (int i = 0; i < pword[5]; ++i)
                {
                if (key!= pword[i])
                {
                   life --;
                   cout << "You've just lost a life, you now have "<< life << "remaining\n"<<endl;;
                }
}
               for (int h = 0; h < 4; h++)
               {
                   cout <<  guess[h] ;
                   if( key == guess[h])
                   {
                       cout << "WHAT";
                       return 0;
                }       }
            }
if (life == 0)
{
cout << "Your out of lives, do you wish to play again?" <<endl;
}

   return 0;
}


SECOND FUNCTION
//headers

string make_display_word(string s)  // REPLACES THE STRING WITH *

{

  string t = "";

  for (int i = 0; i < s.size(); i++)

    t += '*';

  return t;

}

bool ch_in_string(char ch, string s) // CHECKS IF THE CHARACTER ENTERED AND STRING ARE THE SAME

{

  for (int i=0; i < s.size(); i++)

    if (ch == s[i])

      return true;

  return false;

}

void enter_char(char ch, string secret, string &display) // CHECKS IF THE CHARACTER ENTERED AND STRING ARE THE SAME

{

  for (int i = 0; i < secret.size(); i++) {

    if (secret[i] == ch)

      display[i] = ch;

  }

}



// DRAWS THE HANGMAN

void play(int figure)

{    

     int chance;

     cout << "How many Chances do you want" << endl;

     cin >> chance;

    



    string word ;

    string q;

    ifstream fin;

    fin.open(infile);

    if(fin.fail())

    {

                  cerr << "What" << endl;

                  }

    while(!fin.eof())

    {



    int i;

    

    for(i = 1; i<=10;i++)

    {

    i = rand()%100; // Assuming 10,000 words in the file



    do

    {

    fin>> word;

    }while ( --i >= 0 );







  // NUMBER OF WRONG GUESSES

  int wrong_count = 0;

  int figure = 1;



  string display_word = make_display_word(word);

  string already_guessed = "";



  do

{

    cout << word;

    cout << "Your guess so far: " << display_word << "   "

         << "Wrong guesses: " << wrong_count

     << "   Letters guessed: " << already_guessed

     << endl;

       if (display_word == word && wrong_count ==0)

    {

      cout << "Whoa you are great";

      getch();

      exit(0);

      }

    if (display_word == word)

    {

                                      

      cout << "You got it!\n";

      getch();

      exit(0);

      }

    if (wrong_count >= chance)

    {

      cout << "You are dead!\n"

           << "The correct word was"

           << "  "<<word;

           getch();

    exit(0);

    }

    cout << "\nGuess a letter: ";

    char ch;

    cin >> ch;

    ch = tolower(ch);

    if (!isalpha(ch))

    {

      cout << "Only letters!\n\n";

    } else {

      if (ch_in_string(ch, already_guessed))

      {

    cout << "You already guessed '" << ch << "'\n";

      } else {

    already_guessed += ch;

    if (ch_in_string(ch, word))

    {

      enter_char(ch, word, display_word);

    } else { draw(figure);

      cout << "Sorry, wrong guess!\n";

     wrong_count++;

     figure++;

    

    

      }

     }

    }

}while(!false);

}  

}

}

I don't have a clue what question you are asking.

function ch_in_string() -- there's a lot easier way to do that. std::string has a find method

bool ch_in_string(char ch, string s)
{
    return s.find(ch) == string::npos) ? true : false;
}

I don't have a clue what question you are asking.

function ch_in_string() -- there's a lot easier way to do that. std::string has a find method

bool ch_in_string(char ch, string s)
{
    return s.find(ch) == string::npos) ? true : false;
}

I guess that should be :

bool char_in_strin (string s, char ch)
{
    // return true if char is found in string 

    return ( s.find (ch) != string::npos ? true : false ) ;
}

or in a simple way:

if ( my_string.find (my_char) != string::npos )
{
     std::cout << "Character was found in the string \n" ;
}
else
{
     std::cout << "Character NOT found in the string \n" ;
}

[Torbecire]

The first function reads a word from a file and replaces characters with '*'. I want the second function to work in the same way, the only difference that we mask the characters that we enter(Like how you would choose a password and not see what you type).

Standard C/C++ does not allow you to type and not echo the characters. Input is buffered which means you type and the system stores the characters until the ENTER is pressed. Then your program reads the characters.

Some -- but not all -- compilers have a way around this, but the functions are
1) not standard so not recommended
2) not necessarily the same for the compilers that do allow this

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.