Need Help! Formatting issue!

This is my code, I have a formatting issue with day one being moved over, can anyone help me out? thank you!


#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 << "Day " << 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;
}

This is the output

Starting salary: 0.02
Days worked: 20
************************************
Daily Salary Amount Earned
Day 1: 0.02 0.
02
Day 2: 0.04 0.06
Day 3: 0.08 0.14
Day 4: 0.16 0.30
Day 5: 0.32 0.62
Day 6: 0.64 7.66
Day 7: 1.28 2.54
Day 8: 2.56 5.10
Day 9: 5.12 10.22
Day 10: 10.24 20.46
Day 11: 20.48 245.74
Day 12: 40.96 81.90
Day 13: 81.92 163.82
Day 14: 163.84 327.66
Day 15: 327.68 655.34
Day 16: 655.36 7864.30
Day 17: 1310.72 2621.42
Day 18: 2621.44 5242.86
Day 19: 5242.88 10485.74
Day 20: 10485.76 20971.50

--------------------------------
Process exited after 0.1105 seconds with return value 0
Press any key to continue . . .
Last edited on
cout << setw(0) << "Day " << d + 1 << ":" << '\t' << fixed << setprecision(2) << setw(18) << salary << '\t' << setw(18) << (totalpay+bonus) << endl;
Here (line 29 or so),
<< setw(30) << endl;
the setw serves no purpose. Remove it.

In general, it's probably better to avoid using the tab character '\t' whenever you are seriously concerned with formatting.

Regarding the calculations, some of the values can easily exceed the limited precision of type float (about 7 decimal digits). Use double instead (about 15 decimal digits).
Last edited on
Topic archived. No new replies allowed.