Write a program that calculates the amount a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should display a table showing the salary 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.
#include <iostream>
#include <iomanip>
usingnamespace std;
int numDays = 1;
int main ()
{
int numDays;
double money, total, daypay;
int numDays=1;
constdouble money=1.0;
constdouble total=0.0;
constdouble daypay=0.0;
cout << "How many days did you work? ";
cin >> numDays;
while (numDays<1)
{
cout << "Please enter the number of days you worked \n";
cin >> numDays;
}
for (int i = 1; 1 <= numDays; i++)
{ // This brace was missing before
daypay = money/100;
cout << " Day " << 1 << " You earned $" << daypay << "\n";
total += daypay;
money += 2;
}
cout << "total earning are $" << total << endl;
return 0;
}
Problems:
Line 12 shadows the variable declared on line 5. Remove line 5. Prefer to keep variables as local as possible.
Lines 13,14,15: Don't declare these variables as const. You're going to be changing their values.
Line 26: You've created an infinite loop because your loop condition is incorrect. The condition in your code is 1 <= numDays; This will never be true. You mean to say i instead of 1.