Flow Control 1C

Description

The purpose of this challenge is to use the IF statement to control program flow. This challenge simulates using a credit or debit card at the gas pump.

Requirements

  1. Declare a double variable called total
  2. Declare a double variable called gallons
  3. Declare a char variable called payment_type
  4. Declare a double variable called cost. Initialize this value to 2.55
  5. Declare a bool variable called authorized. Initialize this value to false.
  6. Ask the user the type of payment that will be made. Use the payment_type variable to receive this user input.
  7. If the user enters a ‘c’, it will be a credit card payment
    1. Ask the user to enter a zipcode (you will have to create a variable to store the value of the zipcode that the user enters; you decide what data type is appropriate)
    2. Write a nested-if statement and check that the value of the zipcode entered is 93301. If the zipcode entered is correct, set authorized to true.
  8. If the user enters a ‘d, it will be a debit card payment.
    1. Ask the user to enter a PIN (You will have to create a variable to store the value of the PIN that the user enters; you decide what data type is appropriate)
    2. Write a nested-if statement and check that the value of the PIN entered is 1234. If the PIN entered is correct, set authorized to true and change the value of cost to 2.35.
  9. If authorized is true, only then will you do the following:
    1. Ask the user how many gallons are being purchased. User will enter a numeric amount. Use gallons variable to hold this amount.
    2. Calculate the appropriate total of the purchase depending on the payment type. Use the total variable to calculate the total amount. Additionally, you will need to add 0.35 to the total cost for debit card purchases.
    3. Display the cost of the purchase to the user.
  10. Add an else block after checking if authorized is true. In this section, show an appropriate message indicating an invalid PIN or ZIP code (see the Sample Interaction below)

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 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-FLOW0001C

Print Requirements