Uninitialized Variables
The main problem here is that the variable i is not initialized. However, you use this to create arrays count, name, party. This leads to unexpected results.
Bugs in the code
Assignment vs comparison
if (i = vote)
This is a value assignment, you probably wanted to compare their values instead:
Calculating the leading candidate
This doesn't work as intended either. A correct implementation would be something like this:
int leading = 0;
for (int i = 1; i < n; i++)
{
if (count[leading] < count[i])
leading = i;
}
printf ("The leading candidite is %s from party %s with %d votes\n", name[leading], party[leading], count[leading]);
Further Notes:
Scanf Has Its Limitations
Using scanf does not allow you to enter strings with space in it. E.g. candidate name cannot be "John Smith" as scanf will stop at John and the next time you would call it in the code, it will keep reading the stdin starting from "Smith".
Also, if you're scanning for a number but the user enters a string instead, the next time scanf is called, it will have the input still unprocessed, and will try to process the string. This means, that if you are trying to read numbers in a loop, once entering a string will make the code run in an infinite loop.
Also, it is prone to run out of buffer, if the user enters more character than the buffer you try to store your data in.
Use Talkative Identifiers
It's worthwhile to use talkative variable names. It makes easier to understand the code. For instance, instead of n you could use numberOfCandidates or nCandidates.
Group Data Together
You could create a structure that would hold all the data for your candidate: name, party and number of votes:
typedef struct
{
char name[25];
char party[25];
unsigned int voteCount;
} Candidate;
Divide Your Code Into Multiple Functions
It is a good practice not to write lengthy functions, instead separate codes that belong together into well defined functions. Such a function could be one that prints something on the screen asking for an input from the user then storing it into a variable.
Also, the 3 main functionality could potentially be separated into 3 functions (vote, see votes and find the leading candidate).
An Enhanced Solution
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[25];
char party[25];
unsigned int voteCount;
} Candidate;
static bool scanUint(const char * message, unsigned int * value)
{
static char buffer[255];
printf("%s", message);
fgets(buffer, sizeof(buffer), stdin);
return (sscanf(buffer, "%ui", value) == 1);
}
static void scanString(const char * message, char * value)
{
printf("%s", message);
scanf("%s", value);
}
static void castVote(Candidate * candidates, unsigned int nCandidates)
{
printf ("The Candidates for the election are:\n");
for (unsigned int i = 0; i < nCandidates; i++)
printf ("\t%d. %s [%s]\n", i + 1, candidates[i].name, candidates[i].party);
int candidateId;
scanUint("Please enter the ID of the candidate you wish to cast vote on: ", &candidateId);
if (0 < candidateId && candidateId <= nCandidates)
{
candidates[candidateId - 1].voteCount++;
}
else
{
printf("Invalid candidate ID: %d\n", candidateId);
}
}
static void listCandidates(const Candidate * candidates, unsigned int nCandidates)
{
printf("%3s%26s%26s%11s\n", "ID", "Name", "Party", "Vote Count");
static const unsigned int totalLength = 3 + 26 + 26 + 11;
for (unsigned int i = 0; i < totalLength; i++)
printf("-");
printf("\n");
for (unsigned int i = 0; i < nCandidates; i++)
printf("%3d%26s%26s%11d\n", i + 1, candidates[i].name, candidates[i].party, candidates[i].voteCount);
}
static void findLeadingCandidate(const Candidate * candidates, unsigned int nCandidates)
{
unsigned int leadingId = 0;
for (unsigned int i = 1; i < nCandidates; i++)
{
if (candidates[leadingId].voteCount < candidates[i].voteCount)
leadingId = i;
}
printf ("The leading candidate is %s from party %s with %d votes\n", candidates[leadingId].name, candidates[leadingId].party, candidates[leadingId].voteCount);
}
int main()
{
//
// Reading the input from the console
//
unsigned int nCandidates;
printf("###### WELCOME TO ELECTION / VOTING ######\n\n");
while (!scanUint("Enter the number of candidates: ", &nCandidates)) ; // Keep reading until the user enters a valid number
printf("Entered: %i", nCandidates);
// Now, `n` is initialized, we can use it to create the arrays.
Candidate candidates[nCandidates];
for (unsigned int i = 0; i < nCandidates; i++)
{
printf("Candidate #%d:\n", i + 1);
scanString("\tName: ", candidates[i].name);
scanString("\tParty: ", candidates[i].party);
candidates[i].voteCount = 0;
}
//
// Main Menu
//
bool keepRunning = true;
while (keepRunning)
{
printf("###### MAIN MENU ######\n\n");
printf("\t1. Cast a vote on a candidate\n");
printf("\t2. List candidate details\n");
printf("\t3. Find the leading candidate\n");
printf("\t4. Exit\n\n");
unsigned int choice;
scanUint("? ", &choice);
switch (choice)
{
case 1: castVote(candidates, nCandidates); break;
case 2: listCandidates(candidates, nCandidates); break;
case 3: findLeadingCandidate(candidates, nCandidates); break;
case 4: keepRunning = false; break;
default: printf("Unknown option: %d\n", choice);
}
}
printf("Good bye!\n");
return 0;
}