From ea0f234a7f0b95a9ee2644be32f837184109455e Mon Sep 17 00:00:00 2001 From: st529yr Date: Wed, 13 Mar 2024 11:10:55 +0100 Subject: [PATCH] funguje --- cv5/program.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index f380ed2..8da626a 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -4,16 +4,18 @@ #define MAX_NAME_LENGTH 100 +// Structure to hold candidate information typedef struct { char name[MAX_NAME_LENGTH]; int votes; } Candidate; +// Comparator function for qsort int compare_candidates(const void *a, const void *b) { const Candidate *candidate_a = (const Candidate *)a; const Candidate *candidate_b = (const Candidate *)b; - // Sort by number of votes in descending order + // Sort by total votes in descending order if (candidate_a->votes != candidate_b->votes) { return candidate_b->votes - candidate_a->votes; } else { @@ -32,7 +34,7 @@ int main() { char name[MAX_NAME_LENGTH]; // Try to read votes and name - if (scanf("%d %99s", &votes, name) != 2) { + if (scanf("%d ", &votes) != 1) { if (feof(stdin)) { break; // End of input } else { @@ -41,6 +43,15 @@ int main() { } } + // Read candidate name + if (fgets(name, MAX_NAME_LENGTH, stdin) == NULL) { + printf("Error: Failed to read candidate name.\n"); + return 1; + } + + // Remove trailing newline character + name[strcspn(name, "\n")] = '\0'; + // Check if the candidate already exists int found = 0; for (int i = 0; i < num_candidates; i++) {