C语言中使用scanf或fgets时的段错误
Segmentation fault when using scanf or fgets in C language
我在我的一个编程项目中有一个项目 类 创建一个 20 个问题的游戏,该游戏递归地构建计算机在您回答问题时遵循的二叉树选择。我制作的节点文件已经过测试并且工作正常。但是,如果需要,我可以 post 它,但我相信我的问题出在我的主要文件中。
程序失败 运行:
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,我也根据一些 post 和教程尝试过 fgets。当代码完全相同时,fgets 甚至不会让我输入用户输入,如果有任何兴趣,只需将 scanf 更改为 fgets 以获得 newQuestion 输入。
提前感谢大家对类似问题的任何意见或推荐。
再次感谢大家
一行写着:
strcat(strcat("Is it a ", playerObj), "?")
导致了我遇到的问题。当我完全注释掉该行时,程序能够继续执行程序中的其他要点。正如用户 MikeCAT 提到的那样,我正在寻找更好的方法来做到这一点。
我在我的一个编程项目中有一个项目 类 创建一个 20 个问题的游戏,该游戏递归地构建计算机在您回答问题时遵循的二叉树选择。我制作的节点文件已经过测试并且工作正常。但是,如果需要,我可以 post 它,但我相信我的问题出在我的主要文件中。
程序失败 运行:
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,我也根据一些 post 和教程尝试过 fgets。当代码完全相同时,fgets 甚至不会让我输入用户输入,如果有任何兴趣,只需将 scanf 更改为 fgets 以获得 newQuestion 输入。
提前感谢大家对类似问题的任何意见或推荐。
再次感谢大家
一行写着:
strcat(strcat("Is it a ", playerObj), "?")
导致了我遇到的问题。当我完全注释掉该行时,程序能够继续执行程序中的其他要点。正如用户 MikeCAT 提到的那样,我正在寻找更好的方法来做到这一点。