1

I'm having trouble with the last bit of code, where the years are calculated and printed. The loop seems to run once fine. If I put in something like 12 to start and want to get 13 it will give me the result of 1 year. But anymore than that and it seems to break. It won't print anything, and the program keeps running until I manually end it. I'm at my wit's end.

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

int main(void)
{
    // TODO: Prompt for start size
    int start;
    do
    {
        start = get_int("How many llamas do you have?\n");
    }
    while (start < 9);

    // TODO: Prompt for end size
    int end;
    do
    {
        end = get_int("How many llamas do you want?\n");
    }
    while (end < start);

    // TODO: Calculate number of years until we reach threshold
    int years = 0;
    int herd = start;
    while (herd < end)
    {
        herd = trunc (start + (start / 3) - (start /4));
        years++;
    }

    printf("That will take %i", years); printf(" years!\n");

}

1 Answer 1

0

It's an infinite loop here:

while (herd < end)

start is essentially a constant; it never changes after entered by the user. Therefore, the value of herd never changes after the first calculation.

1
  • Sorry for the late response, I got very busy. I figured out where I went wrong, thanks for your help!
    – MaeT
    Oct 4, 2021 at 21:51

You must log in to answer this question.

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