From df4e8d802d918be28de70f8c6166371a971d69d3 Mon Sep 17 00:00:00 2001 From: Miloslav Macko Date: Mon, 15 Apr 2024 22:17:07 +0200 Subject: [PATCH] 2333 --- cv10/program.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index 3d3c193..0102a02 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -15,6 +15,13 @@ int main() { memset(pole_smernikov, 0, SIZE * sizeof(char*)); int pocet_mien_v_poli = 0; + // Načítanie maximálneho počtu študentov + int max_students; + if (scanf("%d\n", &max_students) != 1 || max_students <= 0) { + puts("Nespravny vstup"); + return 1; + } + char line[SIZE]; while (fgets(line, SIZE, stdin) != NULL) { line[strcspn(line, "\n")] = 0; // Remove the newline character @@ -37,22 +44,25 @@ int main() { } } + if (pocet_mien_v_poli == 0) { + puts("Ziadne prihlasky"); + return 1; + } + // Triedenie qsort(pole_smernikov, pocet_mien_v_poli, sizeof(char*), compare); // Výpis prijatých a neprijatých študentov - int max_students; - fgets(line, SIZE, stdin); - sscanf(line, "%d", &max_students); - - printf("Prijati studenti:\n"); + puts("Prijati studenti:"); for (int i = 0; i < max_students && i < pocet_mien_v_poli; i++) { printf("%s\n", pole_smernikov[i]); } - printf("Neprijati studenti:\n"); - for (int i = max_students; i < pocet_mien_v_poli; i++) { - printf("%s\n", pole_smernikov[i]); + if (max_students < pocet_mien_v_poli) { + puts("Neprijati studenti:"); + for (int i = max_students; i < pocet_mien_v_poli; i++) { + printf("%s\n", pole_smernikov[i]); + } } // Uvoľnenie pamäte