English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

This program is supposed to calculate how much a person would earn over a period of time if his salary was I penny the 1st day, two pennies the 2nd day, 4 pennies the 3rd day, 8 pennies the 4th day and so on.
The program should ask the user for the number of days. Then, it is to display a table showing how much the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies.

I was told that I should use a while loop for input validation because it's not supposed to accept a number less than 1 for the number of days worked.

The formula is 2^n-1

Finally, I tried to use a for loop to do the calculation because it's supposed to display a list of totals for each day.

If you could copy and paste and correct this for me, I would greatly appreciate it thanks.

2007-11-09 04:04:05 · 6 answers · asked by USAman 6 in Computers & Internet Programming & Design

// bla bla...
#include
#include
using namespace std;

int main ()
{
int days1;
int days2;
double total = 0.0;

cout << "Enter the amount of days.n\ ";
cin >> days1;
while (day1 < 1)
{
cout << "ERROR: You can't enter a value less than 1. ";
cin >> days1;
}

for (int count = 1; count <= days1; count ++)
{
total += pow(2.0), days1 -1;
}
cout << "The total earned is $" << total << endl;
return 0;
}

2007-11-09 04:04:29 · update #1

6 answers

I have corrected as follows

#include
#include
#include
int main ()
{
int days1;
int days2;
double total = 0.0;

cout << "Enter the amount of days.n\ ";
cin >> days1;
while (days1 < 1)
{
cout << "ERROR: You can't enter a value less than 1. ";
cin >> days1;
}

for (int count = 1; count <= days1; count ++)
{
total += pow(2.0, days1 -1);
}
cout << "The total earned is $" << total << endl;
return 0;
}

2007-11-09 04:12:01 · answer #1 · answered by Kausik kumaar 5 · 1 0

Your while loop should use days1 and not day1.

Just eliminate the loop and output the value as:
cout << "total is " << pow(2, days1-1)/100 << endl;

However, since it needs to display a table, your loop should look like the following:

cout << "Day 1: $0.01";
for (int count = 2; count <= days1; count++)
{
if (!count % 5)
cout << endl;
total *=2.0; /* No need to envoke POW() for just *2 */
cout << "\tDay " << count << ": $" << total;

}
cout << endl << "The total earned is $" << total << endl;

2007-11-09 04:11:20 · answer #2 · answered by BigRez 6 · 0 0

#include
#include
using namespace std;

int main ()
{
int days1;
double x=0.0;
double total;

cout << "\nEnter the amount of days = ";
cin >> days1;
while (days1 < 1)
{
cout << "\nERROR: You can't enter a value less than 1. ";
cin >> days1;
}
total=0.0;
for (int count = 1; count <= days1; count ++)
{
x = pow(2,count-1);
total=total+x;
cout << "Day : " << count << " -> " << x;
cout << " Total : " << total << endl;
}
cout << "\nThe total earned is $" << total/100.0 << endl;
system("pause");
return 0;
}

2007-11-09 04:44:06 · answer #3 · answered by iyiogrenci 6 · 0 0

Also for the formula, it should be

total += pow(2,count-1);

to compute 2^0 + 2^1 + 2^2 ... up to 2^(days1-1)

2007-11-09 04:15:33 · answer #4 · answered by macharoni 3 · 0 0

First

2016-04-03 03:54:35 · answer #5 · answered by ? 4 · 0 0

this line (line 22 i believe...) within your for loop...

total += pow(2.0), days1 -1;

...will likely cause a syntax error unless you change the comma after the pow function into a semicolon.

hope this helped...good luck.

2007-11-09 04:15:01 · answer #6 · answered by massiv_x 3 · 0 1

fedest.com, questions and answers