By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

clearing an array?

P: 9
Hi, i am making a hangman game in C and i have a problem.

I am using 2 arrays to store the word to be guessed, and another array that stores the users guesses.

Ok now, this program runs untill the user tells it to stop. Because of this, i need to reset the array that stores the user guesses. i.e: keep the same size, but clear all the data out of it.

I have used memset(guesses,0,50); (i tried both 0 and '/0')
but this does not seem to work. When i do this, and print out the array, i get blank spaces, but in my program, it keeps saying that the user has already tried this letter.

Any ideas people? Cheers
Oct 8 '06 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Hi, i am making a hangman game in C and i have a problem.

I am using 2 arrays to store the word to be guessed, and another array that stores the users guesses.

Ok now, this program runs untill the user tells it to stop. Because of this, i need to reset the array that stores the user guesses. i.e: keep the same size, but clear all the data out of it.

I have used memset(guesses,0,50); (i tried both 0 and '/0')
but this does not seem to work. When i do this, and print out the array, i get blank spaces, but in my program, it keeps saying that the user has already tried this letter.

Any ideas people? Cheers
when I did my hangman I did not have to worry about all this because I did not use an array to store the user's input. All I did was take the input character and call my replace function. If a character has already been input, the replace does not change the answer array so nothing happens(and the player is not penalised). It also does not limit some aspects of the program.
For example, what size did you declare your inputs array to be? Would not that size limit you in your application of the game for different scenarios.
Oct 8 '06 #2

P: 9
the reason as to why im using an array is because i want to print out the users past guesses.

But at the end of the game, i need to clear this array.
Oct 8 '06 #3

P: 9
one thing i just found out.

if i use the memset thing, it will work properly the second time round if the first thing that the user inputs is /0

thats with : memset(guesses,'/0',0);
ideas?
Oct 8 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
You have not really posted enough code for use to be able to tell you the problem, you have assumed the problem is in the memset line of you code but looking at it it looks correct to me to clear an array that is 50 bytes long.

Assuming guesses is an array a better way to call memset is

memset(guesses,0,sizeof guesses);

'/0' is wrong, I suspect you meant '\0' which is the 0 character.
Oct 9 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.