Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include<stdio.h>
int main()
{
int x=0,y=0,z=0,d=0,vote,n,b_date,b_month,b_year,p_date,p_month,p_year,f_year,i;
char name[1000];
char add[1000];
printf("Enter Today's date:");
scanf("%d/%d/%d", &p_date, &p_month, &p_year);
printf("Enter the number of voters:");
scanf ("%d", &n);
for(i=1;i<=n;i++)
{
printf("Enter Name of voter %d:",i);
gets(name);
printf("\nEnter Adress of voter %d:",i);
gets(add);
printf("Enter Date of Birth of voter %d:",i);
scanf("%d/%d/%d",&b_date,&b_month,&b_year);
f_year = p_year - b_year;
if (f_year >= 18)
{
printf("Cast Your Vote\n");
printf("1)Vote for A\n");
printf("2)Vote for B\n");
printf("3)Vote for C\n");
printf("4)NOTA\n");
printf("Enter your choice:");
scanf("%d", &vote);
switch (vote)
{
case 1:
x++;
printf("Votes for A =%d\n", x);
printf("Votes for B=%d\n", y);
printf("Votes for C=%d\n", z);
printf("Votes for NOTA=%d\n", d);
break;
case 2:
y++;
printf("Votes for A=%d\n", x);
printf("Votes for B=%d\n", y);
printf("Votes for C=%d\n", z);
printf("Votes for NOTA=%d\n", d);
break;
case 3:
z++;
printf("Votes for A=%d\n", x);
printf("Votes for B=%d\n", y);
printf("Votes for C=%d\n", z);
printf("Votes for NOTA=%d\n", d);
break;
case 4:
d++;
printf ("Votes for A=%d\n", x);
printf ("Votes for B=%d\n", y);
printf ("Votes for C=%d\n", z);
printf ("Votes for NOTA=%d\n", d);
break;
default:
printf ("Invalid Choice\n");
}
}
else if(f_year<18)
{
printf("Sorry!You are not eligible to vote.\n");
}
}
if (x > y && x > z && x > d)
{
printf ("A won with %d votes", x);
}
else if (y > x && y > z && y > d)
{
printf ("B won with %d votes", y);
}
else if (z > x && z > y && z > d)
{
printf ("C won with %d votes", z);
}
else if (d > x && d > y && d > z)
{
printf ("Nobody won the election!");
}

return 0;
}


The code is for an votting machine it takes vote from user to decide who won.

What I have tried:

i have tried to add name and address of votters but its not working.The changes need to be made are between line 12 - 17
Posted
Updated 23-Jan-21 10:52am
v2

To add the details of voters you would need an array of voters. C does not have classes, so I imagine you need an array of each type, kept in sync (ugly as hell). This is why C is terrible, why are you learning it?

Also, post just the code with the issue, there's no line numbers in your code, your question is unclear and it's unclear if you've bothered using a debugger to have insight into your issue
 
Share this answer
 
Create a struct which can hold the details of a Voter: name, address, DOB, which way he voted, and so forth. This is probably best using pointers to chars to hold the names, addresses, and any other string-based data, then allocating new spaces on the heap using malloc rather than adding the whole array to each voter struct - or you will run out of stack space very quickly. (Stacks are not big, heap is.)

Then allocate an array of Voter struct objects, and fill each in as they are entered.

It sounds complicated, but it really isn't when you get your head round it!
 
Share this answer
 
Quote:
gets(name);
Do not use gets. Use fgets instead, e.g.
C
fgets(name, sizeof(name), stdin);

afterwards, you may use strtok(name, "\n"); to remove the spurious newline.
See, for instance C - scanf() vs gets() vs fgets() - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900