Penny Doubling Assignment

I have to write a penny doubling assignment for a beginner C++ course. Here is what I have so far:

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
#include <iostream>

using namespace std;

int main()
{
	int numDays = 1;
	double money = 0.01;
	
	cout<<"Enter number of days worked."<<endl;
	cout<<endl;
	cin>>numDays;
	
	while (numDays <1)
	{
		cout<<"Invalid entry. Please enter a valid number (greater than 1)."<<endl;
		cout<<endl;
		cin>>numDays;
	}
	//start here

	// end here
	for (int i =1; i <= numDays; i++)
	{
		cout<<"You earned $"<<money<<"."<<endl;
		cout<<endl;
		money *=2;
		
	}
	
	return 0;
	
}


The problem is that I need only one line of output, and the YouTube video I was watching designed a table. I need the line to print the total dollar amount to two decimal places, not keep printing the amount for each day. How can I fix this?
Last edited on
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
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include <iomanip>
using std::setw;
using std::setiosflags;
using std::setprecision;
#include <cmath>

int main()
{
	int numDays  = 1;
	double money = 0.01;

	cout<<"Enter number of days worked."<<endl;
	cout<<endl;
	cin>>numDays;

	while (numDays <1)
	{
		cout<<"Invalid entry. Please enter a valid number (greater than 1)."<<endl;
		cout<<endl;
		cin>>numDays;
	}

	//start here

	// end here

	// set floating point number format
	cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);

	for (int i =1; i <= numDays; i++)
	{
		//cout << "You earned $" << money << endl;
		//cout<<endl;

		money = money + money*2;

	}
	cout << endl << "Total = " << money << endl;

	return 0;

}

Topic archived. No new replies allowed.