I'm trying to write a program which allows a person to enter the number of days they have worked. Their hypothetical salary is 0.01$ per day and doubles everyday (0.02$ on the 2nd and 0.04$ on the 3rd day etc...). For each day, the day number, pay for the day, and accumulated total pay has to appear. An example below:

Enter number of days: 3

Day-----Pay--- total pay
1------ 0.01----0.01
2------0.02----0.03
3------0.04----0.07
4------0.08----0.15

The program works, but I'm having difficulty with total pay. I'm not sure if its the math, or the structure it is written.
Where have I gone wrong?
I've gotten this so far:

int main ()
{

int x, days, ndays;
cout << " Enter number of days: " << endl;
cin >> ndays;
cout << endl;

double pay ;
double total_pay = 0.01 ;


cout << left << setw(15) << " DAY "
<< right << setw(10) << " PAY "
<< setw(15) << " TOTAL PAY " << endl;

for (days = 1, pay = 0.01; days <= ndays; days ++, pay *= 2)

if (total_pay += pay * 2) /* how do I get it to start on 0.01?*/


cout << left << setw(15) << days
<< right << setw(10) << pay
<< setw(15) << total_pay << endl;


system ("PAUSE");
return 0;
} // end of for loop

Recommended Answers

All 3 Replies

can you post again your code in the code tags please.

int main ()
{

int x, days, ndays;
cout << " Enter number of days: " << endl;
cin >> ndays;
cout << endl;

double pay ;
double total_pay = 0.01 ;


cout << left << setw(15) << " DAY "
<< right << setw(10) << " PAY "
<< setw(15) << " TOTAL PAY " << endl;



for (days = 1, pay = 0.01; days <= ndays; days ++, pay *= 2)

if (total_pay += pay * 2) /* how do I get it to start on 0.01?*/


cout << left << setw(15) << days
<< right << setw(10) << pay
<< setw(15) << total_pay << endl;


system ("PAUSE");
return 0;
} // end of for loop

What should total_pay start at?

I know you want it to start at $0.01, but you should take a step back and think about that a second. If you haven't performed any work yet, you haven't been paid yet.

Also:

system ("PAUSE");
return 0;
} // end of for loop

This is the end of your program; it is not the end of your for-loop. Your for loop ends after Line 21 (see quoted code above).

Hello stan2000,
The problem is in your for loop. You initialized total_pay as 0.01. Inside the for loop total_pay gets added to pay*2 which value on day 1 becomes 0.03( that is 0.01+ (0.01*2)). so the total_pay will not start from 0.01.
To start total_pay from 0.01 you should initialize it to 0.00 in
double total_pay=0.00;
Next you should initialize pay as 0.01 in
double pay=0.01;
Also change your for loop like this one below...

double pay=0.01;
double total_pay=0.00;
for (days = 1; days <= ndays; days ++) 
{total_pay=total_pay+pay;  // the total_pay which was zero first will now become 0.01 
 cout << days<<"\t"<<pay<<"\t"<<total_pay << endl;//on day 1
 pay=pay*2;  //here only the pay is doubled
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.