-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
109 lines (89 loc) · 2.54 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#define MAX_CANDIDATES 10
// Structure to represent a candidate
typedef struct {
int id;
char name[50];
int votes;
} Candidate;
// Function to display the menu
void displayMenu() {
printf("----- Voting System -----\n");
printf("1. Add Candidate\n");
printf("2. Display Candidates\n");
printf("3. Vote\n");
printf("4. Exit\n");
printf("-------------------------\n");
printf("Enter your choice: ");
}
// Function to add a candidate
void addCandidate(Candidate candidates[], int* numCandidates) {
if (*numCandidates >= MAX_CANDIDATES) {
printf("Maximum number of candidates reached.\n");
return;
}
Candidate candidate;
candidate.id = *numCandidates + 1;
printf("Enter candidate name: ");
scanf("%s", candidate.name);
candidate.votes = 0;
candidates[*numCandidates] = candidate;
(*numCandidates)++;
printf("Candidate added successfully.\n");
}
// Function to display all candidates
void displayCandidates(Candidate candidates[], int numCandidates) {
if (numCandidates == 0) {
printf("No candidates found.\n");
return;
}
printf("Candidates:\n");
for (int i = 0; i < numCandidates; i++) {
printf("%d. %s - Votes: %d\n", candidates[i].id, candidates[i].name, candidates[i].votes);
}
}
// Function to cast a vote
void vote(Candidate candidates[], int numCandidates) {
if (numCandidates == 0) {
printf("No candidates found.\n");
return;
}
int candidateId;
printf("Enter the candidate ID to vote for: ");
scanf("%d", &candidateId);
for (int i = 0; i < numCandidates; i++) {
if (candidates[i].id == candidateId) {
candidates[i].votes++;
printf("Vote casted successfully.\n");
return;
}
}
printf("Invalid candidate ID.\n");
}
int main() {
Candidate candidates[MAX_CANDIDATES];
int numCandidates = 0;
int choice;
do {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addCandidate(candidates, &numCandidates);
break;
case 2:
displayCandidates(candidates, numCandidates);
break;
case 3:
vote(candidates, numCandidates);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
printf("\n");
} while (choice != 4);
return 0;
}