Help with error and finishing assigment

This is my assignment, the goal is to look like this but with 30 days:

Starting salary: 0.05
Number of days to work: 5

Day 1: 0.05
Day 2: 0.10
Day 3: 0.20
Day 4: 0.40
Day 5: 0.80

You made $2.05 and earned 1 bonus of $0.50

For this assignment, write a program that will calculate how much a person would earn over a finite (random) period of time if his/her salary is a random amount for the first day and continues to double each day.

So if a person's starting salary is $0.04, they would earn that $0.04 for the first day of work, $0.08 for the second day of work, $0.16 for the third day of work, etc.... Over three days, the person would earn $0.28.

If the example is carried on for a few more days, the person would earn $0.32 for the fourth day, a total of $1.04 ($0.64 in salary and a $0.40 bonus) for the fifth day, and $1.28 for the sixth day, resulting in a total of $2.92 for 6 days of work.



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
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()


{
	
srand(1);

srand(time(0));


	float salary = 0, bonus = 0, totalpay = 0;
	int days = 0;
	
salary = rand() % 6 + 1;
salary = salary / 100;
days = rand() % 31;
totalpay = salary;

	cout << "Starting salary: " << salary << endl
		 << "Days worked: " << days << endl
		 << "************************************" << endl
		 << setw(30) << "Daily Salary" << setw(30) << "Amount Earned" << setw(30) << endl;
		 
	for (int d = 0; d < days; d++)
	{
		
				
		if ((d+1) % 5 == 0)
			bonus = salary * 10;
		else
			bonus = 0.00;
			
		cout << d + 1 << '\t'
		<< fixed << setprecision(2) << setw(18) << salary *=2 << '\t'
		<< (totalpay+bonus) << setw(18)  << endl;
		
		totalpay += salary;

			
	
	}
		 
		 

return 0;		 
}



Made a new topic for this and decided to rewrite the code into something that is more organized. I am getting the error now

invalid operands of types 'int' and 'float' to binary 'operator<<'
Here's the snippet I modified

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
cout << d + 1 << '\t'
		<< fixed << setprecision(2) << setw(18) << salary *2 << '\t'//<-- here's the change
		<< (totalpay+bonus) << setw(18)  << endl;
		salary*=2;
Starting salary: 0.06
Days worked: 30
************************************
                  Daily Salary                 Amount Earned
                             1	              0.12	0.06
                 2	              0.24	0.18
                 3	              0.48	0.42
                 4	              0.96	0.90
                 5	              1.92	11.46
                 6	              3.84	3.78
                 7	              7.68	7.62
                 8	             15.36	15.30
                 9	             30.72	30.66
                10	             61.44	368.58
                11	            122.88	122.82
                12	            245.76	245.70
                13	            491.52	491.46
                14	            983.04	982.98
                15	           1966.08	11796.42
                16	           3932.16	3932.10
                17	           7864.32	7864.26
                18	          15728.64	15728.58
                19	          31457.28	31457.22
                20	          62914.56	377487.28
                21	         125829.12	125829.06
                22	         251658.23	251658.19
                23	         503316.47	503316.44
                24	        1006632.94	1006632.88
                25	        2013265.88	12079595.00
                26	        4026531.75	4026531.50
                27	        8053063.50	8053063.00
                28	       16106127.00	16106126.00
                29	       32212254.00	32212252.00
                30	       64424508.00	386547040.00
 
Exit code: 0 (normal program termination)


apparently you cant use <<salary*=2; although you can use salary++

but why?
apparently you cant use <<salary*=2; although you can use salary++

but why?

Because the operator<< has a higher precedence than operator*= the output operation happens before the multiplication.

This is one of the reasons doing math in a stream output statement should be discouraged. You can do the math if you surround the expression in parentheses. However IMO you really should consider output statements to be using constant variables, do the modifications before or after the output statements.



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
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()


{
	
srand(1);

srand(time(0));


	float salary = 0, bonus = 0, totalpay = 0;
	int days = 0, salary2;
	
salary = rand() % 6 + 1;
salary = salary / 100;
days = rand() % 31;
totalpay = salary;

	cout << "Starting salary: " << salary << endl
		 << "Days worked: " << days << endl
		 << "************************************" << endl
		 << setw(30) << "Daily Salary" << setw(30) << "Amount Earned" << setw(30) << endl;
		 
	for (int d = 0; d < days; d++)
	{
	
		cout << d + 1 << '\t'
		<< fixed << setprecision(2) << setw(18) << salary << '\t'
		<< setw(18)<< (totalpay+bonus) << endl;
		
		salary *= 2;
		
		if ((d+1) % 5 == 0)
			bonus = salary * 10;
		else
			bonus = 0.00;
			
		totalpay += salary;

			
	
	}
		 
		 

return 0;		 
}


Well I improved the code but now im having formatting issues

Also how do I get it so the bonus is only added every 5th day to the amount earned?
Line 40 should be doing that, however, you never do anything with the bonus amount.
You probably want to add the bonus amount to totalpay.

How would I just add bonus amount earned AFTER day 4 so day 5 and onwards?
Also having some slight formatting issues
Topic archived. No new replies allowed.