Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C Programming for the Absolute Beginner, 3rd Edition | HW8.4 Problem

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:

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:
Hey there,

Just a quick reminder, remember to post all your code within the </> option within the editor. In regards to your question, did you get any errors? If it's not completing it's most likely that a condition is met and it's not bothering with checking to see if the other conditions are met as well.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom