Store user input, then display array contents

I'm coding a hangman game. I'm trying to store user entries so i can output them to show the user what they have already entered. Problem is that it's not display anything at all.

I'm trying to store multiple characters of course, and then display all characters stored.

What's going on here? What am i doing wrong?

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

		char guess[27]={0};
		
		cin >> guess[26];

		int hit=0;

		for(int i=0; i<len; i++)
		{
			if( guess[26] == hidden_word[i] )
			{
				hit++;
				select_word[i] = guess[26];
				if(strcmp(hidden_word,select_word) == 0)
				{
					you_win = true;
				}
			}

		}
		if(hit == 0)
		{
			life--;
		}
		if ((life == 5) && (you_win == false))
		{
			system("cls");
			hang05;
			cout << "You have currently guessed ";
			for (int i = 1; i <= 5; i++)
			{
				 cout << guess[i] << "\n";
			}
		}


Thanks for your help.

EDIT: I also get the error - Stack around the variable 'guess' was corrupted. At the end of the game.
Last edited on
Bump! Please help me.
In line 2 you define variable "guess" with 27 items (0-26). When you write guess[27], you try to access out of variable.
Thanks for the reply.

How would i get it do it writes to the array, in a different memory address each time, so i can print all of them out?

Thanks.
Didn't realise that i had to make it 26. Thought that's only when recalling certain addresses. Thanks. That gets rid of the error i was getting.

Edit: How would i get the user to enter information into the array without a for loop counting to 26?

Of course i want the user to just enter one alphabeticle letter, for it to be tested, i don't want the user to enter 26 letters!

This ends up with only one letter being displayed.
Last edited on
If you write
cin >> guess;
and user inputs "word", then
guess[0]='w',
guess[1]='o',
guess[2]='r',
guess[3]='d',
guess[4]='\0'
Thanks for your help so far Konstantin.

Here's my code so far.

Every time the code reverts back to the input, and something is inputted, the first entry in the array is erased and replaced with the new entry. So it only ever displays the last entry inputted.

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
char guess[27]={0};

		cin >> guess;
		
		int hit=0;

		for(int i=0; i<len; i++)
		{
			if( guess[0] == hidden_word[i] )
			{
				hit++;
				select_word[i] = guess[0];
				if(strcmp(hidden_word,select_word) == 0)
				{
					you_win = true;
				}
			}

		}
		if(hit == 0)
		{
			life--;
		}

		if ((life == 6) && (you_win == false))
		{
			system("cls");
			hang05();
			cout << "\nYou have currently guessed ";
			for (int i = 0; i <= 26; i++)
			{
				cout << guess[i];
			}
		}
Topic archived. No new replies allowed.