Saturday, November 19, 2016

Programming Challenge 4.9 - Change Dollar Game

/* Change Dollar Game - This is a game that gets the user to enter a number of
   coins required to make exactly one dollar. The program asks the user to enter
   the number of pennies, nickels, dimes, and quarters.
  
   If the value of the coins entered is equal to one dollar, the program
   congratulates the user for winning the game. Otherwise the program displays a
   message indicating whether the amount entered was more than or less than one
   dollar. */

#include "Utility.h"

int main()
{
    /* Hold coins */
    int pennies,
        nickels,
        dimes,
        quarters;

    /* Constant for dollar */
    const int DOLLAR = 100;

    /* Ask the user to enter a number for the coins */
    cout << "Enter an amount for every of the following coins and try to match\n"
        << "one dollars to win this game. Good Luck!\n";
    cout << "Enter pennies: ";
    cin >> pennies;
    cout << "Enter nickels: ";
    cin >> nickels;
    cout << "Enter dimes: ";
    cin >> dimes;
    cout << "Enter quarters: ";
    cin >> quarters;

    /* Calculations */
    pennies *= 1;
    nickels *= 5;
    dimes *= 10;
    quarters *= 25;

    /* Determine if amount of coins equals 1 dollar */
    if (pennies == DOLLAR)
    {
        cout << "Congratulations, you won!\n";
    }
    else if (pennies + nickels == DOLLAR)
    {
        cout << "Nickel and Dime, the dollar is thine!\n";
    }
    else if (pennies + nickels + dimes == DOLLAR)
    {
        cout << "Penny, Nickel and Dime, this dollar is no longer mine!\n";
    }
    else if (pennies + nickels + dimes + quarters == DOLLAR)
    {
        cout << "Penny, Nickel, Dime and Quarter, throwing this dollar in\n"
            << "your direction and not in the water!\n";
    }
    else
    {
        (pennies > DOLLAR || pennies + nickels > DOLLAR ||
            pennies + nickels + dimes > DOLLAR ||
            pennies + nickels + dimes + quarters > DOLLAR) ?
            cout << "Your guess was too high, time for another try!\n" :
            cout << "Oh what a blow, your guess was too low!\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment