Pls how can ı write a Hangman program

Please how can i write a program that will check a sequence of strings maybe the character is there and the user will be given five chances to search the alphabets
try using arrays and chars instead of strings. and use if-else statements to check whether the inputted alphabet match any char from the array. if it match, then show the array, if not then you lose one life.
using strings is actually a good way of doing it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string key = "this is a secret phrase";
string revealed;
char guess;

for (int i = 0; key[i] != '\0' ; i++)
{
  if (key[i] == ' ') revealed += key[i];
  else revealed += '_';
}

for (int tries = 0; tries < 5 ; tries++)
{
  cout << revealed << endl << "guess: ";
  cin >> guess;

  for (int i = 0; key[i] != '\0' ; i++)
    if (key[i] == guess) revealed[i] = key[i];

  bool escape = true;
  for (int i = 0; revealed[i] != '\0' ; i++)
    if (revealed[i] == '_') escape = false, break;
  
  if (escape) break;
}
cout << revealed<< endl;
Last edited on
Topic archived. No new replies allowed.