From 0e5b2261b024d3ce15372aea900891a82b813444 Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 12 Nov 2024 19:12:14 +0000 Subject: [PATCH] Initializacia --- cv7/program.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 63ac077..e4fdfe0 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -12,7 +12,9 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') { + memset(buffer, 0, SIZE); + char* r = fgets(buffer, SIZE, stdin); + if (!r || buffer[0] == '\n') { return NULL; } buffer[strcspn(buffer, "\n")] = 0; @@ -38,22 +40,21 @@ void run_tree(struct tree* tree) { return; } - printf("%s\n", tree->value); - if (tree->value[0] == '*') { - printf("Koniec\n"); + // If it's an answer + printf("*%s\nKoniec\n", tree->value + 1); return; } - printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + printf("%s\n", tree->value); char response; - while (1) { - if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) { - printf("Nerozumiem\n"); - continue; - } - break; + if (scanf(" %c", &response) != 1) { + printf("Koniec vstupu\n"); + return; + } else if (response != 'a' && response != 'n') { + printf("Nerozumiem\n"); + return; } if (response == 'a') { @@ -83,9 +84,8 @@ void count_items(struct tree* tree, int* count) { } int main() { - printf("Expert z bufetu to vie.\n"); - struct tree* root = load_tree(); + printf("Expert z bufetu to vie.\n"); if (!root) { printf("Chybna databaza\n"); return 0; @@ -94,8 +94,10 @@ int main() { int count = 0; count_items(root, &count); printf("Pozna %d druhov ovocia a zeleniny.\n", count); + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); + destroy_tree(root); return 0; } \ No newline at end of file