-3

This was part of the cs50 assignment. Its a simple program which takes in the name of the candidate as vote and outputs the one which got called the highest time. I tried running it, but it always outputs all the candidates as winners. When I looked into it the candidates[i].votes doesn't increment even though the code is present. I just wanna know why this is occurring.

#define MAX 9
void count(string name);
typedef struct
{
   string name;
   int votes;
}
candidate;

candidate candidates[MAX];


int candidate_count;


void print_winner(void);

int main(int argc, string argv[])
{

if (argc < 2)
{
    printf("Usage: plurality [candidate ...]\n");
    return 1;
}

candidate_count = argc - 1;
if (candidate_count > MAX)
{
    printf("Maximum number of candidates is %i\n", MAX);
    return 2;
}
for (int i = 0; i < candidate_count; i++)
{
    candidates[i].name = argv[i + 1];
    candidates[i].votes = 0;
}

int voter_count = get_int("Number of voters: ");

for (int i = 0; i < voter_count; i++)
{
    string name = get_string("Vote: ");
    count(name);
}
print_winner();
}

void count(string name)
{
   for (int i = 0; i < candidate_count; i++)
   {
      if (name == candidates[i].name)
      {
          candidates[i].votes = candidates[i].votes + 1;
      }

   }
}
void print_winner(void)
{
    int z = 0;
    for (int i = 0; i < candidate_count; i++)
    {
       if (candidates[i].votes > z)
       {
           z = candidates[i].votes;
       }
    }
    for (int i = 0; i < candidate_count; i++)
    {
       if (z == candidates[i].votes)
       {
        printf("the winner is %s\n",candidates[i].name);
       }
    }

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Please understand that "C" and "C#" are entirely different languages. It would also help to narrow down the code you've posted and only include a [mre] - it shows that you've put effort into debugging your code. – gunr2171 Aug 25 '22 at 12:22
  • 1
    What is `string`? – Martin Aug 25 '22 at 12:24
  • @Martin looks like OP forgot to include the CS50 header file, but CS50 has a header file that defines a string data type. – blackbrandt Aug 25 '22 at 12:29
  • Does `cs50` encourage `printf(usage_statement); exit(1)`? That abomination is appearing often lately. A usge statement is not an error message. Either write `printf(usage statement); exit(0);` or write `fprintf(stderr, error-message); exit(1)`. Exiting non-zero without an error message is bad practice. A usage statement is not an error message. – William Pursell Aug 25 '22 at 12:30
  • 1
    In C `name == candidates[i].name` should have been `strcmp(name, candidates[i].name) == 0`. But `string` is also known in C++. In every case try using a _debugger_. – Joop Eggen Aug 25 '22 at 12:37
  • the `printWinner` doesn't make sense, that's why it's printing all of them even when only one of them is the winner. – Shark Aug 25 '22 at 12:42

1 Answers1

0

Here's what i'd do when printing the winner

void print_winner(void)
{
    int maxVotes = 0;
    int winnerIndex = -1;
    for (int i = 0; i < candidate_count; i++)
    {
       if (candidates[i].votes > maxVotes)
       {
           maxVotes = candidates[i].votes;
           winnerIndex = i;
       }
    }
//    for (int i = 0; i < candidate_count; i++)
//    {
//       if (z == candidates[i].votes)
//       {
        printf("the winner is %s\n",candidates[winnerIndex].name);
//       }
//    }

}
Shark
  • 6,513
  • 3
  • 28
  • 50
  • note: if it's printing the wrong winner then your votecounting is wrong. this one just prints the candidate with the maximum number of votes. – Shark Aug 25 '22 at 13:14