Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays and functions for hangman game help!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i am trying to write a C program for a very simple hangman game which uses just one word 'mouse' assumes there is no repeat characters er with graphics. The basic layyout will be ----- for the letters of the word then user will be asked to guess letter and as they are guessed correctly will be revelaed in the correct "-". I dont want this program to involve any strings, just arrays and functions.
The code i have below so far accpets the user input of a letter but it is not registering when the letter is correect. Also Im having problems with how to assign the correct letter to the correct dash. I know this will involve another array guess_word[5] = {'-','-','-','-','-'} but does anyone have any idea how i would go about assigning the letters to there correct spaces?
Any help would be gratefully accepted im really in a hole here!!

#include <stdio.h>

#define letters 5

int search(const char list[], int letternum, char letter);

int main(void)
{
char word[letters] = {'m','o', 'u', 's', 'e'};
char letter;
int location;
int tries = 0;

do
{
printf(&quot;choose a letter: &quot;);
scanf(&quot;%1s&quot;, &letter);

location = search(word, letters, letter);

if (location) printf(&quot;%c was found\n&quot;, letter);
else printf(&quot;%c was not found\n&quot;, letter);

++tries;

}while(tries < 6);

return 0;
}

int search(const char list[], int letternum, char letter)
{
int i = 0, found = 0;

while (i < letternum && !found)
{
if (list == letter) found = 1;

else i++;
}

return found;
}
 
I have made some little modifications to your code,now it should do what you expected it to do.

#include <stdio.h>

#define letters 5

int search(const char list[], int letternum, char letter);

int main(void)
{
char word[letters] = {'m','o', 'u', 's', 'e'};
char guess_word[letters];
char letter;
int location;
int tries = 0;
int i = 0;

for( i = 0; i < letters; i++ )
guess_word = '_';

do
{
printf(&quot;choose a letter: &quot;);
scanf(&quot;%1s&quot;, &letter);

location = search(word, letters, letter);

if ( location != -1 )
{
printf(&quot;%c was found\n&quot;, letter);
guess_word[location] = letter;
for( i = 0; i < letters; i++ )
printf(&quot; %c&quot;,guess_word);
printf(&quot;\n&quot;);
}

else printf(&quot;%c was not found\n&quot;, letter);

++tries;

}while(tries < 6);

return 0;
}

int search(const char list[], int letternum, char letter)
{
int i = 0, found = 0;

while (i < letternum && !found)
{
if (list == letter) found = 1;

else i++;
}

if( found == 1 )
return i;
else
return -1;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top