• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #924
    admin
    Keymaster

      While the program is running and giving valid ouput for at least some inputs, with Check50, it shows for 🙁 plurality compiles

      Cause
      code failed to compile

      This is the code:

      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      #define MAX 9
      
      typedef struct
      {
      int vote;
      string name;
      }
      candidate;
      candidate candidates[MAX];
      
      int maxcandidate;
      int noofcandidate;
      
      
      bool vote (string nameentered);
      void print_winner(void);
      
      int main (int argc, string argv[])
      {
          if(argc < 4)
              {
              printf("less than 4 and so invalid");
              return 0;
              }
          if(argc > 9)
              {
              printf("more than 9 and so invalid");
              return 0;
              }
      noofcandidate = argc - 1;
      
          for (int c = 0; c < noofcandidate; c++)
              {
              candidates[c].name = argv[c+1];
              candidates[c].vote = 0;
              }
      int noofvoters = get_int("Number of voters: ");
      printf("Number of voters: %i\n", noofvoters);
          for (int t = 0; t < noofvoters; t++)
              {
              string nameentered = get_string("Vote: ");
              if (!vote(nameentered))
                  {
                  printf("invalid name");
                  return 1;
                  }
              }
      print_winner();
      }
      
      
      
      bool vote (string nameentered)
      {
      int z;
          for (z = 0; z < noofcandidate; z++)
              {
              if (strcmp(nameentered, candidates[z].name) == 0)
                  {
                  candidates[z].vote = candidates[z].vote + 1;
                  return true;
                  }
              }
      return false;
      }
      
      
      void print_winner(void)
      {
      int maxvote = 0;
          for (int z = 0; z < noofcandidate; z++)
              {
              if (candidates[z].vote > maxvote)
                  {
                  maxvote = candidates[z].vote;
                  }
              }
      
          for (int z = 0; z < noofcandidate; z++)
              {
                  if (maxvote == candidates[z].vote)
                      {
                      printf ("%s\n", candidates[z].name);
                   }
              }
          return;
      }

       


      Reply


      https://edstem.org/us/courses/176/discussion/2117204?comment=4860019


       [learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top