Need homework help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Need homework help:Double pay each day, total pay per day, total pay for month

 
0
  #1
Nov 19th, 2008
Write a program that calculates how much a person earns in a month if the salary is one penny the first day and doubles each day worked. The program should ask the user for the number of days worked during the month and should display a table showing how much the salary was for each day worked, as well as the total pay earned for the month.

Input validation: Do not accept a number less than 1 or more than 31.

Here is my code so far as I am stumped on how to get the salary to double with each day. I believe you have to set something to =0*2 but not sure exactly what

The stuff in bold I have so far in the above description.

[CODE]
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. int days;
  9. double salary;
  10. double const dailyPay=0.01;
  11.  
  12.  
  13. cout << "How many days did you work?: ";
  14. cin >> days;
  15. while (days <1 || days>31)
  16. {cout << "That is an invalid days selection, please select 1-31: ";
  17. cin >> days;}
  18. salary = (dailyPay * 2) * days;
  19.  
  20.  
  21. cout << "\nPer day Total";
  22. cout << "\n-----------------------------\n";
  23.  
  24. cout << "Your total salary is: " << fixed << setprecision (2) << salary;
  25. cout << "\n";
  26.  
  27.  
  28.  
  29. return 0;
  30.  
  31. }
Last edited by davids2004; Nov 19th, 2008 at 1:42 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need homework help

 
1
  #2
Nov 19th, 2008
Initialize the salary at 1 and, each time through some look that runs 'days' times, double it.

Or you could just use 1 << (n - 1) to compute the amount paid on the nth day, or (1 << n) - 1 will give you the cumulative amount paid after n days.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Need homework help:Double pay each day, total pay per day, total pay for month

 
1
  #3
Nov 19th, 2008
Originally Posted by davids2004 View Post
Write a program that calculates how much a person earns in a month if the salary is one penny the first day and doubles each day worked. The program should ask the user for the number of days worked during the month and should display a table showing how much the salary was for each day worked, as well as the total pay earned for the month.

Input validation: Do not accept a number less than 1 or more than 31.

Here is my code so far as I am stumped on how to get the salary to double with each day. I believe you have to set something to =0*2 but not sure exactly what

The stuff in bold I have so far in the above description.

[CODE]
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. int days;
  9. double salary;
  10. double const dailyPay=0.01;
  11.  
  12.  
  13. cout << "How many days did you work?: ";
  14. cin >> days;
  15. while (days <1 || days>31)
  16. {cout << "That is an invalid days selection, please select 1-31: ";
  17. cin >> days;}
  18. salary = (dailyPay * 2) * days;
  19.  
  20.  
  21. cout << "\nPer day Total";
  22. cout << "\n-----------------------------\n";
  23.  
  24. cout << "Your total salary is: " << fixed << setprecision (2) << salary;
  25. cout << "\n";
  26.  
  27.  
  28.  
  29. return 0;
  30.  
  31. }
Line 10. Why is dailyPay a constant? If dailyPay doubles each day, then it is changing and therefore should not be a constant. If you want to have a constant variable, create a variable called firstDayPay , make it constant, assign it to equal one penny, than assign dailyPay to firstDayPay . Then start doubling dailyPay . dailyPay needs to change each day.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need homework help

 
2
  #4
Nov 19th, 2008
You don't need to quote the entire message.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need homework help:Double pay each day, total pay per day, total pay for month

 
0
  #5
Nov 19th, 2008
Originally Posted by Rashakil Fol View Post
Initialize the salary at 1 and, each time through some look that runs 'days' times, double it.

Or you could just use 1 << (n - 1) to compute the amount paid on the nth day, or (1 << n) - 1 will give you the cumulative amount paid after n days.
Where would I put this in my code. I am new at this and have found this sit to be very helpful.

Originally Posted by VernonDozier View Post
Line 10. Why is dailyPay a constant? If dailyPay doubles each day, then it is changing and therefore should not be a constant. If you want to have a constant variable, create a variable called firstDayPay , make it constant, assign it to equal one penny, than assign dailyPay to firstDayPay . Then start doubling dailyPay . dailyPay needs to change each day.
Ok I have changed the dailyPay from constant to int. I have created a new variable called firstDayPay and set it to be const double and set it equal to 0.01. What do you mean assign dailyPay to firstDayPay. Do you mean dailyPay=firstDayPay?

Thanks to both of you
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Need homework help:Double pay each day, total pay per day, total pay for month

 
0
  #6
Nov 19th, 2008
Originally Posted by davids2004 View Post
What do you mean assign dailyPay to firstDayPay. Do you mean dailyPay=firstDayPay?

Thanks to both of you
Yes. I left out a word: "assign dailyPay to equal firstDayPay"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need homework help:Double pay each day, total pay per day, total pay for month

 
0
  #7
Nov 19th, 2008
Originally Posted by VernonDozier View Post
Yes. I left out a word: "assign dailyPay to equal firstDayPay"
Ok I have done that but it does nothing.

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. int days = 1;
  9. double firstDayPay=0.01;
  10. double salary=1,
  11. dailyPay=firstDayPay;
  12.  
  13.  
  14. cout << "How many days did you work?: ";
  15. cin >> days;
  16. while (days <1 || days>31)
  17. {cout << "That is an invalid days selection, please select 1-31: ";
  18. cin >> days;}
  19. salary = (firstDayPay * 2) * days;
  20. dailyPay = salary;
  21.  
  22.  
  23. cout << "\nPer day Total";
  24. cout << "\n-----------------------------\n";
  25.  
  26. cout << "Your total salary is: " << fixed << setprecision (2) << salary;
  27. cout << "\n";
  28.  
  29.  
  30.  
  31. return 0;
  32.  
  33. }
Last edited by davids2004; Nov 19th, 2008 at 2:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need homework help

 
2
  #8
Nov 19th, 2008
How about you go through your code by hand with some sample inputs and follow what it is doing. Then you'll see why your code doesn't work.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need homework help

 
0
  #9
Nov 19th, 2008
Originally Posted by Rashakil Fol View Post
How about you go through your code by hand with some sample inputs and follow what it is doing. Then you'll see why your code doesn't work.
Ok I do put in sample inputs and it comes out producing the correct result. It is basically multiplying .02 * the number of days I put in and out comes the correct amount. I am trying to get the dailyPay to double each day per day worked.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need homework help

 
1
  #10
Nov 19th, 2008
If you think your code produces the correct results on the inputs you've tried, maybe you could say what your code does to us, step by step. We could then see where you have a mistaken understanding.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC