Question

I'm still getting the hang of C loops. I can't figure out why my while loop won't work.

I am not getting any errors in this program but what is happening is that while loop is doing the else if statement and won't go back to the if statement. But if you input the right number on the first guess, it does the if. When you try to guess anytime after the first guess it won't run though the full while loop and keeps displaying number is "to low". What am I doing wrong? And how do I fix it it?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int ranNumGen();
void Right(int num);  // prompt that you guessed right
void wrong(int guess, int num);  // if else's to tell you to high to low...
void sorry(int num); // prompt for not guessing it right


int main(void)
{
    // Local declarations 
    int num;
    int guess;
    int a;

    // Sets variables  
    num = ranNumGen();
    a = 0;


    // Prompt
    printf("\nLets play a game");
    printf("\nCan you guess the number I am thinking of?");
    printf("\nI will give you a hint its a number between (1 and 20)");
    printf("\nI am also going to going to give you five tries.");
    printf("\nHave a guess! ");
    printf("Shows number to win %d", num); // shows number to win to check if it works
    scanf_s("%d", &guess);




    while (a < 4)
    {
        (a++);

        if (guess == num)
        {
            Right(num);
            break;
        }
        else if (guess < num || guess > num)
        {
            wrong(guess, num);
        }
    }


    // End promts
    if (guess == num)
    {
        printf("\nGoodbye\n");
    }
    else
    {
        sorry(num);
    }
    return 0;
}

int ranNumGen()
{
    int range;
    srand(time(NULL));
    range = 20;
    range = (rand() % range + 10) + 1;
    return range;
}

void Right(int num)
{
    printf("\n\t*******************************************");
    printf("\n\t* Wow you guessed it! Yep its %d alright. *", num);
    printf("\n\t*    You won't get it next time! :)       *");
    printf("\n\t*******************************************");
}

void wrong(int guess, int num)
{
    if (guess > num)
    {
        printf("\nYour guess is to high.", guess);
        printf("\nTry again: ");
        scanf_s("%d", &guess);
    }
    else if (guess < num)
    {
        printf("\nYour guess is to low.", guess);
        printf("\nTry again: ");
        scanf_s("%d", &guess);
    }
}

void sorry(int num)
{
    printf("\nSorry buddy, you didn't guess it...");
    printf("\nThe number was %d better luck next time.\n", num);
    return;
}
Was it helpful?

Solution

Change wrong to use pointer argument for guess as

void wrong(int *guess, int num)
{
    if ((*guess) > num)
    {
        printf("\nYour guess is to high.", *guess);
        printf("\nTry again: ");
        scanf_s("%d", guess);
    }
    else if ((*guess) < num)
    {
        printf("\nYour guess is to low.", *guess);
        printf("\nTry again: ");
        scanf_s("%d", guess);
    }
}

this allows your external guess variable value to be changed by wrong

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top