Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please tell me what's wrong with this code void startHangmanGame(void) //second game function { int choice; //choice declaration FILE *p; //pionter declaration char

Can you please tell me what's wrong with this code

void startHangmanGame(void) //second game function

{

int choice; //choice declaration

FILE *p; //pionter declaration

char data_list[20]; //string declaration

int input_status;

int counter = 0; //counter for the word

int counter2 = 5;//counter for attempts

char str[20]; //string declaration

char letter;

printf("Hangman Has Started ");

printf("choose a category: "); //print the menu

printf("1. Food. ");

printf("2. Objects. ");

printf("3. Names. ");

printf("4. Colors. ");

scanf("%d", &choice); //get the player choice

switch(choice) //switch for the choice

{

case 1:

p = fopen("food.txt", "r"); //open food file

break;

case 2:

p = fopen("objects.txt", "r"); //open objects file

break;

case 3:

p = fopen("names.txt", "r"); //open names file

break;

case 4:

p = fopen("colors.txt", "r"); //open colors file

break;

default:

printf("Invalid choice ");

} //end switch

srand(time(NULL)); //random function

int x=rand()%15;

printf("rand value=%d ",x);

if(p ==NULL) //check if the file is available

printf("cannot open the file");

else

{

input_status=fscanf(p,"%s",data_list);

while(input_status==1)

{

input_status=fscanf(p,"%s",data_list);//start while loop

if(counter==x) //check if the counter = random value

{

strcpy(str, data_list); //copy string

printf("%s ", str);

//counter value update

}

counter++;

}//end while loop

fclose(p); //close the file

}

int size=strlen(str); //declare size

printf("the word so far is: ");

for(int i=0; i

{

printf("_");

}

printf(" "); //new line

printf("you have %d remaining attempts " , counter2);

for(int j=0; j<5;j++) //number of attempts

{

printf("enter letter ");

scanf("%c", &letter);

printf("%c", letter);

}

} //end second game function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Learn about reasons for the popularity of R and its interface

Answered: 1 week ago