Fibonacci Green Crud

Here is the question/goal:

// Write a program that determines the size of the green crud population
// on any given day.
// Assumes that green crud reproduces every 5 days starting when
// it is 10 days old,giving a growth rate following the Fibonacci sequence.
// So if there is a new crud population of 10 pounds on day 0, the
// crud population at increments of 5 days beginning with day 0 are as follows:
// 10, 10, 20, 30, 50, 80, 130, ...
// That is, on day 0 there are 10 pounds; on day 5 there are still 10 pounds;
// on day 10 there are 20 pounds; on day 15 there are 30 pounds; and so on.


Here is my code:

#include <iostream>
using namespace std;

int main()
{

char ans;

do {
int oldCrud = 0; // at least 5 days old; will reproduce next cycle
int newCrud; // less than 5 days old; won't reproduce next cycle
int next;
int daysToSimulate;
int day; // current day


cout << "Enter starting amount of green crud (integer): ";
cin >> newCrud;

cout << "Enter number of days to simulate (integer): ";
cin >> daysToSimulate;




for (day = 0; daysToSimulate > day; day++)
{
if (day % 5 == 0)

next = newCrud + oldCrud;
newCrud = oldCrud;
oldCrud = next;

}


if (daysToSimulate < 5)
cout << "\nIt takes at least five days for the green crud to have any growth. Hence, it is still "
<< newCrud << " pounds.";

else
cout << "On day " << daysToSimulate << " you have " << oldCrud
<< " pounds of green crud." << endl;








cout << "\nWould you like to run the program again? (y or n): ";
cin >> ans;

} while (ans == 'Y' || ans == 'y');

return 0;

}





//END


Pardon my structure but I've only learned so much haha. The problems I'm having is that when the user inputs days 6-9 and 10 pounds. It should come out the same amount as 5 days which is 10 but it doesn't, it comes out as 20. This should go the same for every day in between until it hits that 5th day to reproduce. Also, when the user inputs 10 pounds and 15 days it should come out to 30 pounds according to the equation I have. BUT. For whatever reason it comes out as 40 pounds. I've been stuck, any help would be appreciated.
Your if is incorrect. It only contains the first line:
1
2
3
4
if (day % 5 == 0)
    next = newCrud + oldCrud;
newCrud = oldCrud;
oldCrud = next;

Also, if the loop only does something every 5 loops, why not just increment day by 5 instead of 1?
I tried switching up a few things from what you suggested but It all still doesn't work. When I changed the increment to 5, it made the days (6-9) correct. But then the equation did not output the right amount when the days hit the intervals of 5 (10, 15, 20, etc.)
Last edited on
Thanks for the help but I had someone else help me. Everything was fine in my original code except for the "for loop", it was dropping out too early so thats why my amounts were off.
Topic archived. No new replies allowed.