C++ Urgent Hangman Game Detail Help

So I am having a lot of trouble with this use of char arrays. I still haven't figured out how to make the game tell the user if he has already guessed that letter (But I think Im close). But I must also make the code return the word solution in stars (*)!

I have to do a char to hold the *'s. Find the number of elements in the word[] And create the *'s based on the number of letters above.

Next scan the word[] for the guessed letter. If it's there, set the corresponding letter in the star[] to the guessed letter. I guess this is similar to the guessing part, but I cant figure it out! I have to turn this is really soon so any help would be awesome!

The code works well apart from the fact that I'm missing the (*s). And the part where I try to store and check the user's guesses is wrong. This is what I got so far:


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
   char storeguess[26];
   int newlife=0;
   char letter;
   bool yes;
   char solution[100]={0};
   char newW[100]={0};
   int LENGTH;
  
   cout<<"Write a word: ";

   cin>>solution;
  
   LENGTH=strlen(solution);
    cout<<"you have six lives to guess the unknown word, it has" <<endl<<LENGTH<<endl<< "letters";

   while ( newlife <6 && strcmp(newW, solution) != 0)
   
   {
	yes=0;
	cout<<endl<<"Guess a letter: ";
	cin>>letter;

	for(int n=0; n<26; n++) {   // This is my attempt to store and check the user's guess... 
	storeguess[n]=letter; 
	if(letter==storeguess[n]){
		cout<<"You already guessed that!";} 
	}
	
	
	for(int n=0; n<100; n++) {
	
	if(solution[n] == letter){
		letter=solution[n];
	    newW[n]=solution[n]; 
	    yes=(1);
	cout<<"nice!"<<"You got this letter!" <<endl<<newW<<endl;} 
	}
	if (yes<0){ 
	cout<<"Sorry please try again."<<endl; 
	newlife=(newlife+1);
	cout << "you lost this many lifes: "<<newlife<<endl;
	cout<<"please continue: "<<endl<<newW;
	}
	
   if (newlife==1) {
	   cout<<endl<<"Head";}
    if (newlife==2) {
   cout<<endl<<"Head-Body"; }
	 if (newlife==3) {
   cout<<endl<<"Head-Body-RightArm"; }
	  if (newlife==4) {
   cout<<endl<<"Head-Body-RightArm-LeftArm"; }
	   if (newlife==5) {
   cout<<endl<<"Head-Body-RightArm-LeftArm-RightLeg"; }
	   if (newlife==6) {
		   cout<<endl<<"Head-Body-RightArm-LeftArm-RightLeg-LeftLeg"; 
		   cout<<endl<<"Sorry you lost! HANGMAN!!"<<endl<<endl;
		   

	   }}
   cout<<"YOU WIN!!"<<endl;
   system("pause");
   
   getch();
   return 0;
}

For anyone reading this, you can jump in and help in this thread:
http://www.cplusplus.com/forum/beginner/47289/
Yea that would be awesome... but Im not sure if its the fact that it has two pages, but no one has responded in the old thread since yesterday morning and Im really pressured to finish this! So I please do help! In this or the other thread I don't care
Last edited on
so im about 2-3 days new to c++ and thought this was a fun post for me to try and learn off of and i compiled the code you have so far and was able to get the wrong answer part of hangman to work for me heres what i did.
i changed your original
while ( newlife <6 && strcmp(newW, solution) != 0)
{
yes=0;
cout<<endl<<"Guess a letter: ";
cin>>letter;

to

while ( newlife <7 && strcmp(newW, solution) != 0)
{
yes=1;
cout<<endl<<"Guess a letter: ";
cin>>letter;


then
if (yes<0){
cout<<"Sorry please try again."<<endl;
to
if (yes<1){
cout<<"Sorry please try again."<<endl;
__
if (newlife==1) {
cout<<endl<<"Head";}
if (newlife==2) {
cout<<endl<<"Head-Body"; }
if (newlife==3) {
cout<<endl<<"Head-Body-RightArm"; }
if (newlife==4) {
cout<<endl<<"Head-Body-RightArm-LeftArm"; }
if (newlife==5) {
cout<<endl<<"Head-Body-RightArm-LeftArm-RightLeg"; }
if (newlife==6) {
cout<<endl<<"Head-Body-RightArm-LeftArm-RightLeg-LeftLeg";
cout<<endl<<"Sorry you lost! HANGMAN!!"<<endl<<endl;
to
if (newlife==2) {
out<<endl<<"Head";}
if (newlife==3) {
cout<<endl<<"Head-Body"; }
if (newlife==4) {
cout<<endl<<"Head-Body-RightArm"; }
if (newlife==5) {
cout<<endl<<"Head-Body-RightArm-LeftArm"; }
if (newlife==6) {
cout<<endl<<"Head-Body-RightArm-LeftArm-RightLeg"; }
if (newlife==7) {
cout<<endl<<"Head-Body-RightArm-LeftArm-RightLeg-LeftLeg";
cout<<endl<<"Sorry you lost! HANGMAN!!"<<endl<<endl;

like i said im new so im not sure if this is the answer you were looking for but when i was playing it it was unable to register a wrong answer past "head" i hope this is helpful in some way and if someone more experienced could explain why this works i would appreciate it since i would like to learn more too, thank you.

edit: sorry i just realized you do not need to change "if (newlife==) starting at 1 to start at 2 it works fine the original you had it but the changes i made before that seemed to work for me
Last edited on
Topic archived. No new replies allowed.