Sunday, January 1, 2017

Programming Challenge 6.1 - Markup

/* Markup - This program asks the user to enter an item's wholesale cost and its
   markup percentage. It then displays the item's retail price.
  
   * If an item's wholesale cost is $5.00 and its markup percentage is
     100%, then the item's retail price is $10.00.

   * If an item's wholesale cost is $5.00 and its markup percentage is
     50%, then the item's retail price is $7.50.

   This program has a function named calculateRetail that receives the
   wholesale cost and the markup percentage as arguments and returns
   the retail price of the item.

   Input Validation: No negative values for either the wholesale cost
   of the item or the markup percentage are accepted. */

#include "Utility.h"

/* Prototype: Calculate retail */
double calculateRetail(double, double);

int main()
{
   /* Variables: Wholesale cost, Markup percentage, Retail price */
   double wholesaleCost = 0.0,
          markupPct = 0.0,
          retailPrice = 0.0;

   /* Display: Information
      Get: Wholesale cost */
   cout << "\t\tRetail Price Calculator\n\n"
        << "This program allows you to calculate the retail price\n"
        << "of an item, by providing the price, and the markup\n"
        << "percent of the item.\n\n"
        << "Please enter the wholesale cost of the item: $";
   cin >> wholesaleCost;

   /* Validate input: While a negative value is entered for
      wholesaleCost, an error message is displayed, and the
      user is asked to repeat his or her input */
   while (wholesaleCost <= 0)
   {
      cout << "\nSorry, but only positive values are allowed.\n"
           << "Please enter the wholesaleCost of the item: $";
      cin >> wholesaleCost;
   }

   /* Get: Markup percentage */
   cout << "\nPlease enter the markup percentage of the item: %";
   cin >> markupPct;

   /* Validate input: While markupPct gets a negative value, an
      error message is displayed, and the user is asked to repeat
      his or her input */
   while (markupPct <= 0)
   {
      cout << "\nSorry, only positive values are accepted as input.\n"
           << "Please enter the markup percentage of the item: %";
      cin >> markupPct;
   }

   /* Call: calculateRetail */
   retailPrice = calculateRetail(wholesaleCost, markupPct);

   /* Set up: Numeric output */
   cout << fixed << showpoint << setprecision(2);

   /* Display: The retail price of the item */
   cout << "\nThe retail cost of the item is: $" << retailPrice << endl;

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: calculateRetail

   This function gets its arguments from wholesalePrice and
   markupPct, stores the values in cost and percent, then
   calculates and returns the retail price.
   ********************************************************** */

double calculateRetail(double cost, double percent)
{
   /* Variable: Retail cost */
   double retailCost = 0.0;

   /* Calculate: Retail cost */
   retailCost = cost + (cost * percent) / 100;

   /* Return: retailCost to retailPrice in main */
   return retailCost;
}

No comments:

Post a Comment