Penny Program

Hello, I am having trouble with this particular program. I managed to solve it with the FOR loop, but out of curiosity I tried solving it with the while loop and I seem to be having trouble on how to solve it. The first set of code I will display is the correction solution which involves the FOR loop, but the second one I am having trouble on is trying to do the program with the WHILE loop. I don't know how to finish the while loop, trying to both increment the numDays and Sum so it would look like the output from the first code I put up. Thanks a bunch guys!



Question: Write a program that calculates how much a person would earn over a period of time
if his or her salary is one penny the first day and two pennies the second day, and continues
to double each day. The program should ask the user for the number of days.
Display a table showing how much the salary was for each day.


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
  #include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int numDays = 1;
	double sum = 1.0;
	int total;
	int days = 0;

	cout << "Enter number of days\n";
	cin >> numDays;

	while (numDays < 1)
	{
		cout << "Please enter valid number of days\n";
		cin >> numDays;
	}
	
	for (int i = 1; i <= numDays; i++)
	{
		cout << "Day " << i << " you earned " << sum << "pennies\n";
		sum = pow(2, i);
	}

	
	
	return 0;
}


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
  #include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int numDays = 1;
	double sum = 1.0;
	int total;
	int days = 0;

	cout << "Enter number of days\n";
	cin >> numDays;

	while (numDays < 1)
	{
		cout << "Please enter valid number of days\n";
		cin >> numDays;
	}
	
	while (numDays > 1)
	{
		cout << "Day: " << numDays << " cost: " << sum << endl;
		numDays++; // I am trying to make numDays go by 1, but trouble what to do.
	}
	
	
	
	
	return 0;
}
It is possible to convert back-and-forth between the while and for loops in a fairly mechanical manner.

For example a while loop like this:
1
2
3
4
5
    while (numDays < 1)
    {
        cout << "Please enter valid number of days\n";
        cin >> numDays;
    }

could become an equivalent for loop:
1
2
3
4
5
    for ( ; numDays < 1; )
    {
        cout << "Please enter valid number of days\n";
        cin >> numDays;
    }

Notice the semicolons in the for statement:
for ( ; numDays < 1; ).
Here, the first and last parts are left blank. Only the middle part which is the loop condition is used.

It is possible to omit the condition too, giving an empty for statement:
for (;;). That is valid, it means repeat forever, effectively the same as while (true).

To convert a for loop to a while loop, can be done like this:
Original loop.
1
2
3
4
5
    for (int i = 1; i <= numDays; i++)
    {
        cout << "Day " << i << " you earned " << sum << "pennies\n";
        sum = pow(2, i);
    }


Reposition the first and last parts of the for statement:
1
2
3
4
5
6
7
    int i = 1;
    for (; i <= numDays; )
    {
        cout << "Day " << i << " you earned " << sum << "pennies\n";
        sum = pow(2, i);
        i++;
    }


Change for to while and omit the semicolons
1
2
3
4
5
6
7
    int i = 1;
    while (i <= numDays)
    {
        cout << "Day " << i << " you earned " << sum << "pennies\n";
        sum = pow(2, i);
        i++;
    }


A more detailed description is given in the tutorial page:
http://www.cplusplus.com/doc/tutorial/control/

I am trying to make numDays go by 1, but trouble what to do.
numDays--;
Makes sense now, thanks!
Topic archived. No new replies allowed.