Initialization

This commit is contained in:
Kozar 2024-03-21 16:57:37 +01:00
parent a339ad8387
commit ae1b4851e9

View File

@ -1,7 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define SIZE 100 #define SIZE 100
@ -44,11 +43,11 @@ int main() {
// parse the string and extract number of votes and student name // parse the string and extract number of votes and student name
int votes; int votes;
char name[SIZE]; char name[SIZE];
if (sscanf(line, "%d %s", &votes, name) != 2 || !isdigit(name[0])) { if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
// error, input was not in expected format // error, input was not in expected format
// print error message and continue to next line // print error message and exit
fprintf(stderr, "Error: invalid input format\n"); fprintf(stderr, "Error: invalid input format\n");
continue; return 1;
} }
// check if this student already exists // check if this student already exists
@ -74,7 +73,7 @@ int main() {
qsort(students, size, sizeof(struct student), compare); qsort(students, size, sizeof(struct student), compare);
// print the results // print the results
printf("Results:\n"); printf("Vysledky:\n");
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
printf("%d %s\n", students[i].votes, students[i].name); printf("%d %s\n", students[i].votes, students[i].name);
} }