Confused on a minor for loop in Hangman project

I'm building a simple Hangman project, it reads an external text file into an array. That works fine but I want to add a loop to make it display the correct word when the player loses. I don't know if its correct.

Anyway this is the loop I'm trying to make work:

1
2
3
4
for (wordis = word.length(); wordis != word.length(); wordis++)
    {
   	 cout<<"The word is: " << wordis << "!" <<endl;
    }


Here is the function it belongs to:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
void playgame(string word) //function displayes words player has to guess, positions the letters to match the word and displays of player has won or lost
{	
	bool correct =false; //correct letters are false by default.
	char guessletter; //input of what letter the player is guessing
	int rightC = 0, wrongC = 0,size = word.length(); //rightC is counter for correct guesses, & wrongC is counter for wrong guesses
	bool right[size]; ///position
	char wordis;
	for(int i = 0; i< word.length(); i++)
	{
		right[i]=false;
	}
	do 
	{
		correct = false; //by default is false
		cout<<endl<< "Guess the letters!"<<endl;
		
		cin>>guessletter;
		for(int i = 0; i< word.length(); i++)
		{
		
			if(guessletter==word[i])
				{
					right[i]=true;
					correct =true;
					rightC++;
				}		
		}
		if(correct == false)
			{
				wrongC++;
				hanged(wrongC);
				cout<< guessletter << " is wrong!"<<endl;
			}
			
		//show the word results 
		for(int i = 0; i< word.length(); i++)
		{
			if(right[i]==true)
			{
				cout<<word[i];
			}
			
			else
			{
				cout<<"_";
				cout<< s;
			}
		
		
		}	
	}//do wcloses
	
	
	while (wrongC < 7 && rightC < word.length());
	

	
	if(rightC == word.length()) //IF player won
	{
		cout<<endl;
		cout<<"\t    YOU WIN!"<<endl;
		cout<< "\t@@@@@@@@@@@@@@@"<<endl;
		cout<< "\t @@@@@@@@@@@@@"<<endl;
		cout<< "\t  @@@@@@@@@@@"<<endl;
		cout<< "\t   @@@@@@@@@"<<endl;
		cout<< "\t   OOOOOOOOO"<<endl;
		cout<< "\t  0*********0"<<endl;
		cout<< "\t 0***********0"<<endl;
		cout<< "\t 0*  MEDAL  *0"<<endl;
		cout<< "\t 0***********0"<<endl;
		cout<< "\t  0*********0"<<endl;
		cout<< "\t   OOOOOOOOO"<<endl;
		cout<< "\tYOU SAVED THE MAN! "<<endl;

	}
	else
	{
		cout<<endl;
		cout<<endl;
		cout<< "YOU LOSE!"<<endl; //if player lost
		cout<< "THE MAN WAS HANGED!"<<endl;
		cout<< endl;
		cout<< "  I-------|"<<endl;
		cout<< "  I       *"<<endl;	
		cout<< "  I       *"<<endl;
		cout<< "  I       Q"<<endl;
		cout<< "  I      /|\\"<<endl;
		cout<< "  I       |"<<endl;
		cout<< " _|_ ``| / \\ |`` "<<endl;
	}//else closes
	

}


I don't know where to place the loop, or if its the correct way to do it.
I tried placing it before and after the while loop but it doesnt seem to work. I would appreciate all help.

 
while (wrongC < 7 && rightC < word.length());
Last edited on
Try this for (wordis = word.length()-1; wordis >=0; wordis--) or
for (wordis = 0; wordis <word.length();wordis++)
then cout<<word.at(i);
or just cout<<word
Last edited on
@LendraDwi

Okay but where in the program do I place it?
At line 77 or 78 in lose condition ?
Okay so I placed the loop like this
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
while (wrongC < 7 && rightC < word.length());
	
	if(rightC == word.length()) //IF player won
	{
		cout<<endl;
		cout<<"\t    YOU WIN!"<<endl;
		cout<< "\t@@@@@@@@@@@@@@@"<<endl;
		cout<< "\t @@@@@@@@@@@@@"<<endl;
		cout<< "\t  @@@@@@@@@@@"<<endl;
		cout<< "\t   @@@@@@@@@"<<endl;
		cout<< "\t   OOOOOOOOO"<<endl;
		cout<< "\t  0*********0"<<endl;
		cout<< "\t 0***********0"<<endl;
		cout<< "\t 0*  MEDAL  *0"<<endl;
		cout<< "\t 0***********0"<<endl;
		cout<< "\t  0*********0"<<endl;
		cout<< "\t   OOOOOOOOO"<<endl;
		cout<< "\tYOU SAVED THE MAN! "<<endl;

	}
	else
	{
		for (wordis = 0; wordis <word.length();wordis++)
		{
			cout<<"The word was: "<< word << "!"<< endl;
		}
		cout<<endl;
		cout<<endl;
		cout<< "YOU LOSE!"<<endl; //if player lost
		cout<< "THE MAN WAS HANGED!"<<endl;
		cout<< endl;
		cout<< "  I-------|"<<endl;
		cout<< "  I       *"<<endl;	
		cout<< "  I       *"<<endl;
		cout<< "  I       Q"<<endl;
		cout<< "  I      /|\\"<<endl;
		cout<< "  I       |"<<endl;
		cout<< " _|_ ``| / \\ |`` "<<endl;
		
	}//else closes 


And it shows the correct word, but it keeps repeating it like 10 times. For example:
"YOU LOSE!
THE MAN WAS HANGED!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!
The word was: MONSTERHUNTER!"

How can I make it stop repeating like that?
I got it to work, I simply placed a break before the loop closed. Thank you for the help!!
Topic archived. No new replies allowed.