Advertisement
userxbw

(votes) Get info into struct array C

Sep 11th, 2022 (edited)
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <errno.h>
  5. #define MAX_A 3
  6. #define MAX_LEN 202
  7. #define NAME_LEN 100
  8. struct vote_4_me{
  9. char fname[NAME_LEN];
  10. char lname[NAME_LEN];
  11. int vote;
  12. };
  13. void start_mess(void);
  14. void get_votes(void);
  15. int chop_string(struct vote_4_me *st, char *s, int c);
  16. void stars(int am);
  17. int main() {
  18. char run='y',yes='y';
  19. struct vote_4_me vfm[MAX_A];
  20. char buff[MAX_LEN];
  21. int vts,count=0;
  22. // strol
  23. int val,base;
  24. char *string, *endptr;
  25. while(run=='y'){
  26.       start_mess();
  27.       fgets(buff,MAX_LEN,stdin);
  28.       printf("\n%s\n",buff);
  29.       if(chop_string(vfm,buff,count))
  30.       continue;
  31.       while(yes=='y'){
  32.       get_votes();
  33.       fgets(buff,MAX_LEN,stdin);
  34.       base=atoi(buff);
  35.       printf("You entered -> %d\n",base);
  36.             if(!base){
  37.                   printf("\nERROR -> %d\n\n",base);
  38.                   continue;
  39.             }
  40.             else
  41.             {
  42.                   yes='n';
  43.             }
  44.       } // End while
  45.       yes='y';
  46.       vfm[count].vote=base;
  47.       count++;
  48. if(count == MAX_A) run='n';
  49. } // end while
  50. stars(45);
  51. for(int i=0;i<count;i++){
  52.       printf("%s %s\n Has %d votes\n",
  53.       vfm[i].fname,vfm[i].lname,vfm[i].vote);
  54. }
  55. stars(45);
  56. return 0;
  57. }
  58. void start_mess(void)
  59. {
  60.       printf("HELLO\n"
  61.             "Enter persons first last name\n");
  62. }
  63. void get_votes(void)
  64. {
  65.       printf("Enter amount of votes\n");
  66. }
  67. int chop_string(struct vote_4_me *st, char *s,int c)
  68. {
  69.    char *tok;
  70.    char *tmp;
  71.    int i;
  72.       tok=strtok_r(s," ",&tmp);
  73.       for(i=0;i<1;i++){
  74.             strcpy(st[c].fname,tok);
  75.             tok=strtok_r(NULL," ",&tmp);
  76.             if(tok == NULL){
  77.                   printf("\n\nINPUT ERROR\n\n");
  78.                   return 1;
  79.            }else{
  80.               strcpy(st[c].lname,tok);
  81.               }
  82.        }
  83. return 0;
  84. }
  85. void stars(int am)
  86. {
  87.       printf("\n");
  88.       for(int i=0;i<am;i++)
  89.             printf("*");
  90.       printf("\n");
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement