繁体   English   中英

C 语言中使用 scanf 或 fgets 时出现分段错误

[英]Segmentation fault when using scanf or fgets in C language

我在我的一个编程课程中有一个项目来创建一个 20 个问题的游戏,该游戏以递归方式构建一个二叉树,当您回答问题时计算机会遵循该选择。 我制作的 Node 文件已经过测试并且工作正常。 但是,如果需要,我可以发布它,但我相信我的问题出在我的主文件中。

程序运行失败:

Is it a dog?

Enter "y" for yes and "n" for no: n

YOU WIN!!!

What were you thinking of? fish

Please give me a yes or no questions to distinguish dog from fish: does it have scales?

Segmentation fault (core dumped)

该程序运行得非常好,并按预期执行,直到用户输入可区分的问题。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Node.h"
#define BUFF 1

//prototype methods
void startGame();
void askQuestion(Node* currentNode);

//Declare variables
Node* startNode;
Node* moveNode;
Node* makeNode;
char ans;
char newQuestion[1000];
char playerObj[1000];

void startGame() {
    //Allocate memory for altering decision tree when new questions are added
    startNode = (Node*) malloc(BUFF * sizeof(Node));
    moveNode = (Node*) malloc(BUFF * sizeof(Node));
    makeNode = (Node*) malloc(BUFF * sizeof(Node));

    //Make root node of tree
    setQuestion("Is it a dog?", startNode);
    setObjName("dog", startNode);

}

void askQuestion(Node* currentNode) {

    printf("%s\n", getQuestion(currentNode));//Ask user a question
    printf("Enter \"y\" for yes and \"n\" for no: ");//Prompt user for response
    scanf("%c", &ans);//Get user character from keyboard
    fflush(stdin);//Flush standard input to clear newline character

    //If the computer is right computer wins
    if((isObjNode(currentNode)) & (ans == 'y')){
        printf("COMPUTER WINS!!!");
        //this section will later start another game using the current decision tree

    //Move to next yes node in tree
    }else if(!(isObjNode(currentNode)) & (ans == 'y')){
        askQuestion(currentNode->yesPointer);

    //If at leaf node and computer is wrong notify user and expand tree
    }else if((isObjNode(currentNode)) & (ans == 'n')){
        printf("YOU WIN!!!\n");

        printf("What were you thinking of? ");
        scanf("%s", &playerObj);//Get what user was thinking of
        fflush(stdin);//Flush newline character

        printf("Please give me a yes or no questions to distinguish %s from             %s: ",currentNode->objName, playerObj);
        fflush(stdout);

        scanf("%s", &newQuestion);
        //I also tried:
        //fgets(newQuestion, sizeof(newQuestion), stdin);
        fflush(stdin);

        //PROGRAM SEGMENTATION FAULTS

        setQuestion(strcat(strcat("Is it a ", playerObj), "?"), makeNode);
        setObjName(playerObj, makeNode);

        moveNode = currentNode;

        printf("If the answer would be %s, what would the answer be y for    yes, n for no?",playerObj);
        scanf("%c", &ans);
        fflush(stdin);
        setQuestion(newQuestion, currentNode);

        if(ans == 'y'){
            setYesNodeRef(currentNode, makeNode);
            setNoNodeRef(currentNode, moveNode);
        }else if(ans == 'n'){
            setYesNodeRef(currentNode, moveNode);
            setNoNodeRef(currentNode, makeNode);
        }

    }else if(!(isObjNode(currentNode)) & (ans == 'n')){
        askQuestion(currentNode->noPointer);
    }

}

int main() {

    startGame();
    askQuestion(startNode);

}

所以,简而言之,我的问题是导致分段错误的原因是什么?

我已经查看了几个重新分级 scanf 的帖子,并且我也根据一些帖子和教程尝试了 fgets。 当代码完全相同时,fgets 甚至不会让我输入用户输入,只是将 scanf 更改为 fgets 以获取 newQuestion 输入(如果有兴趣的话)。

提前感谢大家对类似问题的任何信息的任何输入或推荐。

再次感谢大家。

一行写着:
strcat(strcat("Is it a ", playerObj), "?")

导致了我遇到的问题。 当我完全注释掉该行时,程序能够继续执行程序中的其他点。 正如用户 MikeCAT 所提到的,我正在寻找更好的方法来做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM