簡體   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