0

In the prime exercise of practice problems 1 I am encountering the following error for the code below: non-void function does not return a value in all control paths [-Werror, Wreturn-type]

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

bool prime(int number);

int main(void)
{
    int min;
    do
    {
        min = get_int("Minimum: ");
    }
    while (min < 1);

    int max;
    do
    {
        max = get_int("Maximum: ");
    }
    while (min >= max);
    for (int i = min; i <= max; i++)
    {
        if (prime(i))
        {
            printf("%i\n", i);
        }
    }
}

bool prime(int number)
{
    for (int j = 1; j < number; j++)
    {
        if (number % j != 0)
        {
            return false;
        }
    }
}

1 Answer 1

0

Exactly what it says, your prime function never returns true or more importantly it never returns anything if number % j == 0 and the loop containing that check is exhausted.

If you mark a function as returning a type you should make sure that all pathways through the function end up returning a value eventually.

You must log in to answer this question.

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