Flow Control 1E

Description

The purpose of this challenge is to use various control constructs with functions. This challenge simulates using a credit or debit card at the gas pump.

Requirements

  1. Create two global int constants.
int const PAY_CREDIT = 1;
int const PAY_DEBIT = 2;
  1. Create a function int get_payment_type(). This function will ask the user what type of payment will be used. It will return either PAY_DEBIT or PAY_CREDIT depending on what the user selected. This function will cout a message asking the user to choose the payment type. The return value of get_payment_type() will be used in main to determine how the payment is authorized and what subsequent costs will be. (See sample main())
  2. Declare double variables called cost, gallons
  3. Declare double constants
double const DEBIT_COST = 2.35;
double const CREDIT_COST = 2.55;
  1. Declare a string variable called validator.
  2. Declare a bool variable in main() called is_authorized
  3. Create a function prototype bool authorize(string validator, int key = 1234). Its matching definition will look like bool authorize(string validator, int key). Note that the key parameter will have a default value specified in the prototype. In this function, ask the user to enter a value, using the validator as part of the message (See sample main()). This function returns a true or false value depending on if the user’s input matches key. You don’t have to display any errors or messages if they don’t match; simply return a boolean value. 
  4. Create a function double get_gallons(). This function simply asks the user to enter a numerical value for the gallons. The return value is what the user entered.
  5. Create a function double calculate(double gallons, double gallon_cost). This function returns the total cost based on gallons and gallon_cost.
  6. Create an overloaded function double calculate(double gallons, double gallon_cost, double fee). This function returns the total cost based on gallons and gallon_cost. Additionally, it will add the fee to the cost.
  7. Depending on the payment type selected by the user, set the value of validator accordingly to either “Zip code” or “PIN”. Also, based on the payment type, call calculate() accordingly; with a fee for debit, or without a fee for credit transactions. (See sample main()).
  8. Create a function void display_message(double gallons, double cost). This function simply displays the following message with the appropriate values for GALLONS and COST substituted with the actual values. Call this function from main().
Thank you for your purchase of GALLONS gallons. Please pay COST. All taxes are included DRIVE SAFE - DON'T TEXT AND DRIVE

Hints

  1. Include the <iomanip> header file. This will allow you to use various options to format your output
  2. It’s good practice to initialize variables to default values that make sense (for example, set gallons to 0.00)
  3. Include the code below before any other cout lines. The setprecision(x) will display your decimal output with x decimal places.
cout << fixed << setprecision(2);

Sample main()

int main()
{
  // ask user for payment type
  int payment_type = get_payment_type();
  bool is_authorized = false;
  double gallons, cost;
  double const DEBIT_COST = 2.35;
  double const CREDIT_COST = 2.55;
  string validator;

  switch (payment_type)
  {
    case PAY_DEBIT:
      validator = "PIN";
      is_authorized = authorize(validator);
      if (is_authorized)
      {
        gallons = get_gallons();      
        cost = calculate(gallons, DEBIT_COST, 0.35);
        cout << "Gallon cost: " << DEBIT_COST << endl;
      }
      break;
    case PAY_CREDIT:
      validator = "Zip code";
      is_authorized = authorize(validator, 93301);
      if (is_authorized)
      {
         gallons = get_gallons();
         cost = calculate(gallons, CREDIT_COST);
         cout << "Gallon cost: " << CREDIT_COST << endl;
      }
      break;
  }

  if (is_authorized)
  {
    display_message(gallons, cost);
  }
  else
  { 
    cout << "Invalid " << validator << ". Transaction canceled" << endl;
  }  

  return 0;
}

Sample Interaction / Output

-- Welcome to GAS CLUB --
Are you paying with credit or debit (c/d)? c
Enter Zip code: 93301

How many gallons purchased? 10 
Gallon cost: $2.55
Thank you for your purchase of 10.00 gallons. Please pay $25.50.
All taxes are included
DRIVE SAFE - DON'T TEXT AND DRIVE

RUN IT AGAIN:

-- Welcome to GAS CLUB -- 
Are you paying with credit or debit (c/d)? d 
Enter PIN: 1234

How many gallons purchased? 22 
Gallon cost: $2.35

Thank you for your purchase of 22.00 gallons. Please pay $52.05.
All taxes are included
DRIVE SAFE - DON'T TEXT AND DRIVE

RUN IT AGAIN:

-- Welcome to GAS CLUB -- 
Are you paying with credit or debit (c/d)? d
Enter PIN: 0000
Invalid PIN. Transaction canceled.

RUN IT AGAIN:

-- Welcome to GAS CLUB -- 
Are you paying with credit or debit (c/d)? c 
Enter Zip code: 10101
Invalid Zip code. Transaction canceled.

LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT

CATALOG ID: CPP-FLOW0001D

Print Requirements