我在我的一个编程类中有一个项目来创建一个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);
}
那么,简而言之,我的问题是导致分段错误的原因是什么?
我已经看了几个post regrading scanf,我也根据一些帖子和教程尝试了fgets。当代码完全相同时,fgets甚至不会让我输入用户输入,如果感兴趣,只需将scanf更改为fgets for newQuestion输入。
提前感谢大家对类似问题的任何信息的任何输入或推荐。
答案 0 :(得分:0)
再次感谢大家。
行内容:
strcat(strcat("Is it a ", playerObj), "?")
造成了我遇到的问题。当我完全注释掉该行时,该程序能够继续进入该程序的其他点。我正在研究用户MikeCAT提到的更好的方法。