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.
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>
usingnamespace 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;
}
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.
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.
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 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.
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