0

I'm stuck on trying to figure out how to take a multiple candidate data from a loop and figure out a winner from them by stating their name, ID, and total percent of votes within the same function. I also need to return the winning candidates ID from the function. For example: candidate 1 - is named Stevens with ID 13.07 and total votes of 1500, candidate 2 - is named George with ID 17.49 and total votes of 2000. I need to output the winner which is George with ID 17.49 and his percent of the total 3500 votes. This is what I have so far but I'm new to programming and have no idea how to figure this out. Any help would be greatly appreciated!

float CandidateData(int N, int X) {

    char candName[21];
    int i;
    double candID;
    int candVotes;
    int voteSum;
    int j = 0;
    double max = 0;
    double first;
    

    for (i = 0; i < N; i++) {    //N is the number of candidates 
        printf("Enter candidates name (20 characters max): ");
        scanf("%s", &candName);
        printf("Candidate name: %s\n\n", candName);

        printf("Enter candidate ID (float 01.01-52.99): ");
        candID = CandidateID(52.99);
        printf("Candidate ID is: %g\n\n", candID);

        printf("Enter %d precinct votes (int 1-1000): ", X);

        voteSum = 0;

        for (j = 0; j < X; j++) {   //X is number of precincts 

            candVotes = NumberOfCandidatesAndPrecincts(1000);
            printf("Precinct #%d = %d\n", j + 1, candVotes);
            voteSum = voteSum + candVotes;

        }
        printf("Total candidate votes = %d\n", voteSum);
        printf("Average votes for county = %d\n\n", voteSum / X);

    if (voteSum > max) {
        max = voteSum;
    }
    }
    
    printf("The winning candidate is %s with ID %g with %d% of total votes", candName, candID, )

    return (candID);
}
  • 1
    You should [edit] and post a [mcve] including some simple examples of input and expected vs actual output. – Jabberwocky Nov 04 '20 at 13:37
  • when writing code, parameter names like `N` and `X` are meaningless, even in the current context. Please use meaningful variable (and parameter) names. – user3629249 Nov 04 '20 at 23:11
  • OT: regarding statements like: `scanf("%s", &candName);` The 'input format conversion specifier' %s allows the user to input ANY number of characters for the candidate name. Even though the code informs the user that the name needs to be 20 (or less) characters, this puts no constraint on the user. Also, a bare array name degrades to the address of the first byte of the array. `&candName` has a totally different meaning from what is wanted. Also, should always check the returned value to assure the operation was successful. Suggest: `if( scanf("%20s", candName) != 1 ) { handle error }` – user3629249 Nov 04 '20 at 23:15
  • regarding; `printf("Enter candidate ID (float 01.01-52.99): "); candID = CandidateID(52.99);` and `double candID;` a 'float'` is not a 'double'. Chose one or the other. – user3629249 Nov 04 '20 at 23:23
  • the posted code can only display a candidate name from the last candidate entered. There needs to be a field to save the name of the candidate with the highest number of votes – user3629249 Nov 04 '20 at 23:28
  • the posted code calls to functions: `NumberOfCandidatesAndPrecincts()` and `CandidateID()` but the contents of those functions are not presented, so how are we to know 'exactly' what they are doing? – user3629249 Nov 04 '20 at 23:30
  • OT: the posted code does not handle ties between candidates – user3629249 Nov 04 '20 at 23:34
  • Note: `%s` stops at any `white space`, so cannot enter first and last name of a candidate. What if more than one candidate was named `jeff` If 'jeff' won, how are we to know which 'jeff' won – user3629249 Nov 04 '20 at 23:37

1 Answers1

1

I see few mistakes in your code.

First of all, it should be :

scanf("%s", candName);

not

scanf("%s", &candName);

Because candName is already the address of the first element of the array.

Another issue, suppose a user types Whoever President for candName. Then try this:

printf("Candidate name: %s\n\n", candName);

You will see that only Whoever is printed because scanf("%s",candName) takes input until a white space comes. It can be a blank space (' '), new line (\n) or tab (\t).

Instead, I strongly suggest you to use fgets.

fgets (candName, 21, stdin);

where 21 is the maximum buffer length that you want to have.

Now, you can print candName with printf and observe that fgets puts extra new line ('\n') at the end of your string. You can easily remove it by:

if ((strlen(candName) > 0) && (candName[strlen (candName) - 1] == '\n')){
    candName[strlen (candName) - 1] = '\0';}

Keep in mind that using fgets and scanf together is problematic because scanf function leaves \n in the input buffer, after fgets reads input from buffer, you will encounter unexpected results. Use getchar(); after each scanf. It consumes \n. The problem disappears!

The next mistake you made is return_type of CandidateData function. Its return_type is float but type of candID variable is double so make a change for just one of them. In your task, it is sufficient to use float type for candID.

There are also some typos in the last printf function. It should be:

printf("The winning candidate is %s with ID %f with %d of total votes", candName, candID, voteSum);

Let's come to your question. To store the candidate who takes maximum vote, you can declare a struct and fill related fields when number of votes of a candidate exceeds current max value.

I add the final code:

#include <stdio.h>
#include <string.h>


struct President{
    char candName[21];
    float candID;
    int candVotes;
};

float CandidateData(int N, int X) {

    struct President winnerCandidate = {.candVotes = 0};

    char candName[21];
    float candID;
    int candVotes;
    int voteSum;
    int i,j;

    for (i = 0; i < N; i++) {    //N is the number of candidates
        printf("Enter candidates name (20 characters max): ");
        fgets (candName, 21, stdin);
        if ((strlen(candName) > 0) && (candName[strlen (candName) - 1] == '\n')){
            candName[strlen (candName) - 1] = '\0';}
        printf("Candidate name: %s\n", candName);

        printf("Enter candidate ID (float 01.01-52.99): ");
        scanf("%f",&candID);
        getchar();
        printf("Candidate ID is: %.2f\n", candID);

        voteSum = 0;
        printf("Enter %d precinct votes (int 1-1000):\n", X);
        for (j = 1; j <= X; j++) {   //X is number of precincts
            scanf("%d",&candVotes);
            getchar();
            printf("Precinct #%d = %d\n", j , candVotes);
            voteSum = voteSum + candVotes;

        }
        printf("Total candidate votes = %d\n", voteSum);
        printf("Average votes for county = %.3f\n\n", voteSum/(float)X);

        if(voteSum > winnerCandidate.candVotes){
            winnerCandidate.candVotes = voteSum;
            winnerCandidate.candID = candID;
            strcpy(winnerCandidate.candName,candName);
        }

    }
    printf("The winning candidate is %s with ID %.2f with %d of total votes", winnerCandidate.candName, winnerCandidate.candID, winnerCandidate.candVotes);
    return winnerCandidate.candID;
}


int main(){
    float winner_candID = CandidateData(3,2);
    printf("\nWinner Candidate ID is: %.3f",winner_candID);
    return 0;
}

If you have any question, let me know!

black
  • 799
  • 1
  • 9
  • 23