C - programming HI-LO game

-1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#define PI 3.14159
#define PLANK_CONSTANT 6.626E-034
#define MASS_OF_ELECTRONS 9.109E-031
#define VELOCITY_OF_LIGHT 299800000

void hilo();

int main()
{
    char check;

    while (1)
    {
        hilo();
        printf("Would you like to play again (y/n)? ");
        scanf('%c', &check);
        if (check != 'y' || check != 'y')
        {
            break;
        }
    }

    system("pause");
    return 0;
}

void hilo()
{
    srand(time(NULL));

    int count = 0;
    int guess;
    int randomnumber = rand() % 101;

    while (1)
    {
        printf("Guess the number: ");
        scanf("%d", &guess);

        if (guess == randomnumber)
        {
            printf("Hooray, you have won!\n");
            break;
        }

        if (guess < randomnumber)
            printf("Wrong Number, Try again! The number you guessed is too low\n");
        if (guess > randomnumber)
            printf("Wrong Number, Try again! The number you guessed is too high\n");

        count++;

        if (count == 7)
        {
            printf("Sorry you lose, the number is: %d\n", randomnumber);
            break;
        }
    }
}

Can someone help me with this? After guessing the numbers for 7 times and "break" The C said there is an error "Exception thrown at 0x586A4B02 (ucrtbased.dll) in ALLlabs.exe: 0xC0000005: Access violation reading location 0x00002563."

I tried to search everywhere what's the problem with the break but I couldn't find what's the problem. Can you guys please tell me what's wrong with the code? You can copy the code to try to play with it yourself.

c
asked on Stack Overflow Oct 2, 2018 by Key • edited Oct 2, 2018 by Mike

1 Answer

2

It's this line:

scanf('%c', &check);

It's using single quotes - so the '%c' becomes a number, rather than a string.

Try this:

scanf(" %c", &check);

Remember to compile with all warnings enabled. Do not ignore a compiler warning!

There's some notes on using the space before "%c".

answered on Stack Overflow Oct 2, 2018 by Kingsley • edited Oct 2, 2018 by Kingsley

User contributions licensed under CC BY-SA 3.0