943,685 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1350
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2008
0

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

Expand 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]
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 19th, 2008
1

Re: Need homework help

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.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Nov 19th, 2008
1

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

Click to Expand / Collapse  Quote originally posted by davids2004 ...
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]
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Nov 19th, 2008
2

Re: Need homework help

You don't need to quote the entire message.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Nov 19th, 2008
0

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

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.

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
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 19th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by davids2004 ...
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"
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Nov 19th, 2008
0

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

Yes. I left out a word: "assign dailyPay to equal firstDayPay"
Ok I have done that but it does nothing.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 19th, 2008
2

Re: Need homework help

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.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Nov 19th, 2008
0

Re: Need homework help

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 19th, 2008
1

Re: Need homework help

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.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: isdigit ç à è
Next Thread in C++ Forum Timeline: memory game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC