Strange math error in otherwise simple program

I'm pulling my hair out over this one. This program is simply supposed to take in two user supplied numbers and use them as the basis of a Fibonacci growth sequence. Basically, the user supplies the starting amount of green crud and the number of days it will be growing. Every fifth day the crud divides and produces more crud. The user then has the option to repeat the process with new values. The problem is that when I build & run the program if the user supplies a value less than 10 for the number of days the output will always be 4273312, no matter what value is supplied for the amount of crud. Stranger still, if the user decides to repeat the process with new numbers, the output will still be 4273312 no matter what values are supplied. This only happens if a number less than 10 is supplied for the days and only if that number is supplied when the program is first run. If any number greater than 10 is supplied then all operations output the expected values, even if a number less than 10 is supplied. Any ideas?

//////////////////////////////////////////////////////////////////////////

#include<iostream>

using namespace std;

int main()
{
int a, b, c, days, divs; //Declare my variables
char yes_no = 'y';


//A banner to explain the product to the user

cout << "***************************************************************\n";
cout << "* CONGRATULATIONS! *\n";
cout << "* You are the proud owner of a brand new, live, fully active *\n";
cout << "* supply of *VaulTec Deadly Green Crud!* Please consult this *\n";
cout << "* built-in user guide before administering this product to *\n";
cout << "* ensure effectiveness. Thank you for choosing VaulTec! *\n";
cout << "***************************************************************\n";

//The loop that allows users to repeat the process
while ((yes_no == 'y') || (yes_no == 'Y'))

{
//ask the user for an initial amount of crud and store the value in the a //variable
cout << "How many pounds of Deadly Green Crud will you be unleashing on your foes?: ";
cin >> a;

b = 0;
//ask the user for the amount of days the crud will grow and store the value in //the days variable
cout << "For how many days do you intend allow the Deadly Green Crud to grow?: ";
cin >> days;

// Divide the number of days by five. The green crud only divided every 5 days
divs = days / 5;


// This loop is how we create our Fibonacci sequence.
for (int i = 1; i < divs; i++)
{
c = a + b;
b = a;
a = c;

}

// inform the userr of the results
cout << "Your Deadly Green Crud will have grown to " << c << " pounds after " << days << " days.\n";

//Prompt user to run again
cout << "Would you like to calculate another deployment of Deadly Green Crud? (Y/N):";
cin >> yes_no;
}


return 0;

}
Hi ! Code tags pls ;)

Always initialize variables. This'll help isolate problems.
It looks like the issue here is divs = days / 5;

divs and days are integers so u always have to be very careful when doing divisions. divs will have to be rounded to be able to loop it...
c is not initialised. When divs <= 1 the Fibonacci loop is not entered and c is never assigned any value. So basically the result displayed is random garbage. (no offence, that's just a technical term).
Last edited on
Sorry about the lack of code tags. I'm still trying to get used to this forum. Regardless, initializing 'c' outside of the loop appears to have completely fixed my issue. Thanks guys! I'm going to keep that in mind for future projects. Awesome, thank you!
Topic archived. No new replies allowed.