Alignment in columns using justification?

I know this has most likely been asked a trillion times, but I have had no luck searching topics/forums. I am preparing a code in a for loop and am having difficulties aligning the output using justification. Purpose of program is to calculate a random starting salary between 1 cent and 5 cents doubling each day over a period of x days (random) up to 30 days.

Current Code:
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MAX_DAYS_WORKED = 30;

const int MAX_STARTING_SALARY = 5;

int main()
{

srand(10);


double salary1, salarySum;

int daysWorked1;

salary1 = rand() % MAX_STARTING_SALARY + 1;

daysWorked1 = rand() % MAX_DAYS_WORKED + 1;

cout << "***** JOB 1 *****" <<endl <<endl;
cout << "Starting Salary: $" <<fixed << setprecision(2) << salary1 * 0.01 << endl;

cout << "Number of days to work: " << daysWorked1 << endl <<endl;

for ( daysWorked1 = 1; daysWorked1 <= 10; daysWorked1++, salary1 *= 2)
{
cout << "Day " << internal << setw(2) << daysWorked1 << ": ";
cout << right << setw(12) << "$" << salary1 * 0.01 << fixed << setprecision(2) << endl;

salarySum += salary1;
}
cout << "\nFor job 1, you earned $" << fixed << setprecision (2) << salarySum * 0.01 << " in " << daysWorked1 - 1 << " day(s)." << endl << endl;

return 0;
}


Current output:

***** JOB 1 *****

Starting Salary: $0.02
Number of days to work: 10

Day 1: $0.02
Day 2: $0.04
Day 3: $0.08
Day 4: $0.16
Day 5: $0.32
Day 6: $0.64
Day 7: $1.28
Day 8: $2.56
Day 9: $5.12
Day 10: $10.24


(spaces between the two columns are not showing up, there should be 12 spaces between Day #: (left) and $x.xx (right) columns)

The end goal is to have the decimals in the right hand column lined up. My guess is the arrangement of justification? I have experimented moving different things around and trying internal, left, and right with no luck. Any help would be appreciated. And yes, I do understand this is quite elementary, apologies.

Thanks
Last edited on
When I run the program this is the ouput:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
***** JOB 1 *****

Starting Salary: $0.01
Number of days to work: 29

Day  1:            $0.01
Day  2:            $0.02
Day  3:            $0.04
Day  4:            $0.08
Day  5:            $0.16
Day  6:            $0.32
Day  7:            $0.64
Day  8:            $1.28
Day  9:            $2.56
Day 10:            $5.12

For job 1, you earned $10.23 in 10 day(s).


Do you realize that setw() doesn't add spaces if the value already has equal to or more characters than the value specified specified?

In future please use code tags when posting code.




I am not sure how you are not getting a starting salary of $0.02. I just copy and pasted exactly what I posted initially and got the same output I posted initially.

const int MAX_DAYS_WORKED = 30;
const int MAX_STARTING_SALARY = 5;

in conjunction with

salary1 = rand() % MAX_STARTING_SALARY + 1;
daysWorked1 = rand() % MAX_DAYS_WORKED + 1;

yields a starting salary of $0.02 and max days worked can be between 1-30 (as opposed to 1-29 if you did not add 1). If +1 was not added to salary1, you would get a starting salary of $0.01, not $0.02. Correct?

Also, apologies, this was my first post. What exactly are code tags?

Thanks!
I am not sure how you are not getting a starting salary of $0.02.

Remember you have that call to rand() in that calculation which may be different on different machines. You will have one of several values (1 .. 5) depending on the seed. The seed you used produces a value of 1215069295 on my machine which after the modulus call (%) will yield 0 since it is evenly divisible by 5.

They're markup tags that help to retain the format of your code. You press the <> to the right of the edit window and place your code between the tags. [code] Your code here. [/code]

Last edited on
Awesome, I appreciate the feedback!

I guess to narrow down, in the right hand column of output I cannot seem to get the decimals to line up, while retaining the spaces in between the two columns. Is it just a matter of playing around with justifications? On my end, day ten yields $10.24, with the $ signs lined up, not the decimal. I just need to scoot the day 10 $ amount over to the left 1.
Topic archived. No new replies allowed.