#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define FLUSH_STDIN(x) {if(x[strlen(x)-1]!='\n'){do fgets(Junk,16,stdin);while(Junk[strlen(Junk)-1]!='\n');}else x[strlen(x)-1]='\0';}
char Junk[16];
struct tree_element {
char animal[80];
char question[80];
struct tree_element *parent;
struct tree_element *choiceYes, *choiceNo; /*Children of this element*/
};
typedef struct tree_element node;
void fixNewLines(char *input);
void stringToLower(char *input);
void getAnswer(char *input);
void askQuestion(node *treeNode);
void guessAnimal(node *treeNode);
void giveUp(node *newNode);
void makeNode(node *treeNode, node *newNode, char newAnimal[80],
char newQuestion[80]);
int main(void){
int keepGoing;
node *rootNode;
char animal1[80];
char question1[80];
strcpy(animal1, "Zebra\n");
strcpy(question1,"Does it have stripes?\n");
makeNode(NULL, rootNode, animal1, question1);
printf((*rootNode).animal);
keepGoing = 1;
while (keepGoing) {
askQuestion(rootNode);
printf("Continue?\n");
char input[5];
getAnswer(input);
keepGoing = (strcmp(input, "yes") == 0) ? 1 : 0;
}
return EXIT_SUCCESS;
}
void stringToLower(char *input){
int i;
int size;
size = strlen(input);
for(i = 0; i < size; i++){
input[i] = tolower(input[i]);
}
}
void fixNewLines(char *input){
int i, size;
FLUSH_STDIN(input);
size = strlen(input);
for(i = 0; i < size; i++){
if(input[i] == '\n'){
input[i] = '\0';
}
}
}
/* my stuff */
void getAnswer(char *input) {
char ans[5];
fgets(ans, 5, stdin);
fixNewLines(ans);
stringToLower(ans);
strcpy(input, ans);
}
void askQuestion(node *treeNode) {
printf("D:<");
printf("%s\nEnter yes or no:\n", (*treeNode).question);
char input[5];
getAnswer(input);
if (strcmp(input, "yes") == 0) {
if (((*treeNode).choiceYes == 0)) {
printf("I lost ._.\n");
guessAnimal(treeNode); /* Reached end of yes path */
}
else {
printf("
askQuestion((*treeNode).choiceYes); /* Continue along yes */
}
}
else if (strcmp(input, "no") == 0) {
if (!((*(*treeNode).choiceNo).animal)) {
if (!treeNode->parent) guessAnimal(treeNode->parent);
else giveUp(treeNode);/* Reached end of no path */
}
else askQuestion((*treeNode).choiceNo);/* Continue along no */
}
else printf("Input must be yes or no\n");
}
void guessAnimal(node *treeNode) {
printf("Is your animal a %s\n", treeNode->animal);
char input[5];
getAnswer(input);
if (strcmp(input, "yes") == 0) {
printf("I win! I guessed your animal!\n");
}
else giveUp(treeNode);
}
void giveUp(node *treeNode) {
char *newAnimal, *newQuestion;
printf("What animal are you thinking of?\n");
fgets(newAnimal, 80, stdin);
printf("What question would describe this animal?\n");
fgets(newQuestion, 80, stdin);
printf("You win. I didn't get your animal. D=\n");
makeNode(treeNode, (*treeNode).choiceNo, newAnimal, newQuestion);
}
/**
* TODO: This is supposed to create a new node to branch off of the node
* treeNode, with user-given data as the other parameters. However, this doesn't
* work; Is it possible to fix this function.
*/
void makeNode(node *treeNode, node *newNode, char newAnimal[80], char newQuestion[80])
{
node *tempNode = (node *)malloc(sizeof(node));
strncpy((*tempNode).animal,newAnimal, sizeof((*tempNode).animal));
strncpy((*tempNode).question, newQuestion, sizeof((*tempNode).question));
(*tempNode).parent = treeNode;
(*tempNode).choiceYes = (*tempNode).choiceNo = NULL;
newNode = tempNode;
}
]
The last node is not copying animal1 or question1 and I dont know how to fix this.

New Topic/Question
Reply


MultiQuote


|