gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:41:05 25 4
gpt4 key购买 nike

我的一个编程类(class)中有一个项目,要创建一个包含 20 个问题的游戏,该游戏会递归地构建一个二叉树,当您回答问题时,计算机会遵循该二叉树进行选择。我制作的节点文件已经过测试并且工作正常。不过,如果需要,我可以发布它,但我相信我的问题出在我的主文件中。

程序运行失败:

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("这是一个",playerObj),"?")

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

关于c - C语言中使用scanf或fgets时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33948360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com