0

I was trying to compile my code for plurality but it says:

plurality.c:82:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
<builtin>: recipe for target 'plurality' failed
make: *** [plurality] Error 1. 

So then I used help50 and it told me: Ensure that your function will always return a value. If your function is not meant to return a value, try changing its return type to void. I'm not sure how to do this so any help would be nice. A copy of my code is below. Thank you.

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

//max number of candidates
#define MAX 9

//candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

//array of candidates
candidate candidates [MAX];

//number of candidates
int candidate_count;

//function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    //check for invalid usage
    if(argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }
    
    // Populate array of candidates
    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: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

//update vote totals given a new vote
bool vote(string name)
{
    bool got = false;
    for(int y = 0; y < candidate_count; y++)
    {
        if(strcmp(name, candidates[y].name) == 0)
        {
           candidates[y].votes += 1;

            got = true;

            break; 
        }
        return got;
    }
}

void print_winner(void)
{
    int most = 0;
    string winner;
    
    for(int j = 0; j < candidate_count; j++)
    {
        if(candidates[j].votes >= most)
        {
            most = candidates[j].votes;

            for(int x = 0; x < candidate_count; x++)
            {
                if(candidates[j].name >= winner)
                {
                    winner = candidates[j].name;
                }
            }
        }
    }
    printf("Winner is %s with %i votes\n", winner, most);

    return;
}
0

It's exactly what it says. The code can theoretically get to the end of the vote function without returning a value. Let's look at the code:

bool vote(string name)
{
    bool got = false;
    for(int y = 0; y < candidate_count; y++)
    {
        if(strcmp(name, candidates[y].name) == 0)
        {
            candidates[y].votes += 1;
            got = true;
            break; 
        }
        return got;
    }
}

Here's how the compiler looks at it. The code could potentially not execute the for loop because y is greater than or equal to candidate_count (for instance, if candidate_count is 0.) If this happens, the for loop is skipped and then there's no return statement to be found after that. OR, if it goes into the loop and executes the break on the first pass through the loop, it drops out of the loop and still fails to hit a return statement.

There needs to be a return statement following the for loop code block to ensure that the code MUST execute a return statement and return a value. You need to decide what's appropriate in this case.

Having said all that, there's still a problem. The for loop will only execute once. In the loop, it will hit a return statement or a break statement before executing the for loop a second time.

Seems to me that both problems have the same simple fix. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Using what you said I changed the placement of return got; but I'm still confused on the second thing you said. You said that they both have the same simple fix, so would that mean I need another return statement? Sorry for asking I'm new to this and am really confused. – Aarti Jun 30 '20 at 6:05
  • Nope. Just moving the last return to the correct location would fix both problems at the same time. You figured it out. ;-) Here's a tip. If you ever wonder if something is right, or wrong, or just trying to figure out how something works, just do it! It's time well spent and you'll learn things whether it works or not! ;-) – Cliff B Jun 30 '20 at 6:17
  • Oh well it works when I compile it now, but when I try to run the program using ./plurality it says Usage: plurality [candidate ...]. Any advice on how to fix that? – Aarti Jun 30 '20 at 6:20
  • That would be a new problem that warrants a new question with the current code. – Cliff B Jun 30 '20 at 6:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .