Friday, December 9, 2016

Programming Challenge 5.7 - Pennies for Pay

/* Pennies for Pay - This program calculates how much a person would earn over a
   period of time if his or her salary is one penny the first day and two pennies
   the second day, and continues to double each day. The program asks the user for
   the number of days. A table is displayed showing how much the salary was for
   each day, and then shows the total pay at the end of the period. The output is
   displayed in a dollar amount.
  
   Input Validation: No numbers less than 1 for the number of days worked is
   accepted. */

#include "Utility.h"

int main()
{
    /* Constants: Pennies min, Pennies max */
    const double PENNIES_MIN = .01,
                 PENNIES_MAX = .1;

    /* Variables: Number of days worked, Days (counter variable) */
    int daysWorked = 0,
        days = 1;
   
    /* Variables: Pennies (declared and initialized with the value PENNIES_MIN),
       Dollars, Total */
    double pennies = PENNIES_MIN,
           dollars = 0,
           total = 0;

    /* Input: Display information, Ask the user for the number of days he or
       she has worked */
       /* Display: Information, Table header */
    cout << "Suppose you work for a certain number of days, and for each day you\n"
        << "double the number of pennies earned. On day 1 you earn 1 p., 2 after\n"
        << "day 1, 4 on the third day, and so on. To find out, this program will\n"
        << "calculate the total amount of savings.\n"
        << "\nHow many days have you worked (1 and above)? ";
    cin >> daysWorked;

    /* While loop::Input Validation: If the amount of days is lower than 1, the
       user is asked again, until he or she provides a valid number */
    while (daysWorked < 1)
    {
        /* Display: Error message */
        cout << "You entered an invalid number for days worked. Only positive\n"
            << "numbers, 1 and above, are allowed.\n";

        /* Ask the user again */
        cout << "How many days have you worked? ";
        cin >> daysWorked;
    }

    /* Display: Table header */
    cout << "\nIf you work:\t\t\t" << "Your salary will be:\n"
         << "--------------------------------------------------------------------------\n";

    /* For loop: This loop uses multiple update expressions. Each time the loop
       iterates, days is increased by 1, and pennies is incremented by * 2 for
       each day */
    for (days; days <= daysWorked; days++, pennies *= 2)
    {
        /* Nested While loop: Once the amount of pennies equals PENNIES_MAX (100),
           dollars gets the value of pennies. */
        while (pennies == PENNIES_MAX)
        {
            dollars = pennies;
        }

        /* Display: The formatted output */
        cout << fixed << showpoint << setprecision(2);
        cout << setw(6) << right << days << " day(s)"
             << setw(6) << right << "\t\t\t$ " << setw(32) << right << (dollars + pennies) << " pennies\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment