From 48bfefad8c747ef1fd5d281574180201a8cec5ee Mon Sep 17 00:00:00 2001 From: mr314ot Date: Thu, 9 Oct 2025 08:48:47 +0200 Subject: [PATCH] du2 - 3 --- du2/program.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/du2/program.c b/du2/program.c index cec874f..416e054 100644 --- a/du2/program.c +++ b/du2/program.c @@ -17,18 +17,17 @@ int read_pizza(struct pizza *item) { char line2[LINESIZE]; //nazov - if (!fgets(line1, LINESIZE, stdin)) return 0; + if (!fgets(line1, sizeof(line1), stdin)) return 0; line1[strcspn(line1, "\n")] = 0; if (strlen(line1) == 0) return 0; //cena - if (!fgets(line2, LINESIZE, stdin)) return 0; + if (!fgets(line2, sizeof(line2), stdin)) return 0; line2[strcspn(line2, "\n")] = 0; - float value; - if (sscanf(line2, "%f", &value) != 1 || value == 0.0f) { - return 0; - } + float value = 0.0f; + if (sscanf(line2, "%f", &value) != 1) return 0; + if (value == 0.0f) return 0; strcpy(item->name, line1); item->price = value; @@ -49,12 +48,16 @@ int compare_pizza(const void *a, const void *b){ int main() { struct pizza list[MAXITEMS]; - int count = 0; + memset(list, 0, sizeof(list)); //nacitanie listka + int count = 0; while (count < MAXITEMS && read_pizza(&list[count])){ count++; } + + //ukoncit ak ziadne polozky + if (count == 0) return 0; //triedenie qsort(list, count, sizeof(struct pizza), compare_pizza);