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=cplusplus]#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int days;
	double	salary;
	double const dailyPay=0.01;


		cout << "How many days did you work?: ";
		cin >> days;
			while (days <1 || days>31)
				 {cout << "That is an invalid days selection, please select 1-31: ";
				  cin >> days;}
				salary = (dailyPay * 2) * days;


				cout << "\nPer day                Total";
				cout << "\n-----------------------------\n";

				cout << "Your total salary is: " << fixed << setprecision (2) << salary;
			cout << "\n";



return 0;

}

Recommended Answers

All 18 Replies

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.

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=cplusplus]#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int days;
	double	salary;
	double const dailyPay=0.01;


		cout << "How many days did you work?: ";
		cin >> days;
			while (days <1 || days>31)
				 {cout << "That is an invalid days selection, please select 1-31: ";
				  cin >> days;}
				salary = (dailyPay * 2) * days;


				cout << "\nPer day                Total";
				cout << "\n-----------------------------\n";

				cout << "Your total salary is: " << fixed << setprecision (2) << salary;
			cout << "\n";



return 0;

}

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.

commented: La la la +7

You don't need to quote the entire message.

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

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"

Yes. I left out a word: "assign dailyPay to equal firstDayPay"

Ok I have done that but it does nothing.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int days = 1;
	 double firstDayPay=0.01;
	double	salary=1,
		dailyPay=firstDayPay;


		cout << "How many days did you work?: ";
		cin >> days;
			while (days <1 || days>31)
				 {cout << "That is an invalid days selection, please select 1-31: ";
				  cin >> days;}
				salary = (firstDayPay * 2) * days;
				dailyPay = salary;


				cout << "\nPer day                Total";
				cout << "\n-----------------------------\n";

				cout << "Your total salary is: " << fixed << setprecision (2) << salary;
			cout << "\n";



return 0;

}

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.

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.

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.

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.

Maybe I am not understanding what you are asking, but this is what my code does right now.

It ask for the number of days worked this month, as long as it is in between 1-31
then it takes the days I put in and first multiplies (0.01*2)*#days I put in and produces a result based off the days I have put in.

But that wouldn't give the right result. It would give 0.50 if the number of days was 25. But on the 25th day, the person would earn 167772.16.

But that wouldn't give the right result. It would give 0.50 if the number of days was 25. But on the 25th day, the person would earn 167772.16.

Ok yes I know it is not producing the right result. That is my problem as how do I get the code to produce the right result.

I believe I need to set something to 0 and have it double each day they work, but I just do not get it.

If you set something to 0 you can double it for a lifetime - it will never become something different than 0.

Think on using a loop to add the dailySalary to the total earntMoney for each day of work. Then you can adjust the value of dailySalary to be added in every cycle of the loop at your pleasure

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int days = 1;
	 double firstDayPay=0.01;
	double	salary=1,
		dailyPay=firstDayPay;


		cout << "How many days did you work?: ";
		cin >> days;
			while (days <1 || days>31)
				 {cout << "That is an invalid days selection, please select 1-31: ";
				  cin >> days;}
				salary = (firstDayPay * 2) * days;
				dailyPay = salary;


				cout << "\nPer day                Total";
				cout << "\n-----------------------------\n";

				cout << "Your total salary is: " << fixed << setprecision (2) << salary;
			cout << "\n";



return 0;

}

You need a loop, right? You have a loop, but only to get good input. Once you have good input, say 10, then you need to do something ten times, right?

should display a table showing how much the salary was for each day worked

That is one cout statement for every day. Right now you only have a cout statement for the grand total:

cout << "Your total salary is: " << fixed << setprecision (2) << salary;

Where is the the cout statement for the individual days?

Ok I am just not getting it at all.

I had programming.

Got it working now. I just subracted 0.01 from the total and it comes up with the correct amount. Would there be an easier way to do this?

Thanks.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int days, firstday;
    double numPay = .01;
    double total = 0;
			


		cout << "How many days did you work?: ";
		cin >> days;
		cout <<"\n";
			while (days <1 || days>31)
				 {cout << "That is an invalid days selection, please select 1-31: ";
				  cin >> days;}
			firstday=1;

			cout<<"Day"<<"\t\t"<<"Amount Earned"<<endl;
			cout<<"----------------------------"<<endl;

			while (firstday<=days)
			{cout<< firstday <<"\t\t\t"<< numPay <<endl;
          numPay = numPay *2;
          firstday++;
		  }
		  total=numPay-0.01;
          cout<<" "<<endl;
          cout<< fixed << showpoint << setprecision(2);
          cout<<" Total Amount To Be Paid Is $"<<"\t"<< total << endl;
          cout<<" "<<endl;
          return 0;
          }

How about this

//...With original code
salary = (dailyPay*days)*2;
//...
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int days, firstday;
    double numPay = .01;
    double total = 0;
			


		cout << "How many days did you work?: ";
		cin >> days;
		cout <<"\n";
			while (days <1 || days>31)
				 {cout << "That is an invalid days selection, please select 1-31: ";
				  cin >> days;}
			firstday=1;

			cout<<"Day"<<"\t\t"<<"Amount Earned"<<endl;
			cout<<"----------------------------"<<endl;

			while (firstday<=days)
			{cout<< firstday <<"\t\t\t"<< numPay <<endl;
          numPay = numPay *2;
          firstday++;
		  }
		  total=numPay-0.01;
          cout<<" "<<endl;
          cout<< fixed << showpoint << setprecision(2);
          cout<<" Total Amount To Be Paid Is $"<<"\t"<< total << endl;
          cout<<" "<<endl;
          return 0;
          }

I would go through this code and rename a lot of the variables. The computer couldn't care less, but anyone reading it and trying to understand the code (including yourself) is going to get pretty confused because the variable names aren't a good description of what they represent.

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.