-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
135 lines (122 loc) · 3.35 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <time.h>
void opening();
void numbers(int position);
void votePres();
void voteGov();
void storeVote(int position);
int userVote;
int main()
{
opening();
numbers(0);
votePres();
storeVote(0);
numbers(1);
voteGov();
storeVote(1);
printf("\n ***** DONE! THANKS FOR VOTING! *****\n\n");
return 0;
}
void opening()
{
printf("\n|***************************************|\n");
printf("|__________ VOTING SYSTEM 1.0 __________|\n");
printf("|_______________________________________|\n");
}
// Show number of candidates.
void numbers(int position)
{
printf("\n\t|-----------------------|\n");
printf("\t| CANDIDATE\t NUMBER |\n");
printf("\t|-----------------------|\n");
if (position == 0){
printf("\t| Candidate 1\t %6d |\n", 11);
printf("\t| Candidate 2\t %6d |\n", 22);
printf("\t| Candidate 3\t %6d |\n", 33);
printf("\t|-----------------------|\n");
}else{
printf("\t| Candidate 1\t %6d |\n", 10);
printf("\t| Candidate 2\t %6d |\n", 20);
printf("\t|-----------------------|\n");
}
}
// Initialize and confirm user vote for President.
void votePres()
{
char confirmation;
do{
printf("\nType in the number of your PRESIDENT candidate: ");
int numRead = scanf("%d", &userVote);
if (numRead == 0)
{
printf("Invalid number\n");
while (getchar() != '\n');
}
else
{
switch (userVote)
{
case 11:
printf("You voted for Candidate 1\n");
break;
case 22:
printf("You voted for Candidate 2\n");
break;
case 33:
printf("You voted for Candidate 3\n");
break;
default:
printf("You voted NULL\n");
break;
}
printf("Do you confirm that?(Y/N): ");
scanf(" %c", &confirmation);
}
}while(confirmation == 'N' || confirmation == 'n');
}
// Initialize and confirm user vote for Governor.
void voteGov()
{
char confirmation;
do{
printf("\nType in the number of your GOVERNOR candidate: ");
int numRead = scanf("%d", &userVote);
if (numRead == 0)
{
printf("Invalid number\n");
while (getchar() != '\n');
}
else
{
switch (userVote)
{
case 10:
printf("You voted for Candidate 1\n");
break;
case 20:
printf("You voted for Candidate 2\n");
break;
default:
printf("You voted NULL\n");
break;
}
printf("Do you confirm that?(Y/N): ");
scanf(" %c", &confirmation);
}
}while(confirmation == 'N' || confirmation == 'n');
}
// Store the vote as President or Governor with data and hour.
void storeVote(int position)
{
time_t hour = time(NULL);
char *stringHour = ctime(&hour);
FILE *flpt;
flpt = fopen("logVotes.txt", "a+");
if (position == 0){
fprintf(flpt, "President: %d\t\t%s\n", userVote, stringHour);
}else{
fprintf(flpt, "Governor: %d\t\t%s\n", userVote, stringHour);
}
fclose(flpt);
}