facebook like button

05 June, 2011

program 60: sample election program

The need:
  This program is a sample C program, PHP modification of which can be used in voting of any club or association. I wrote this program to be used in sangam election when I did not know php.
The code:
--------------------------------------------
//A program for voting

#include<stdio.h>
main()
{
    FILE *fp;
    char z;
    struct post
    {
        int cand1, cand2, cand3,none;
    }treas,sec,pres;
    printf("Election for SANGAM...\n");
    printf("press ^ for next vote and press @ to count and store results\n");
    pres.cand1=0;
    pres.cand2=0;
    pres.cand3=0;
    pres.none=0;
   
    treas.cand1=0;
    treas.cand2=0;
    treas.cand3=0;
    treas.none=0;
   
    sec.cand1=0;
    sec.cand2=0;
    sec.cand3=0;
    sec.none=0;
   
    for(z='t';z!='\0';)
    {
    printf("\n\nEnter choice for pres . \n1. cand1\n2. cand2\n3. cand3\n4. none\n\t\t");
   
    lebel_1:
    z=getchar();
    fflush(stdin);
    switch(z)
    {
    case '1' :
              pres.cand1++;
              break;
    case '2' :
              pres.cand2++;
              break;
    case '3' :
              pres.cand3++;
              break;
    case '4' :
              pres.none++;
              break;
    default :printf("Please enter a valid choice !!\n\t\t");
              goto lebel_1;
              break;
    }
   
   
    printf("\n\nEnter choice for sec . \n1. cand1\n2. cand2\n3. cand3\n4. none\n\t\t");
   
    lebel_2:
    z=getchar();
    fflush(stdin);
    switch(z)
    {
    case '1' :
              sec.cand1++;
              break;
    case '2' :
              sec.cand2++;
              break;
    case '3' :
              sec.cand3++;
              break;
    case '4' :
              sec.none++;
              break;
    default :printf("Please enter a valid choice !!\n\t\t");
                goto lebel_2;
              break;
    }
   
       
    printf("\n\nEnter choice for treasurer . \n1. cand1\n2. cand2\n3. cand3\n4. none\n\t\t");
   
    lebel_3:
    z=getchar();
    fflush(stdin);
    switch(z)
    {
    case '1' :
              treas.cand1++;
              break;
    case '2' :
              treas.cand2++;
              break;
    case '3' :
              treas.cand3++;
              break;
    case '4' :
              treas.none++;
              break;
    default :printf("Please enter a valid choice !!\n\t\t");
                goto lebel_3;
              break;
    }
   
    printf("Your vote is successful ! Thank You !\n");
   
    printf("\n\n\n\n\n\n\nNext Vote ? y/n\n");
    while(z!='\0')
    {
    z=getchar();
    fflush(stdin);
    if(z=='^'||z=='@')
    break;
    }
    if(z=='^')
    continue;
    if(z=='@')
    break;
       
    }
   
    fp=fopen("Results.txt","w");
    printf("\nVote results are .\n\n");
    printf("President .\n\ncand1=%d\ncand2=%d\ncand3=%d\nnone=%d\n
\n",pres.cand1,pres.cand2,pres.cand3,pres.none);
    fprintf(fp,"President .\n\ncand1=%d\ncand2=%d\ncand3=%d\nnone=%d\n
\n",pres.cand1,pres.cand2,pres.cand3,pres.none);
    printf("Secretory .\n\ncand1=%d\ncand2=%d\ncand3=%d\nnone=%d\n
\n",sec.cand1,sec.cand2,sec.cand3,sec.none);
    fprintf(fp,"Secretory .\n\ncand1=%d\ncand2=%d\ncand3=%d\nnone=%d\n
\n",sec.cand1,sec.cand2,sec.cand3,sec.none);
    printf("Treasurer .\n\ncand1=%d\ncand2=%d\ncand3=%d\nnone=%d\n
\n",treas.cand1,treas.cand2,treas.cand3,treas.none);
    fprintf(fp,"Treasurer .\n\ncand1=%d\ncand2=%d\ncand3=%d\nnone=%d\n
\n",treas.cand1,treas.cand2,treas.cand3,treas.none);
    fclose(fp);
}
--------------------------------------------

The approach:
    The description of the problem is following. There are 3 posts (secretory, president and treasurer) and 3 candidates for each post. A person can either vote for a single person or choose none for a particular post. The approach to program is simple. We can have structures for each post. In each structure there will be counters for each candidate and none. The program pauses to take some input after a person is done with voting. Now if authorized person gives ^ as input, the program gets ready for next vote else if @ is entered the program prints results on screen, stores the results in a file "results.txt" and exits.

Remarks:
1. This program was not submitted there because I came to know that php programs were better for voting for some reasons.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply