wgilkerson
New Coder
Hello,
I am having an issue completing HW8.4 in the above mentioned textbook. The homework problem is a Word Search meant to complete 5 loops at 3 different difficulties (less time). The issue I run into is that it will not complete more than 1 loop. Perhaps the issue is in my "if" statements? Here is my code:
I am having an issue completing HW8.4 in the above mentioned textbook. The homework problem is a Word Search meant to complete 5 loops at 3 different difficulties (less time). The issue I run into is that it will not complete more than 1 loop. Perhaps the issue is in my "if" statements? Here is my code:
C:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
//function prototypes
void checkAnswer(char *, char []);
int main()
{
char *strGame[5] = {"ADELANGUAGEFERVDATAZOPIBMOU",
"ZBPOINTERSKLMLOARRAYOPMNOCO",
"PODSTRINGGDIGWHIEEFUNCTIONICERLS",
"YVCPROGRAMMERWQPRINTKNULTHMD",
"UKUNIXFIMWXIZNULLEQZINPUTEX"};
char answer[80] = {0};
int displayed = 0;
int x;
int level = 0;
int startTime = 0;
system("clear");
printf("\n\n\tWord Find\n\n");
printf("Select a difficulty level (1-3): ");
scanf("%d", &level);
if (level == 1) //displays word for least time
{
startTime = time(NULL);
for (x=0; x<5; x++)
{
while (startTime + 3 > time(NULL))
{
if (displayed == 0)
{
printf("\nFind a word in: \n\n");
printf("%s\n\n", strGame[x]);
displayed = 1;
} //end if
} //end while loop
//no longer using in case this was the problem system("clear");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("Enter word found: ");
scanf(" %s", &answer);
//gets doesn't appear to be a safe function gets(answer);
checkAnswer(strGame[x], answer);
displayed = 0;
startTime = time(NULL);
break;
} //end for loop
}
if (level == 2) //displays word for more time
{
startTime = time(NULL);
for (x=0; x<5; x++)
{
while (startTime + 2.5 > time(NULL))
{
if (displayed == 0)
{
printf("\nFind a word in: \n\n");
printf("%s\n\n", strGame[x]);
displayed = 1;
} //end if
} //end while loop
//no longer using in case this was the problem system("clear");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("Enter word found: ");
scanf(" %s", &answer);
//gets doesn't appear to be a safe function gets(answer);
checkAnswer(strGame[x], answer);
displayed = 0;
startTime = time(NULL);
break;
} //end for loop
}
if (level == 3) //displays word for most time
{
startTime = time(NULL);
for (x=0; x<5; x++)
{
while (startTime + 2 > time(NULL))
{
if (displayed == 0)
{
printf("\nFind a word in: \n\n");
printf("%s\n\n", strGame[x]);
displayed = 1;
} //end if
} //end while loop
//no longer using in case this was the problem system("clear");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("Enter word found: ");
scanf(" %s", &answer);
//gets doesn't appear to be a safe function gets(answer);
checkAnswer(strGame[x], answer);
displayed = 0;
startTime = time(NULL);
break;
} //end for loop
}
else
{
printf("\nThat is not a valid level\n");
}
//end if
return 0;
} //end main function
void checkAnswer(char *string1, char string2[])
{
int x;
int y;
//convert answer to UPPER CASE to perform a valid comparison
for (x=0; x<=strlen(string2); x++)
string2[x] = toupper(string2[x]);
if (strstr(string1, string2) != 0 && string2[0] != 0)
{
printf("\nGreat job!\n");
y = y + 1;
printf("\nYour score is: %d", y);
}
else
{
printf("\nSorry, word not found!\n");
y = y - 1;
printf("\nYour score is: %d", y);
} //end checkAnswer
}
Last edited by a moderator: