From e9b1cca2e42c00d47fe3f526187a1e398411d252 Mon Sep 17 00:00:00 2001 From: st529yr Date: Wed, 20 Mar 2024 15:28:37 +0100 Subject: [PATCH] funguje --- cv5/program.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index bc93527..0d85018 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -34,12 +34,20 @@ int main() { char buffer[MAX_BUFFER_LENGTH]; while (fgets(buffer, sizeof(buffer), stdin) != NULL) { // Načítajte počet hlasov a meno študenta - int votes; - char name[MAX_NAME_LENGTH]; - if (sscanf(buffer, "%d %99s", &votes, name) != 2) { + char *token = strtok(buffer, " "); + if (token == NULL) { fprintf(stderr, "Chyba: Neplatný formát vstupu.\n"); return 1; } + int votes = atoi(token); + + token = strtok(NULL, "\n"); + if (token == NULL) { + fprintf(stderr, "Chyba: Neplatný formát vstupu.\n"); + return 1; + } + char name[MAX_NAME_LENGTH]; + strncpy(name, token, MAX_NAME_LENGTH); // Uložte študenta do poľa strncpy(students[num_students].name, name, MAX_NAME_LENGTH);