Initialization
This commit is contained in:
parent
b721c84406
commit
fcac30a233
@ -19,7 +19,7 @@ int compare(const void *a, const void *b) {
|
|||||||
} else if (student_a->votes < student_b->votes) {
|
} else if (student_a->votes < student_b->votes) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
// if votes are the same, compare names
|
// if the votes are the same, compare names
|
||||||
return strcmp(student_a->name, student_b->name);
|
return strcmp(student_a->name, student_b->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,20 +34,27 @@ int main() {
|
|||||||
memset(line, 0, SIZE);
|
memset(line, 0, SIZE);
|
||||||
char *r = fgets(line, SIZE, stdin);
|
char *r = fgets(line, SIZE, stdin);
|
||||||
|
|
||||||
// check if the line is empty
|
// check if the line is empty or just contains newline character
|
||||||
if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) {
|
if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) {
|
||||||
// end of input
|
// end of input
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the line was truncated
|
||||||
|
if (strlen(line) == SIZE - 1 && line[SIZE - 2] != '\n') {
|
||||||
|
// line was truncated, handle the error
|
||||||
|
fprintf(stderr, "Error: input line is too long\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// 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 %[^\n]", &votes, name) != 2) {
|
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 exit
|
// print error message and exit
|
||||||
printf("Nepodarilo nacitat nic\n");
|
fprintf(stderr, "Error: invalid input format\n");
|
||||||
break;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if this student already exists
|
// check if this student already exists
|
||||||
|
Loading…
Reference in New Issue
Block a user