...

View Full Version : Hangman C code



haze man
11-13-2012, 08:53 PM
whats wrong with my hangman code?? it isnt assigning the arrays to be the word guessed, i know at the moment i have hard coded 'trick' to be guessed but even that doesnt work. Thanks in advance hope you can help!
here is the code:

#include <stdio.h>
#include <stdlib.h>
main()
{
printf("Welcome To Hangman \n \n \n") ;
printf("To play this game you have to guess the letter of the word that is picked \n \n") ;
printf("If you guess correct you will not lose any lives \n") ;
printf("However an incorect guess will cause you to lose one life \n") ;
printf("Good Luck! \n \n") ;


//random word select
srand(0) ;
int num = rand() %5 +1 ;

printf("%d \n", num) ;

flushall();

//assignment of random words to arrays
char array ;

if ( num == 1 )
{
char array[] = {'b','r','i','c','k'} ;
}

else
if ( num == 2)
{
char array[] = {'t','r','i','c','k'} ;
}

else
if ( num == 3)
{
char array[] = {'b','o','a','t','s'} ;
}

else
if ( num == 4 )
{
char array[] = {'m','e','n','u','s'} ;
}

else
if ( num == 5 )
{
char array[] = {'g','u','i','d','e'} ;
}

//To scan the users imput to the word to be guessed

int lives = 10 ;

printf("You have %d lives \n \n", lives) ;

char array1[] = {'t','r','i','c','k'} ;
char letter;
int i;

for (i = 0 ; i < 6 ; i++)
{
letter = (char)getchar();
scanf("%c", &letter);

if (letter == array1[i])
{

lives = lives ;
printf("Corrent you now have %d lives left \n \n", lives) ;
}
else
{
lives = lives --;
printf("Incorrect you now have %d lives left \n \n", lives) ;
}

flushall();

if ( lives <= 0)
printf("You lose") ;


}


getchar() ;

}// end to main

alykins
11-14-2012, 02:41 PM
according to this (http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/) link your arrays should be something like


char array1[] = "trick";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum