Question

Hello still getting the hang of C, this program should work! But it keeps crashing at void cheack(int *guess, int num)and I just don't know what to do to fix it. Does anyone have suggestions? Thanks.Also this program is supposed to make a random number 1-20 and the user is supposed to guess it and if prints out high or low for five tries until the user gets it right or loses.

#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 cheack(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;





    // Sets variables  
    num = ranNumGen();



    // 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("\nShows number to win %d\n", num); // shows number to win to check if it works
    scanf_s("%d", &guess);


    // Check function
    cheack(&guess, num);




    // End promts (will be its own function later)
    if (guess == num)
    {
        printf("\nGoodbye\n");
    }
    else
    {
        sorry(num);
    }
    return 0;
}

int ranNumGen()
{
    int range;
    srand(time(NULL));
    range = 20;
    range = (rand() % range + 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 cheack(int *guess, int num)
{
    int a;
    a = 0;
    while (a < 4)
    {
        if ((*guess) == num)
        {
            Right(num);
            break;
        }
        if ((*guess) > num)
        {
            printf("\nYour guess is to high.", *guess);
            printf("\nTry again: ");
            scanf_s("%d", &guess);
        }
        if ((*guess) < num)
        {
            printf("\nYour guess is to low.", *guess);
            printf("\nTry again: ");
            scanf_s("%d", &guess);
        }

    }

return;
}

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

scanf expects a pointer so the adress operator is not needed here:

  scanf_s("%d", guess);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top