bool program (doubling pennies to a million)

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main()
{

bool bank(allowance)
  {
    bool result = false;

    int weeks = 0;
    double allowance = 0.01;
    double bank = 0.00;

    weeks++;
 
    bank += allowance;

    allowance = (allowance * 2);


    if (bank >= 1000000.00)
      {
      cout << "In " << weeks << " weeks, I will be a millionaire!" << endl;
      result = true;
      } 
  return result;
  }


  return 0;
}
Last edited on
What's your question?
My issue is that it doesn't produce any output. Not sure why... I can see the description part, but nothing else happens.

This is the latest go I've had at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.
see next post...
Last edited on
In this edit, I at least get it to say "In 0 weeks...", but not sure why it doesn't use the looped value for weeks?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace 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);.
Last edited on
Well on this assignment, we've been doing bool, so although I can think of a couple of ways (like do(while)), I want to figure out how using bool.

The purpose of result was just to give bool a variable. I really don't know much about bool, and searching didnt yield much help!
@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!
Topic archived. No new replies allowed.