//Devin Scheu CS1A Chapter 5, P. 295, #7
//
/**************************************************************
*
* CALCULATE DOUBLED PENNY SALARY OVER TIME
* ____________________________________________________________
* This program determines the daily salary and total earnings
* when the salary starts at one penny and doubles each day.
*
* Computation is based on a loop doubling the salary each day
* and converting the total to dollars
* ____________________________________________________________
* INPUT
* daysWorked : The number of days worked
*
* OUTPUT
* dailySalaryTable : The table of daily salaries in dollars
* totalEarnings : The total earnings in dollars
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main () {
//Variable Declarations
int daysWorked; //INPUT - The number of days worked
double dailySalary; //OUTPUT - The salary for each day in dollars
double totalEarnings; //OUTPUT - The total earnings in dollars
//Prompt for Input
cout << "Enter the number of days worked: ";
cin >> daysWorked;
//Input Validation
while (daysWorked < 1) {
cout << "\nError: Please enter a number greater than or equal to 1: ";
cin >> daysWorked;
}
//Initialize Variables
totalEarnings = 0.0;
//Display Header
cout << fixed << setprecision(2);
cout << "\nDay\tDaily Salary ($)" << endl;
cout << "------------------------" << endl;
//Calculate and Display Daily Salary
for (int day = 1; day <= daysWorked; day++) {
dailySalary = (0.01 * pow(2, day - 1)); // Convert pennies to dollars and double each day
totalEarnings += dailySalary;
cout << left << setw(4) << day << right << setw(16) << dailySalary << endl;
}
//Display Total Earnings
cout << "------------------------" << endl;
cout << left << setw(4) << "Total" << right << setw(16) << totalEarnings << endl;
return 0;
} //end of main()