So I need to write a program that shows how many weeks it will take to double a weekly one penny allowance until the banked amount equals a million dollars. So, first week = 0.01, second week = 0.01+0.02, third week = 0.03+0.04, and so on. I've tried a few things, but here is my latest attempt:
int main()
{
cout << "Description: This program calculates the number of weeks required" << endl;
cout << " to turn a weekly allowance that starts at one cent and doubles" <<endl;
cout << " weekly, into a million dollars!" << endl;
cout << endl;
bool result = false;
while(false)
{
int weeks = 0;
double allowance = 0.01;
double bank = 0.00;
weeks++;
bank += allowance;
allowance = (allowance * 2);
if (bank >= 1000000.00)
{
result = true;
cout << "In " << weeks << " weeks, I will be a millionaire!" << endl;
}
}
return 0;
}
I also tried changing while(false) to while(true) like some of the if/while examples we've had, but we just werent given many bool examples to work with, just one value returning function, which is why I had that mess I started with!
int main()
{
cout << "Description: This program calculates the number of weeks required" << endl;
cout << " to turn a weekly allowance that starts at one cent and doubles" <<endl;
cout << " weekly, into a million dollars!" << endl;
cout << endl;
int weeks = 0;
double allowance = 0.01;
double bank = 0.00;
bool result = false;
while(false)
{
weeks++;
bank += allowance;
allowance = (allowance * 2);
if (bank >= 1000000.00)
{
result = true;
cout << "In " << weeks << " weeks, I will be a millionaire!" << endl;
}
}
return 0;
}
You were resetting weeks, allowance and bank every iteration of the loop.
#include <iostream>
usingnamespace std;
int main()
{
cout << "Description: This program calculates the number of weeks required" << endl;
cout << " to turn a weekly allowance that starts at one cent and doubles" <<endl;
cout << " weekly, into a million dollars!" << endl;
cout << endl;
bool result = false;
int weeks = 0;
double allowance = 0.01;
double bank = 0.00;
while(false)
{
weeks++;
bank += allowance;
allowance = (allowance * 2);
if (bank >= 1000000.00)
{
result = true;
}
}
cout << "In " << weeks << " weeks, I will be a millionaire!" << endl;
return 0;
}
I can tell you that you don't want to use while(false). That is tantamount to says if(false); it makes that code never run. Why not just loop while bank < 1 million?
What is the purpose of 'result'? Use while(true); and a break; on line 29. Or you could keep it as it is and use while(result==false);. Otherwise, I can't see the point in using 'result'. You could also get rid of both 'result' and the if statement within the loop, and just use while(bank<1000000.00);.
@CJC - Thanks, that worked! Any good reference material anyone can link me to that shows examples of things like this? Most of what I've found are just reference tables, without in depth explanations or examples. Our lecture notes are not always great, and this is an online class. We have a message board, but sometimes responses take awhile, and while I understand the point, the answers are typically quite vague!