From c12528480bdde934424e89c25163dbeae6ccee1c Mon Sep 17 00:00:00 2001 From: ak643du Date: Thu, 21 Mar 2024 17:18:39 +0100 Subject: [PATCH] Initialization --- cv5/program.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index 125acae..b517c01 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -1,7 +1,6 @@ #include #include #include -#include // Include ctype.h for isdigit function #define SIZE 100 @@ -28,7 +27,6 @@ int compare(const void *a, const void *b) { int main() { struct student students[SIZE]; int size = 0; - int error = 0; // Flag to track if an error occurred // read student data from standard input while (1) { @@ -36,27 +34,19 @@ int main() { memset(line, 0, SIZE); char *r = fgets(line, SIZE, stdin); - // check if the line is empty or just contains newline character + // check if the line is empty if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) { - // end of input or empty line - if (size == 0 && !error) { - // If no valid input was processed and no error occurred, print error message and exit - fprintf(stderr, "Error: No valid input\n"); - return 1; - } + // end of input break; } // parse the string and extract number of votes and student name int votes; char name[SIZE]; - - // Use sscanf to parse the input line - if (sscanf(line, "%d %[^\n]", &votes, name) != 2 || !isdigit(votes)) { - // If sscanf fails to parse the input correctly or votes is not a number - // Set error flag and continue to the next line - error = 1; - continue; + if (sscanf(line, "%d %[^\n]", &votes, name) != 2) { + // error, input was not in expected format + // print error message and exit + break; } // check if this student already exists