hi guys. sorry if this is a newbie question but i cant get my code to work! it compiles and runs but when i get to the point of inputing a character, the program crashes/wont accept any input. this is a piece of homework i have and it is to write a hangman game. the comments are from my lecturer because we were handed a skeleton code to work from. any help would be appreciated because it is due in 4 hours!
my code is
Code:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>
char *list[12] ={
"banana", "programmer",
"tongue", "favourite",
"serendipity", "sportman",
"harmony", "goalkeeper",
"operation", "hamstring",
"vibration", "wonderful"
};
int main(void)
{
int wordi, lives;
char word[20];
char str1[20];
int wordlength;
int i;
char x;
int win;
char *ptr;
char yesno;
srand((unsigned int)time(NULL));
while (1){
// this block select a random word to start a game
wordi = rand() % 12;
wordi=1;
printf("\n rand index is %d \n ", wordi);
strcpy(word,list[wordi]);
// set variables to initial state
lives=8;
strcpy(str1,word);
wordlength=strlen(str1);
if (wordi=1){
strcpy(str1,"______");}
else if (word=="programmer"){
strcpy(str1,"__________");}
else if (word=="tongue"){
strcpy(str1,"______");}
else if (word=="favourite"){
strcpy(str1,"_________");}
else if (word=="serendipity"){
strcpy(str1,"___________");}
else if (word=="sportman"){
strcpy(str1,"________");}
else if (word=="harmony"){
strcpy(str1,"_______");}
else if (word=="goalkeeper"){
strcpy(str1,"__________");}
else if (word=="operation"){
strcpy(str1,"_________");}
else if (word=="hamstring"){
strcpy(str1,"_________");}
else if (word=="vibration"){
strcpy(str1,"_________");}
else if (word=="wonderful"){
strcpy(str1,"_________");}
printf("%s",str1);
printf("\n Let's start. You have %d lives \n ", lives);
while(1)
{
while (lives != 0){
printf("\n Next x: ");
scanf("%c",&x);
printf("%c",x);
for (i=0;i=wordlength;i++){
if (word[i]==x){
str1[i]==word[i];
}
else {}
}
ptr=strchr(str1, '_');
if (ptr==NULL){win=1;}
else{}
// update stare accordingly and print one of the follwing messages
if (win == 1){
printf(" The word is %s, you have won !! \n",str1);}
else if (lives != 0){
printf(" Lives: %d, Current word: %s \n", lives, str1);}
else if (lives ==0){
printf(" Game Over \n");}
}
// this loops ask the player if he wants to play again and waits for x Y/N
// you don't need to change this
while (1);
{
printf("\n Do you want to play again [Y/N] \n");
scanf("%s",&yesno);
if ((yesno == 'N') || (yesno == 'n'))
return 0;
if ((yesno == 'Y') || (yesno == 'y'))
break;
}
// goes to play again
}
}
}