Initialization

This commit is contained in:
Kozar 2024-03-21 17:01:57 +01:00
parent b07dd7c0a0
commit 00c0b044b6

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#define SIZE 100 #define SIZE 100
@ -34,7 +35,7 @@ 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;
@ -43,19 +44,22 @@ 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 %[^\n]", &votes, name) != 2) {
// error, input was not in expected format // check if sscanf successfully parsed the input
// print error message and exit if (sscanf(line, "%d %[^\n]", &votes, name) != 2 || votes < 0) {
break; // error, input was not in expected format or votes are negative
// print error message and continue to next line
fprintf(stderr, "Nepodarilo nacitat nic\n");
continue;
} }
// check if this student already exists // check if this student already exists
int found = 0; bool found = false;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (strcmp(students[i].name, name) == 0) { if (strcmp(students[i].name, name) == 0) {
// add votes to existing student // add votes to existing student
students[i].votes += votes; students[i].votes += votes;
found = 1; found = true;
break; break;
} }
} }