C-编程HI-LO游戏

时间:2018-10-02 07:09:02

标签: c

#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;
        }
    }
}

有人可以帮我吗?在猜测了7次数字并“中断”之后,C表示存在错误“ Exception thrown at 0x586A4B02 (ucrtbased.dll) in ALLlabs.exe: 0xC0000005: Access violation reading location 0x00002563."

我试图到处搜索中断的问题是什么,但是我找不到问题所在。你们能告诉我代码有什么问题吗?您可以复制代码以尝试自己使用。

1 个答案:

答案 0 :(得分:2)

是这一行:

scanf('%c', &check);

它使用单引号-因此'%c'成为数字而不是字符串。

尝试一下:

scanf(" %c", &check);

请记住要在启用所有警告的情况下进行编译。不要忽略编译器警告!

"%c"之前使用some notes