A Simple Voting Program

Hello everyone, I am new here. I am only a Tenth Grader, and my knowledge with regards to programming.

So this is our school project. The objective is to make a simple voting program with five candidates. The program would ask for the number of voters, and as it continues to run, people can encode their votes. It will start with just one display of "Vote Number %d, who do you vote for?", and the number will increase, and will only stop once the number of voters are met. Afterwards, it will total the number of votes for each candidate, and the program will show who won among the candidates. Then, once the voting is complete, the voters can either do a do-over, or end the program.

The problem is that the program abruptly ends by the time the first vote is tallied. Can anyone help? Thank you so much in advance!

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	int VO, y, z, x, a, b, c, d, e, f, g, h;
	char NA, FA, A, B, C, D, E;
	z=1;
	x=0;
        y=0;
	a=0;
	b=0;
	c=0;
	d=0;
	e=0;
	
	do
	{
        system("color 70");
        printf("\n________________________________________________________________________________\n\n");
	    printf("\nWelcome to the Presidential Election 2016!\n\n");
	    printf("\nVote NOW!\n\n");
	    printf("\n________________________________________________________________________________\n\n");
	    
	
		printf("\n\n Vote for Clementine Berkowitz - President");
	    printf("\n Background:");
	    printf("\n Starting out as an orphan in the aftermath of the demise of her parents, \n Clementine Haynes Berkowitz managed to climb");
	    printf("\n the ranks of law-enforcement, becoming one of the best detectives in the country. \n Seeing this as an");
	    printf("\n an opportunity to inspire others, Clementine is determined to lead the nation \n to a bright future..");
	    printf("\n\n There's always a light at the end of the tunnel. - Parker Chapman, 2016");
	    printf("\n________________________________________________________________________________\n\n");
		
		printf("\n\n Vote for Parker Chapman - President");
	    printf("\n Background:");
	    printf("\n Born from an impoverish family, Parker Roycelann Chapman \n found success after landing a scholarship in Harvard.");
	    printf("\n After hitting it big time as the district attorney of Metro Manila, \n Parker decided that from that point on,");
	    printf("\n he should dedicate his entire life in serving the country.");
	    printf("\n\n All things are possible through God! - Parker Chapman, 2016");
	    printf("\n________________________________________________________________________________\n\n");
	    
	   	printf("\n\n Vote for Stanley DeMille - President");
	    printf("\n Background:");
	    printf("\n Not a stranger to intense controversy, Stanley Holbrooke-Quentin DeMille \n was infamous for allegely being involved");
	    printf("\n in the previous administration's Infinity Fund Scam. Being a man of ambition, \n Stanley believes in second chances, and");
	    printf("\n he is aiming to clean-up the sins of the past!");
	    printf("\n\n Nothing is ever set in stone! - Stanley DeMille, 2016");
	    printf("\n________________________________________________________________________________\n\n");
	   
	    printf("\n\n Vote for Michael Frankleiss - President");
	    printf("\n Background:");
	    printf("\n At the age of thirteen, Michael Johns Frankeleiss had a fascination \n for law. At the same age, he was bullied.");
	    printf("\n Now at the age of twnety-one, Michael is one of the brightest lawyers around. \n With the nation's crime wave at");
	    printf("\n an all-time high, Michael is now aiming to eliminate the criminal element!.");
	    printf("\n\n I am no hero, but I'm here to save the day! - Michael Frankleiss, 2016");
	    printf("\n________________________________________________________________________________\n\n");
		
		printf("\n\n Vote for Erin Goodchild - President");
	    printf("\n Background:");
	    printf("\n By far, the youngest among all candidates, Erin L. Goodchild believes \n in the concept of transitivity ever.");
	    printf("\n since she started working in the world of journalism. Now, the country \n needs a leader who can stand for truth,");
	    printf("\n regardless of circumstance, and Erin is the right woman for the job!.");
	    printf("\n\n When the sun goes down, be a light of your own. - Erin Goodchild, 2016");
	    printf("\n________________________________________________________________________________\n\n");
			    
		printf("Enter Number of Voters: ");
		scanf("%d",&VO);
		
			
		while(z<=VO)
		{
			printf("\nVoter Number %d, who will you vote for?",z);
			scanf("%s",&NA);

                        while(NA!=0)
                        {

                        if(NA=='A' || 'a')
                        {
                        a++;			
                        }

                        else if(NA=='B' || 'b')
                        {
                        b++;			
                        }

                        else if(NA=='C' || 'c')
                        {
                        c++;			
                        }

                        else if(NA=='D' || 'd')
                        {
                        d++;			
                        }

                        else if(NA=='E' || 'e')
                        {
                        e++;			
                        }

                        else
                        {
                        printf("Invalid Vote!");
                        }
 
                        }
                         
                        for(x=0; x<a; x++);
                        {
                        printf("Clementine Berkowitz");
                        printf("*");
                        }


                        for(y=0; y<b; y++);
                        {
                        printf("Parker Chapman");
                        printf("*");
                        }

                        for(f=0; f<c; f++);
                        {
                        printf("Stanley DeMille");
                        printf("*");
                        }

                        for(g=0; g<d; g++);
                        {
                        printf("Michael Frankleiss");
                        printf("*");
                        }

                        for(h=0; h<e; h++);
                        {
                        printf("Erin Goodchild");
                        printf("*");
                        }

			if(a > b && b > c && c > d && d > e)
			{
				printf("\nClementine Berkowitz wins by %d votes.",x);
			}
			
			else if(b > c && c > d && d > e && e > a)
			{
                                printf("\nParker Chapman wins by %d votes.",x);
			}
			
			else if(c > d && d > e && e > a && a > b)
			{
                                printf("\nStanley DeMille wins by %d votes.",x);
			}
			
			else if(d > e && e > a && a > b && b > c)
			{
                                printf("\nMichael Frankleiss wins by %d votes.",x);
			}
			
			else if(e > a && a > b && b > c && c > d)
			{
                                printf("\nErin Goodchild wins by %d votes.",x);
			}
		z++;
		}
		
		
		printf("Would you like to vote again? (Y/N)");
		scanf("%s",&FA);
	}

	while(FA == 'Y' || FA == 'y');
	system("cls");
	printf("Thank you for voting!");
	
	getche();
}
to my knowledge i do not see any main or any libraries or any pause at the end of the code.
Last edited on
There are at least three problems in the above code.
line 68:
 
    scanf("%s",&NA);

The scanf function is expecting the input of a string %s, but at line 2 char NA the variable NA is defined as a character, not a string.
Line 68 should be
 
    scanf(" %c",&NA);

Note the %s is now %c. note also the blank space between the first " and the % symbol. That is important, it tells scanf() to ignore any whitespace characters before reading the value into NA.
http://www.cplusplus.com/reference/cstdio/scanf/

Next problem - the loop at line 70 while(NA!=0) doesn't serve any useful purpose. It tests that the ASCII code of the character entered from the keyboard is not zero. Of course that condition will usually be true, so the loop repeats forever. For now I'd simply remove that loop. If you want some way to force the user to vote for one of the candidates, you'd need some way to break out of the loop in the case of a valid vote, or to re-enter (another scanf() statement) in the case of an invalid one. But keep things simple for now, I'd leave that out, remove the loop.

Next problem. Line 73:
 
    if(NA=='A' || 'a')

That or condition is incorrect. You need to test each value separately,
 
    if (NA == 'A' || NA == 'a')


That's as far as I got, there may be other problems further on.
Last edited on
Thanks @axa643 and @Chervil for your insights! I'll test the code to see where it goes. :)
Topic archived. No new replies allowed.