-1

Who can tell me what return 2 is exactly doing in this piece of code and why is it there? What about return 3 and return 4?

The code you see is it not all, and basically is an exercise for the course cs50 where they propose a runoff election method.

Thank you!!

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

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: runoff [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX_CANDIDATES)
    {
        printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
        return 2;
    }

    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
        candidates[i].eliminated = false;
    }

    voter_count = get_int("Number of voters: ");
    if (voter_count > MAX_VOTERS)
    {
        printf("Maximum number of voters is %i\n", MAX_VOTERS);
        return 3;
    }

    // Keep querying for votes
    for (int i = 0; i < voter_count; i++)
    {

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            // Record vote, unless it's invalid
            if (!vote(i, j, name))
            {
                printf("Invalid vote.\n");
                return 4;
            }
        }
        printf("\n");
    }
anastaciu
  • 23,467
  • 7
  • 28
  • 53
program101
  • 27
  • 4
  • Not entirely clear what your question is but hopefully the above post answers it. If not, search for "main return value" and read the many results that come up. And if that still doesn't help then please clarify what you are asking. – kaylum Apr 19 '20 at 20:46
  • not really because the code continues with return 3 and return 4.... – program101 Apr 19 '20 at 21:56
  • So you are asking why `2` in particular? The programmer just made that up so that "2" means "maximum number of candidates exceeded". It is not set in stone. It could be 3, or 4 or 100. As long as whoever uses the value knows what it means. And they know that by documentation - if you use my code I have told you that "2" means this. – kaylum Apr 19 '20 at 21:58
  • I posted the rest of the code with return 3 and return 4. Not really clear (for me) what they do. – program101 Apr 19 '20 at 22:00
  • 1
    Look at it this way. Why do you stop at the traffic light when it turns "red"? "red" doesn't have to mean "stop". It cold be "go". But we have all agreed that red means stop. Same with 2, 3, 4 in that program. The programmer and the user of the code agree to understand what each of those values mean - in this case each communicates a specific error condition. – kaylum Apr 19 '20 at 22:03

1 Answers1

3

It's the main() function return value as you can see main() returns int so any value in int range can be used and is valid, including, of course, 2, 3 or 4. The conventional return values are usually 1 for failure and 0 for success, but it's better to use the macros EXIT_FAILURE and EXIT_SUCCESS from <stdlib.h> standard library for more portable code.

You can look at it as being the program exit codes, it's not clear with the code you show, if the system does anything with those.

In the context of the program they represent different types of reasons for the program to exit, for example, 1 is when the number of command line arguments is different from what is required, 2 is the exit code for when candidate_count is larger than MAX_CANDIDATES, 3 will be the exit value for when voter_count is larger than MAX_VALUES, and so on.

If these where return values from another function to main() they could be used to take different actions deppending on the return value, for example, if the return value is 1 some function is called, if it's 2 the program exits.

anastaciu
  • 23,467
  • 7
  • 28
  • 53