-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminivotingsystem.c
98 lines (85 loc) · 2.67 KB
/
minivotingsystem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#define MAX_CONTESTANTS 8
// Function to cast a vote
void CastVote(int votes[], int contestantCount)
{
int choice;
printf("Enter the contestant number you want to vote for: ");
scanf("%d", &choice);
// Validate the user's choice
if (choice >= 1 && choice <= contestantCount)
{
votes[choice - 1]++;
printf("Vote cast successfully!\n");
} else
{
printf("Invalid choice. Please enter a valid contestant number.\n");
}
}
// Function to show the total votes for each contestant
void ShowResults(int votes[], char contestants[][50], int contestantCount) {
printf("Voting Results:\n");
for (int i = 0; i < contestantCount; i++)
{
printf("%s: %d votes\n", contestants[i], votes[i]);
}
}
// Function to show the contestants
void ShowContestants(char contestants[][50], int numContestants)
{
printf("Contestants:\n");
for (int i = 0; i < numContestants; i++)
{
printf("%d. %s\n", i + 1, contestants[i]);
}
}
int main() {
char contestants[MAX_CONTESTANTS][50];
int votes[MAX_CONTESTANTS] = {0};
int numContestants;
// Get the number of contestants
printf("Enter the number of contestants (up to %d): ", MAX_CONTESTANTS);
scanf("%d", &numContestants);
// Validate the number of contestants
if (numContestants < 1 || numContestants > MAX_CONTESTANTS)
{
printf("Invalid number of contestants. Exiting program.\n");
return 1; // Exit with an error code
}
// Get the names of the contestants
for (int i = 0; i < numContestants; i++)
{
printf("Enter the name of contestant %d: ", i + 1);
scanf("%s", contestants[i]);
}
int choice;
do {
// Show menu
printf("\nMenu:\n");
printf("1. Show Contestants\n");
printf("2. Cast Vote for the Contestants\n");
printf("3. Show Results for each Contestants\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
ShowContestants(contestants, numContestants);
printf("\n");
break;
case 2:
CastVote(votes, numContestants);
break;
case 3:
ShowResults(votes, contestants, numContestants);
printf("\n");
break;
case 4:
printf("voting finished.Thank You for the participation!\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}