Jan 10, 2015 at 3:23pm UTC
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 Jan 11, 2015 at 11:56am UTC
Jan 10, 2015 at 7:43pm UTC
In line 2 you define variable "guess" with 27 items (0-26). When you write guess[27], you try to access out of variable.
Jan 11, 2015 at 2:03am UTC
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.
Jan 11, 2015 at 11:56am UTC
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 Jan 11, 2015 at 12:07pm UTC
Jan 11, 2015 at 12:40pm UTC
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'