Advertisement
dmilicev

voting_system.c

Dec 4th, 2019
325
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.52 KB | None
  1. /*
  2.  
  3.     voting_system.c
  4.  
  5.     Task:
  6.  
  7.     https://scontent.fbeg7-1.fna.fbcdn.net/v/t1.15752-9/79090088_2557402667827007_2246587501504364544_n.png?_nc_cat=108&_nc_ohc=xZavIJ8pweYAQn3PHIaGRv8ABXRuOaKcynjHDGpEKfnjyw5JEMRdJ4RPw&_nc_ht=scontent.fbeg7-1.fna&oh=25240f9c364557059e776852ea1a58e4&oe=5E6D8BE6
  8.  
  9.     https://scontent.fbeg7-1.fna.fbcdn.net/v/t1.15752-9/79265770_727426401076495_1186445090151727104_n.jpg?_nc_cat=102&_nc_ohc=u5LhbMTsrI0AQmzd4xCRDWs-sRAjnfppZhWvfn6vJZUjcvn7VFDBbpB_w&_nc_ht=scontent.fbeg7-1.fna&oh=397d73147916bc2933a6a402c108385e&oe=5E72C34E
  10.  
  11.  
  12.  
  13.     You can find all my C programs at Dragan Milicev's pastebin:
  14.  
  15.     https://pastebin.com/u/dmilicev
  16.  
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <conio.h>
  21. #include <string.h>
  22.  
  23.  
  24. // fill the bank of 10 users names and their passwords
  25. void fill_users_names_and_passwords( char users_names_arr[10][20], char users_pass_arr[10][20] )
  26. {
  27.     int i;
  28.  
  29.     for( i=0; i<10; i++)
  30.     {
  31.         sprintf( users_names_arr[i], "User%d", i );
  32.         sprintf( users_pass_arr[i],  "pass%d", i );
  33.     }
  34.  
  35. /*  // or
  36.     strcpy( users_names_arr[0], "User0" );
  37.     strcpy( users_pass_arr[0],  "pass0" );
  38.  
  39.     strcpy( users_names_arr[1], "User1" );
  40.     strcpy( users_pass_arr[1],  "pass1" );
  41.  
  42.     strcpy( users_names_arr[2], "User2" );
  43.     strcpy( users_pass_arr[2],  "pass2" );
  44.  
  45.     strcpy( users_names_arr[3], "User3" );
  46.     strcpy( users_pass_arr[3],  "pass3" );
  47.  
  48.     strcpy( users_names_arr[4], "User4" );
  49.     strcpy( users_pass_arr[4],  "pass4" );
  50.  
  51.     strcpy( users_names_arr[5], "User5" );
  52.     strcpy( users_pass_arr[5],  "pass5" );
  53.  
  54.     strcpy( users_names_arr[6], "User6" );
  55.     strcpy( users_pass_arr[6],  "pass6" );
  56.  
  57.     strcpy( users_names_arr[7], "User7" );
  58.     strcpy( users_pass_arr[7],  "pass7" );
  59.  
  60.     strcpy( users_names_arr[8], "User8" );
  61.     strcpy( users_pass_arr[8],  "pass8" );
  62.  
  63.     strcpy( users_names_arr[9], "User9" );
  64.     strcpy( users_pass_arr[9],  "pass9" );
  65. */
  66. } // fill_users_names_and_passwords()
  67.  
  68.  
  69. void fill_candidates_names_arr( char Mayor_name_arr[3][20],     int Mayor_vote_arr[3],
  70.                                 char V_Mayor_name_arr[3][20],   int V_Mayor_vote_arr[3],
  71.                                 char Secretary_name_arr[3][20], int Secretary_vote_arr[3],
  72.                                 char Treasurer_name_arr[3][20], int Treasurer_vote_arr[3] )
  73. {
  74.     int i;
  75.  
  76.     strcpy( Mayor_name_arr[0], "Maria" );
  77.     strcpy( Mayor_name_arr[1], "Jose" );
  78.     strcpy( Mayor_name_arr[2], "Xhi" );
  79.  
  80.     for( i=0; i<3; i++)         // reset votes
  81.         Mayor_vote_arr[i] = 0;
  82.  
  83.     strcpy( V_Mayor_name_arr[0], "Estella" );
  84.     strcpy( V_Mayor_name_arr[1], "Agnes" );
  85.     strcpy( V_Mayor_name_arr[2], "Marley" );
  86.  
  87.     for( i=0; i<3; i++)         // reset votes
  88.         V_Mayor_vote_arr[i] = 0;
  89.  
  90.     strcpy( Secretary_name_arr[0], "Janinen" );
  91.     strcpy( Secretary_name_arr[1], "Ariel" );
  92.     strcpy( Secretary_name_arr[2], "Gerard" );
  93.  
  94.     for( i=0; i<3; i++)         // reset votes
  95.         Secretary_vote_arr[i] = 0;
  96.  
  97.     strcpy( Treasurer_name_arr[0], "Jenalyn" );
  98.     strcpy( Treasurer_name_arr[1], "Rhea" );
  99.     strcpy( Treasurer_name_arr[2], "Marife" );
  100.  
  101.     for( i=0; i<3; i++)         // reset votes
  102.         Treasurer_vote_arr[i] = 0;
  103.  
  104. } // fill_candidates_names_arr()
  105.  
  106.  
  107. void display_candidates_and_votes( char Mayor_name_arr[3][20],     int Mayor_vote_arr[3],
  108.                                    char V_Mayor_name_arr[3][20],   int V_Mayor_vote_arr[3],
  109.                                    char Secretary_name_arr[3][20], int Secretary_vote_arr[3],
  110.                                    char Treasurer_name_arr[3][20], int Treasurer_vote_arr[3] )
  111. {
  112.     int i;
  113.  
  114.     printf("\n List of all candidates and their votes: \n");
  115.  
  116.     printf("\n Mayor \n");
  117.     for(i=0;i<3;i++)
  118.         printf("\n %d. %s \t %d ", i+1, Mayor_name_arr[i], Mayor_vote_arr[i] );
  119.  
  120.     printf("\n\n Vice - Mayor \n");
  121.     for(i=0;i<3;i++)
  122.         printf("\n %d. %s \t %d ", i+1, V_Mayor_name_arr[i], V_Mayor_vote_arr[i] );
  123.  
  124.     printf("\n\n Secretary \n");
  125.     for(i=0;i<3;i++)
  126.         printf("\n %d. %s \t %d ", i+1, Secretary_name_arr[i], Secretary_vote_arr[i] );
  127.  
  128.     printf("\n\n Treasurer \n");
  129.     for(i=0;i<3;i++)
  130.         printf("\n %d. %s \t %d ", i+1, Treasurer_name_arr[i], Treasurer_vote_arr[i] );
  131.  
  132.  
  133.     printf("\n");
  134.  
  135. } // display_candidates_and_votes()
  136.  
  137.  
  138. void display_candidates( char text[], char name_arr[3][20] )
  139. {
  140.     int i;
  141.  
  142.     printf("%s", text);
  143.  
  144.     for(i=0;i<3;i++)
  145.         printf("\n %d. %s ", i+1, name_arr[i] );
  146.  
  147.     printf("\n");
  148.  
  149. } // display_candidates()
  150.  
  151.  
  152. void display_all_candidates( char Mayor_name_arr[3][20],
  153.                              char V_Mayor_name_arr[3][20],
  154.                              char Secretary_name_arr[3][20],
  155.                              char Treasurer_name_arr[3][20] )
  156. {
  157.     display_candidates("\n Mayor:\n", Mayor_name_arr );
  158.  
  159.     display_candidates("\n V_Mayor:\n", V_Mayor_name_arr );
  160.  
  161.     display_candidates("\n Secretary:\n", Secretary_name_arr );
  162.  
  163.     display_candidates("\n Treasurer:\n", Treasurer_name_arr );
  164. } // display_all_candidates()
  165.  
  166.  
  167. // return 1 if in bank exist user_name and user_pass, otherwise return 0
  168. int exist_in_bank( char users_names_arr[10][20], char users_pass_arr[10][20],
  169.                  char user_name[20], char user_pass[20] )
  170. {
  171.     int i, exist=0;
  172.  
  173.     for( i=0; i<10; i++)
  174.     {
  175.         if( strcmp(users_names_arr[i],user_name)==0 && strcmp(users_pass_arr[i],user_pass)==0 )
  176.             exist = 1;              // 1 if user_name and user_pass exists in bank
  177.     }
  178.  
  179.     return exist;
  180.  
  181. } // exist_in_bank()
  182.  
  183.  
  184. // returns 1 on success, otherwise returns 0 (for more than 3 attempts)
  185. int user_login( char user_name[20], char user_pass[20],
  186.                 char users_names_arr[10][20], char users_pass_arr[10][20] )
  187. {
  188.     int number_of_login_attempts = 0;
  189.     int end = 0;
  190.  
  191.     while( !end && number_of_login_attempts<=2 )
  192.     {
  193.         number_of_login_attempts++;     // count user number_of_login_attempts
  194.  
  195.         // Enter user login name
  196.         printf("\n\n Enter user login name: ");
  197.         scanf( "%s", user_name );
  198.  
  199.         // Enter user login password
  200.         printf("\n Enter user login password: ");
  201.         scanf( "%s", user_pass );
  202.  
  203.         if( exist_in_bank(users_names_arr, users_pass_arr, user_name, user_pass) )
  204.             end = 1;
  205.         else
  206.             printf("\n Wrong data. Try %d more times \n", 3 - number_of_login_attempts );
  207.     }
  208.  
  209.     return end;
  210.  
  211. } // user_login()
  212.  
  213.  
  214. // returns 0 if user voted for first candidate
  215. // returns 1 if user voted for second candidate
  216. // returns 2 if user voted for third candidate
  217. int get_vote( void )
  218. {
  219.     int num = 0;
  220.  
  221.     while ( !num )
  222.     {
  223.         printf("\n Enter the number of candidate you are voting for :  ");
  224.         scanf("%d", &num );
  225.  
  226.         if ( num < 1 || num > 3 )
  227.         {
  228.             printf("\n Vote must be 1, 2 or 3.  Try again ! \n");
  229.             num = 0;
  230.         }
  231.     }
  232.  
  233.     return (num-1);
  234.  
  235. } // get_vote()
  236.  
  237.  
  238. void vote( char Mayor_name_arr[3][20],     int Mayor_vote_arr[3],
  239.            char V_Mayor_name_arr[3][20],   int V_Mayor_vote_arr[3],
  240.            char Secretary_name_arr[3][20], int Secretary_vote_arr[3],
  241.            char Treasurer_name_arr[3][20], int Treasurer_vote_arr[3] )
  242. {
  243.     display_candidates("\n Mayor candidates \n", Mayor_name_arr );  // display candidates
  244.     Mayor_vote_arr[ get_vote() ]++;             // we are increasing the number of votes
  245.  
  246.     display_candidates("\n V_Mayor candidates \n", V_Mayor_name_arr );  // display candidates
  247.     V_Mayor_vote_arr[ get_vote() ]++;           // we are increasing the number of votes
  248.  
  249.     display_candidates("\n Secretary candidates \n",  Secretary_name_arr ); // display candidates
  250.     Secretary_vote_arr[ get_vote() ]++;         // we are increasing the number of votes
  251.  
  252.     display_candidates("\n Treasurer candidates \n",  Treasurer_name_arr ); // display candidates
  253.     Treasurer_vote_arr[ get_vote() ]++;         // we are increasing the number of votes
  254.  
  255. } // vote()
  256.  
  257.  
  258. // return -1 if no one is elected because of equal number of votes
  259. // return  0 if first  candidate is elected
  260. // return  1 if second candidate is elected
  261. // return  2 if third  candidate is elected
  262. int elected( char name_arr[3][20], int vote_arr[3] )
  263. {
  264.     int i, max, index, number_of_the_same_votes=0;
  265.  
  266.     max = vote_arr[0];          // suppose the first candidate has the highest number of votes
  267.     index = 0;                  // we remember his index in array
  268.  
  269.     for( i=1; i<3; i++ )        // compare him to other two candidates
  270.         if( max < vote_arr[i] )
  271.         {
  272.             max = vote_arr[i];  // then he has max votes
  273.             index = i;          // we remember his index in array
  274.         }
  275.  
  276.     for( i=0; i<3; i++ )        // count same votes
  277.         if( max == vote_arr[i] )
  278.             number_of_the_same_votes++;
  279.  
  280.     if( number_of_the_same_votes > 1 )
  281.         return -1;              // no one is elected because of equal number of votes
  282.     else
  283.         return index;           // return array index of elected candidate
  284.  
  285. } // elected()
  286.  
  287.  
  288. void display_result_of_voting( char Mayor_name_arr[3][20],     int Mayor_vote_arr[3],
  289.                                char V_Mayor_name_arr[3][20],   int V_Mayor_vote_arr[3],
  290.                                char Secretary_name_arr[3][20], int Secretary_vote_arr[3],
  291.                                char Treasurer_name_arr[3][20], int Treasurer_vote_arr[3]  )
  292. {
  293.     int index;
  294.     //char elected_candidate[20];
  295.  
  296.     printf("\n ______________________________________________________ \n\n");
  297.     printf("\n \t\t\t List of Nominated Candidates \n\n");
  298.  
  299.     index = elected( Mayor_name_arr, Mayor_vote_arr );
  300.     if ( index == -1 )
  301.         printf("\n Mayor          :  %2d \t %s \n", index, "No one is elected because of same number of votes." );
  302.     else
  303.         printf("\n Mayor          :  %2d \t %s \n", Mayor_vote_arr[index], Mayor_name_arr[index] );
  304.  
  305.     index = elected( V_Mayor_name_arr, V_Mayor_vote_arr );
  306.     if ( index == -1 )
  307.         printf("\n Vice - Mayor   :  %2d \t %s \n", index, "No one is elected because of same number of votes." );
  308.     else
  309.         printf("\n Vice - Mayor   :  %2d \t %s \n", V_Mayor_vote_arr[index], V_Mayor_name_arr[index] );
  310.  
  311.  
  312.     index = elected( Secretary_name_arr, Secretary_vote_arr );
  313.     if ( index == -1 )
  314.         printf("\n Secretary      :  %2d \t %s \n", index, "No one is elected because of same number of votes." );
  315.     else
  316.         printf("\n Secretary      :  %2d \t %s \n", Secretary_vote_arr[index], Secretary_name_arr[index] );
  317.  
  318.  
  319.     index = elected( Treasurer_name_arr, Treasurer_vote_arr );
  320.     if ( index == -1 )
  321.         printf("\n Treasurer      :  %2d \t %s \n", index, "No one is elected because of same number of votes." );
  322.     else
  323.         printf("\n Treasurer      :  %2d \t %s \n", Treasurer_vote_arr[index], Treasurer_name_arr[index] );
  324.  
  325.  
  326.     printf("\n\n Thank you for Voting !!! \n\n");
  327.  
  328. } // display_result_of_voting()
  329.  
  330.  
  331.  
  332. int main()
  333. {
  334.     char user_name[20];                 // name of user who will vote
  335.     char user_pass[20];                 // pass of user who will vote
  336.  
  337.     char users_names_arr[10][20];       // bank of 10 users with their names long max 20 characters
  338.     char users_pass_arr[10][20];        // bank of 10 passwords each long max 20 characters
  339.  
  340.     char Mayor_name_arr[3][20];         // 3 candidates names long max 20 characters
  341.     char V_Mayor_name_arr[3][20];
  342.     char Secretary_name_arr[3][20];
  343.     char Treasurer_name_arr[3][20];
  344.  
  345.     int Mayor_vote_arr[3];              // 3 candidates and their int number of votes
  346.     int V_Mayor_vote_arr[3];
  347.     int Secretary_vote_arr[3];
  348.     int Treasurer_vote_arr[3];
  349.  
  350.     int number_of_login_attempts = 0;
  351.     int end = 0;                        // flag 0-1
  352.     int i;
  353.     char ch;                            // char for answer (Y / N)
  354.  
  355.  
  356.     // initialize user names and passwords for 10 users
  357.     fill_users_names_and_passwords( users_names_arr, users_pass_arr );
  358.  
  359.     // display users names and their passwords
  360.     // (put in comment if you don't want to display this)
  361.     for(i=0;i<10;i++)
  362.         printf("\n users_names_arr[%d] = %s \t users_pass_arr[%d] = %s \n",
  363.                 i, users_names_arr[i], i, users_pass_arr[i] );
  364.  
  365.     // Initialize all candidates
  366.     fill_candidates_names_arr( Mayor_name_arr,     Mayor_vote_arr,
  367.                                V_Mayor_name_arr,   V_Mayor_vote_arr,
  368.                                Secretary_name_arr, Secretary_vote_arr,
  369.                                Treasurer_name_arr, Treasurer_vote_arr );
  370.     while( !end )
  371.     {
  372.         if( user_login(user_name, user_pass, users_names_arr, users_pass_arr ) == 1 )
  373.         {
  374.             printf("\n ______________________________________________________ \n\n");
  375.             printf("\n Welcome %s to Voting System: \n", user_name );
  376.  
  377.             vote( Mayor_name_arr,     Mayor_vote_arr,
  378.                   V_Mayor_name_arr,   V_Mayor_vote_arr,
  379.                   Secretary_name_arr, Secretary_vote_arr,
  380.                   Treasurer_name_arr, Treasurer_vote_arr );
  381.  
  382.             printf("\n Next user (Y or N) :  ");
  383.  
  384.             ch = _getch();
  385.  
  386.             if ( ch=='n' || ch=='N' )
  387.                 end = 1;
  388.         }
  389.         else
  390.         {
  391.             printf("\n Voting is interrupted by a total of three failed logins ! \n");
  392.             end = 1;
  393.         }
  394.     }
  395.  
  396.     // display result of voting
  397.     display_result_of_voting( Mayor_name_arr,     Mayor_vote_arr,
  398.                               V_Mayor_name_arr,   V_Mayor_vote_arr,
  399.                               Secretary_name_arr, Secretary_vote_arr,
  400.                               Treasurer_name_arr, Treasurer_vote_arr  );
  401.  
  402.     // display all candidates names and their votes
  403.     // (put in comment if you don't want to display this)
  404.     display_candidates_and_votes( Mayor_name_arr,     Mayor_vote_arr,
  405.                                   V_Mayor_name_arr,   V_Mayor_vote_arr,
  406.                                   Secretary_name_arr, Secretary_vote_arr,
  407.                                   Treasurer_name_arr, Treasurer_vote_arr  );
  408.  
  409.     return 0;
  410.  
  411. } // main()
Advertisement
RAW Paste Data Copied
Advertisement