I am totally lost on this question

Pages: 12
Hey!! guys i don't understand loops so i want a little from everyone

Here is the question :

Write a program that calculates how much a person earns in a month if the salary is one penny the first day, two pennies the second day, four pennies the third day, and so on with the daily pay doubling each day the employee works. The program should ask the user for the number of days the employee 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. The output should be displayed in dollars with two decimal points, not in pennies.

Input Validation: Do not accept a number less than 1 or more than 31 for the number of days worked.


I HOPE THAT I WILL GET SOME HLP

THANKS
First take input.
Then validate it (check if it's more than 31 or less than 1).
Then use a loop to calculate the money, and display it. (you can use "\t" to make a table).
This is what i did so far...please guide me further
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>
using namespace std;

int main ()
{
	int days;

	cout << "Enter the number of days worked." << endl;
	cin >> days;

	while ((days < 1) || (days > 31))
	{
		cout << "Enter the valid number of days worked." << endl;
		cin >> days;
	}
That's great. Now make two variables, days_so_far, money. each day, add 2**days_so_far to the money. Use the pow function from cmath.
i don't get it..
Use the pow function from cmath.


It would be an ineffiecient use of this function. From what I understand he needs the table to display every day worked and a total for the month. I would use a for loop that displays salary for one day, as well as the sum, for every iteration.

@qwertyasdfgh:
If you need further explanation or an example of a for loop go ahead and ask.
please give an example..im jus not getting this
Here is the syntax of a for loop:

1
2
3
4
for (initializer of counter variable; control expression; counter)
{
     // code
}

The initializer sets a counter variable to a certain value. The control expressions tells the loop to stop when a condition is no longer true. The counter usually just increments or decrements the counter variable, but could also be something like counter+=10. The code within the brackets is executed until the control expression is false.

An example:

1
2
3
4
for (short days_so_far = 1; days_so_far<=days; days_so_far++)
{
     cout << days_so_far << '\n';
}


Edit: Fixed an two small errors.
Last edited on
no what I meant was that the sum of 2**i as i goes from zero to n is 2**(n) -1. that is,
day: money
1: 1 = 2**1 -1
2: 3 = 2**2-1
3: 7 = 2**3-1
4: 15 = 2**4-1
still don't get it...first tell me is the code i wrote at the top correct up to that point or no ?? if it is tell me what to do further and tell me exactly what i need to type in my compiler. Give me examples using my own variables.thanks

I told you it's correct. However, I won't just do your work for you. take a shot at it, and I'll help you.
aite fine..let me try
this is what i entered after

1
2
3
4
5
6
7
8
9
10
11
12
cout << "Number of days worked" << "    " << "Salary per day" << endl;
cout << "-----------------------------------" << endl;
	
salary = 1;
while (salary <= days)
	

{
	cout << "    " << salary << "    " << 0.01 * 2 << endl;
	salary++;
}
	
You need to print pow(2,salary-1), and pow(2, salary)-1.
i replaced 0.01 *2 with pow(2,salary-1) which is displaying the numbers correctly on the left but values like 1,2,4 on the right ..there is supposed to be 0.01,0.02,0.04 and so on ..hw do i do that.
Last edited on
Why not just .01*pow(2,salary-1)?
that won't work
wait it does..hold on
everything works now what abt the total suppose the user enters 3 so it is gonna display 1,2,3 on the left and 0.01,0.02,0.04 on the right so now how to add the total..based on the number the user enters
the total for n days is pow(2,days)-1.
Pages: 12