From 7bd8fbba85be9127db7b2a39a578cc3e98ff42ef Mon Sep 17 00:00:00 2001 From: Vasylenko Date: Tue, 23 Apr 2024 11:05:21 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9Cprogram=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv10/program.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index ac3c104..d963cae 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -32,14 +32,13 @@ int main() { return 1; } - while (fgets(name, MAX_NAME_LENGTH, stdin) && num_students < max_students) { + while (fgets(name, MAX_NAME_LENGTH, stdin)) { name[strcspn(name, "\n")] = 0; if (strlen(name) == 0) { break; } - int duplicate = 0; for (int i = 0; i < num_students; i++) { if (strcmp(students[i], name) == 0) { @@ -49,13 +48,15 @@ int main() { } if (!duplicate) { - students[num_students] = strdup(name); - if (students[num_students] == NULL) { - puts("Memory allocation failed"); - free_memory(students, num_students); - return 1; + if (num_students < max_students) { + students[num_students] = strdup(name); + if (students[num_students] == NULL) { + puts("Memory allocation failed"); + free_memory(students, num_students); + return 1; + } + num_students++; } - num_students++; } } @@ -69,18 +70,10 @@ int main() { puts("Prijati studenti:"); for (int i = 0; i < num_students; i++) { - if (i < max_students) { - puts(students[i]); - } else { - if (i == max_students) { - puts("Neprijati studenti:"); - } - puts(students[i]); - } + puts(students[i]); } free_memory(students, num_students); return 0; } -