0

First, let me share my code:

int main(void)
{
    int x = 1, y, n = 0, year;
    do
    {
        x = get_int("Enter the starting population size: ");   // TODO: Prompt for start size
    }
    while (x<9);

    do
    {
        y = get_int("Enter the ending population size: ");   // TODO: Prompt for end size
    }
    while (y<x);

    do
    {
        year = x + (x/3) - (x/4);   // TODO: Calculate number of years until we reach threshold
        n++;
        x = year;
    }
    while (year < y);

    printf("Years: %i\n", n);    // TODO: Print number of years
}

Results for cs50/labs/2021/x/population generated by check50 v3.1.2
:) population.c exists
:) population.c compiles
:) handles starting values less than 9
:) handles ending values less than starting values
:) handles decimal number of llamas
:( handles same starting and ending sizes
    expected "Years: 0", not "Years: 1\n"
:) handles starting population of 1200
:) rejects invalid populations and then handles population 9
:) rejects invalid populations and then handles population 20
:) handles starting population of 100

Why is it failing the test :( handles same starting and ending sizes expected "Years: 0", not "Years: 1\n"?

0

It's failing the test because you are using a do-while loop which is incrementing n before it checks if year < y. Remember that the reason to use a do-while loop instead of a while loop is that you can ensure that the code block is executed at least once, regardless of the condition.

You must log in to answer this question.

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