diff --git a/cv7/program.c b/cv7/program.c index 3934d8f..54fe039 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -90,38 +90,52 @@ void interact(struct tree *node) //while (scanf(" %c", &answer) == 1) char answer[SIZE]; - while (fgets(answer, SIZE, stdin)) + while (1) { - if (answer[0] == '\n') + + if (fgets(answer, sizeof(answer), stdin) == NULL) { printf("Koniec vstupu\n"); - return; + break; } - answer[strcspn(answer, "\n")] = 0; + size_t len = strlen(answer); - if (strcmp(answer, "a") == 0) + if (answer[len - 1] == '\n') { + answer[len - 1] = '\0'; + } + + if (strlen(answer) != 1 || (answer[0] != 'a' && answer[0] != 'n')) + { + printf("Nerozumiem\n"); + break; + } + + if (answer[0] == 'a') { interact(node->left); return; - } else if (strcmp(answer, "n") == 0) + } + else if (answer[0] == 'n') { interact(node->right); return; - } else - { - printf("Nerozumiem\n"); - getchar(); } + } } int main() { struct tree *root = read_tree(); - getchar(); + int leafs = count_leaf_nodes(root); + if(leafs == 0) + { + printf("Chybna databaza\n"); + return 0; + } printf("Expert z bufetu to vie.\n"); - printf("Pozna %d druhov ovocia a zeleniny.\n", count_leaf_nodes(root)); + printf("Pozna %d druhov ovocia a zeleniny.\n", leafs); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); if (root == NULL) {