This commit is contained in:
Tančáková 2024-03-20 15:28:37 +01:00
parent 3eb4c202d2
commit e9b1cca2e4

View File

@ -34,12 +34,20 @@ int main() {
char buffer[MAX_BUFFER_LENGTH]; char buffer[MAX_BUFFER_LENGTH];
while (fgets(buffer, sizeof(buffer), stdin) != NULL) { while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Načítajte počet hlasov a meno študenta // Načítajte počet hlasov a meno študenta
int votes; char *token = strtok(buffer, " ");
char name[MAX_NAME_LENGTH]; if (token == NULL) {
if (sscanf(buffer, "%d %99s", &votes, name) != 2) {
fprintf(stderr, "Chyba: Neplatný formát vstupu.\n"); fprintf(stderr, "Chyba: Neplatný formát vstupu.\n");
return 1; 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 // Uložte študenta do poľa
strncpy(students[num_students].name, name, MAX_NAME_LENGTH); strncpy(students[num_students].name, name, MAX_NAME_LENGTH);