在c中猜数字游戏遇到了麻烦

时间:2017-07-24 10:04:07

标签: c

我是c语言的新手,所以如果我犯了一个愚蠢的错误,请原谅。我正在尝试实现一个迷你游戏,你猜测计算机在C中生成的密码。这是我到目前为止提出的代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>


int main(void)
{
    int c=1;
    bool running = 1;
    int x = rand() % 20;
    printf("The secret number is between 0 and 19, take a guess\n");
    int inp = getchar();
    while (running) {
        if (inp==x) {
            printf("Correct. Total number of guesses you spent was %i. Would you like to start a new game? Y/n", c);
            bool running=0;
        }
        else if (inp>x) {
            printf("guess smaller\n");
            scanf("%i", inp);
        }
        else if (inp<x) {
            printf("guess larger\n");
            scanf("%i", inp);
        }
        c+=1;   
    }
}

编译完成后,游戏遇到了麻烦:

zhiwei@zhiwei-Lenovo-Rescuer-15ISK:~/workspace$ ./guess
The secret number is between 0 and 19, take a guess
3
guess smaller
2
Segmentation fault (core dumped)

什么是“分段错误”?我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

由于此行,您有分段错误(核心转储)错误:

scanf("%i", inp);

scanf 方法将指针作为参数。现在程序正在尝试将新值写入内存,而不是为我们的程序保留。

您应该将此行替换为:

scanf("%i", &inp);

答案 1 :(得分:3)

您的代码存在的一个问题是您使用scanf时使用不当。 scanf的第一个参数是格式字符串,任何其他参数都应该是指针,因此:

// Incorrect
scanf("%i", inp);

// Correct
scanf("%i", &inp);

当您尝试访问无效的内存地址或您无权访问时,会发生分段错误。假设你运行你的程序,你写的第一个数字是3,所以inp = 3。到目前为止,一切都运行正常,因为您正在使用getchar读取第一个数字。但是,您使用scanf("%i", inp)scanf将在地址inp处写下一个数字,即地址3,这是您无法写入的地址!因此,您会遇到分段错误。

您的代码中还有另一个错误,但是存在逻辑错误。您使用getchar读取了第一个数字:如果您想输入大于9的数字,该怎么办?您也应该使用scanf进行第一次阅读。

...
printf("The secret number is between 0 and 19, take a guess\n");
int inp;

// Not good
inp = getchar();

// Good
scanf("%i", &inp);
...

此外,您的while循环将永远不会结束,即您的程序将永远运行(除非您将其终止)。确实,每当执行if块时

if (inp == x) {
    printf("Correct. Total number of guesses you spent was %i. Would you like to start a new game? Y/n", c);
    bool running = 0;
}

声明一个新变量bool running,它会覆盖先前声明的running变量。您应该将其替换为:

if (inp == x) {
    printf("Correct. Total number of guesses you spent was %i. Would you like to start a new game? Y/n", c);
    running = 0;
}

答案 2 :(得分:1)

除了正确指出分段错误原因的先前答案之外,还有更多问题。以下(我认为)你需要的是:

int main()
{
    int c = 1;
    bool running = 1;
    srand(time(0));
    int x = rand() % 20;
    printf("The secret number is between 0 and 19, take a guess\n");
    int inp;
    scanf("%i", &inp);
    while (running) {
        if (inp == x) {
            printf("Correct. Total number of guesses you spent was %i. Would you like to start a new game? Y/N", c);
            char a;
            scanf(" %c", &a);
            if (a == 'N' || a == 'n')
            {
                running = 0;
            }
            else
            {
                c = 0;
                srand(time(0));
                x = rand() % 20;
                printf("The secret number is between 0 and 19, take a guess\n");
                scanf("%i", &inp);
            }
        }
        else if (inp>x) {
            printf("guess smaller\n");
            scanf("%i", &inp);
        }
        else if (inp<x) {
            printf("guess larger\n");
            scanf("%i", &inp);
        }
        c += 1;
    }
}

注意事项。你需要在调用rand()之前给出一个种子值,否则你会得到一个相同顺序的数字序列。通常当前时间是一个足够好的种子。其次,您需要阅读问题的答案,用户是否希望继续,并采取相应的行动。在这里,您不能重新定义running(使用bool),否则您将创建一个新变量,而不是替换更高级别变量中的值。另请注意在提示输入char(“c%”)时使用空格。这是避免程序似乎跳过输入所必需的。最后,如果用户选择继续,则需要重置计数器和x。

相关问题