C++ Program

MR7
Program2NOTES.rtf

Program 1:  Shipping Fees

The Acme Company sells Widgets at $2.00 each.  A customer has the following options for shipping:

  • Ground Delivery:  7 to 10 days          $  5.00
  • Express Delivery: 3.  2 days                                             $ 10.00 4.  Overnight                                       $ 20.00

Write a program using the steps in the lecture titled "Even More Algorithm Practice" from this module.

Be sure to do the following:

  • Prompt the User for how many Widgets they wish to purchase.
  • Error check the input to make sure that it's not zero.
  • Use an outer IF/ELSE where the IF is used to display an error message if the input is zero and the ELSE is used for steps 3 through 12 below and for printing the receipt.
  • Prompt the User for the delivery option.
  • Compute the sub-total cost of the Widgets.
  • Compute sales tax on the sub-total cost of the Widgets.  Use 8%.
  • Compute the total cost of the Widgets.
  • Determine the delivery cost.
  • Use an IF/ELSE structure to determine Ground or Express fee. 
  • Nest an inner IF within the Express option to determine the 2 day or overnight fee.
  • Once the shipping fee has been selected, add it to the total cost of the Widgets. 
  • Format all dollar amounts with two decimal places and add a dollar sign to each one.
  • Format the sales tax with two decimal places.
  • Only print out the receipt if the number of widgets is valid.
  • Print out the receipt showing the following:

Number of Widgets: Sub-Total Cost: Sales Tax: Sub-Total with Tax: Shipping Fee: Total Cost:

You will also need to do the following for the prologue section:

  • Create an initial algorithm of the basic steps.
  • Develop the refined algorithm from the initial algorithm.

Be sure to include with your program as documentation:

 

  • The Program Prologue
  • The Input data
  • The Output data
  • Any Formulas you may have.
  • The Initial Algorithm
  • The Refined Algorithm
  • Line-by-line documentation within the body of code.
  • The code to pause your output screen.

 

NOTE 1:

  • DO NOT use the "return" code, end, break, continue, or exit to force an exit from within your IF/ELSE structure.  
  • Let the program progress to the end of the main function. 
  • The end of the main function is the only place that the “return” should be placed. 
  • A switch statement is the only place that the break command should be placed.

NOTE 2:

1.  Declare all variables within the data declaration section of each class and method.  (-5) 2   Do not get input on the same line as a variable declaration.  (-5) 3.  Do not place an equation for computation on the same line as declaring a variable.  (-5) 4.   Do not place an equation for computation on the same line as input.  (-5)

1. IF/ELSE IF/ELSE Syntax

 

IF/ELSE IF/ELSE Statements

IF/ELSE IF/ELSE statements are really just special cases of the IF/ELSE. 

The basic syntax is as follows:

if ( logical_test_1 ) {     statement(s); }

else if ( logical_test_2 ) {      statement(s); }

else if ( logical_test32 ) {      statement(s); }

. .  //you can keep putting as many else if's as you need .

else {      statement(s); }

 

  • Notice that you can have any number of ELSE IF clauses, or just one.
  • Also note that the above syntax example shows curly brackets around statement(s). 
  • It is critical to remember that you must have curly brackets when you have more than one statement attached to any if/else if/ or else.
  • You do not need curly brackets if you only have one statement – however if you always include them then you don’t need to worry about forgetting for multiple statements.

When you use an ELSE IF structure, C++ will begin by evaluating the first condition.

  • If the first condition evaluates to False, then C++ will continue on and evaluate the second condition.
  • C++ will continue evaluating conditions in order UNTIL one of them evaluates to true.  
  • AS SOON AS a condition evaluates to True, C++ will stop evaluating conditions and perform the statements within the statement block of the true condition.
  • It is critical that you understand that C++ will NOT evaluate any conditions after a true one is encountered.
  • C++ evaluates each condition in order and STOPS as soon as it encounters a true condition.
  • If none of the conditions is true, then the ELSE is executed.

 

NOTE:  Do NOT use the break or the continue command in an IF/ELSE IF/ELSE structure.

2. IF/ELSE IF/ELSE Example

 

IF/ELSE IF/ELSE Exampl e

 

if (GPA >= 3.9) {      cout << "Summa Cum Laude\n"; }

else if (GPA >= 3.7) {      cout << "Magna Cum Laude\n"; }

else if (GPA >= 3.5) {      cout << "Cum Laude\n"; }

else {      cout << "No graduation honors\n"; }

Let’s say that GPA contains a 3.95. 

  • The first condition will be tested and the result will be true. 
  • Since the result is true then the output will be “Summa Cum Laude.” 
  • The other conditions will NOT be tested – they will be skipped. 

 

Now let’s see what happens if GPA contains a 3.8. 

  • The first condition is tested. 
  • Since 3.8 is not greater or equal to 3.9 then the result of this test is a false. 
  • The second condition is then tested.  The result is now true so the output will be “Magna Cum Laude.” 
  • The remaining conditions will be skipped.

 

What happens if the GPA is 2.0? 

  • The first condition is tested and the result is false. 
  • The second and third conditions are tested in turn – with each test resulting in a value of false.
  • Since all conditions resulted in false, the else is then executed and the output will be “No graduation honors.” 

Switch Structure

The next selection structure that we are going to explore is the Switch Structure.   With the Switch Structure,  a variable is compared to a list of values.  The comparisons are made down the list until a true is found.  Once the result of the comparison is true – no more comparisons occur.

The logic of this structure is similar to the If/Else If/Else structure in that :

  •  Only one comparison can result in true. 
  •  No further comparisons are made once a true is found.

If you set up your Switch Structure such that more than one comparison could be true you will have a logic error which will produce incorrect results.

The syntax for the Switch Structure is as follows:

switch ( selector ) {

      case  label 1 :               statement 1.1 ;               statement 1.2 ;       break;

      case  label 2 :               statement 2.1 ;               statement 2.2 ;        break;

       case  label 3 :

        .

        .

        .

        default:                statement d.1 ;                statement d.2 ;

Things to remember:

  • The  selector  must be an integer (int), character (char), or Boolean (bool) data type.
  • The entire body of the Switch is contained between a set of curly brackets { } .
  • Each  label   must be a unique value.   However, multiple cases may be used on the same line, but they must be separated by colons ( : ).
  • Each case clause ends with a  break  statement.  If you omit the  break  the next case will automatically be executed. 
  • Unlike the If structures, a Switch case does not need its own set of  curly brackets.
  • Just like the If statement has an else clause, the Switch has a default clause.  It is not required, but may be used as the “catch-all.”  The default clause does not end with a  break  statement.

Here is an example of a Switch Structure:

switch (grade) {

     case 'a': case 'A':             cout << "Outstanding" << endl;             qualityPoints = 4.0;             break;

     case 'b': case 'B':             cout << "Good Job" << endl;             qualityPoints = 3.0;             break;

   case 'c': case 'C':             cout << "Passing Grade" << endl;             qualityPoints = 2.0;             break;

   default:             cout << "You did not enter a passing grade" << endl; }

Notice that this example demonstrates the use of multiple cases separated by colons.  Each case has an individual and unique label.

NOTE: ONLY use the break command for the Switch/Case statements.  DO NOT use it for any IF structures or looping structures.

5. Even More Algorithm Practice

 

Steps for Developing an Algorith m

Let’s now go through the steps to develop an algorithm for computing shipping fees - which is the Program 1 assignment.  Here is the problem:

  • Have the Customer input the number of Widgets they wish to purchase.
  • If the Customer input a zero then display a message that states they did not enter a valid number of Widgets.
  • Else, if the Customer input a number of Widgets greater than zero:
  • Display a menu for delivery options:
  • 1.  Ground Delivery
  • 2.  Express Delivery
  • Prompt the Customer for the delivery option.
  • If the Customer selects 1 then the shipping fee is $5.00
  • Else if the Customer selects 2 then display the following:
  • 3. Two Days
  • 4. Overnight
  • If the Customer selects 3 then the shipping fee is $10.00
  • Else if the Customer selects 4 then the shipping fee is $20.00
  • Compute the sub-total of the Widgets.
  • Compute the sales tax of the Widgets at 8%.
  • Compute the total cost of the Widgets.
  • Add the total cost of the Widgets to the shipping fees.
  • Display the receipt.  Include sub-total, sales tax, total cost for the Widgets, the shipping fees, and the total cost of the order.

Be sure to do the following if the Customer does not enter a valid number of Widgets:

  • Do not display anything other than the error message.
  • Do not compute any costs or shipping fees.
  • Do not compute any shipping fees. 

Steps for setting up the program .

 

1. The first step was to create an Initial Algorithm that will outline the basic steps needed to write this program:

How will you solve this problem?

  • You need to know the how many Widgets the customer wants to purchase.  Make sure the number of Widgets is not zero.
  • You need to know the delivery option the customer wants.  Make sure the customer enters a valid delivery option and not zero.
  • You will then charge $2.00 for each Widget.
  • Then compute the sales tax at 8%.
  • Compute the total cost of the Widgets.
  • Add the shipping fee to the total cost of the Widgets.
  • You then display the number of Widgets, the sub-total, the sales tax, the total cost of Widgets, the shipping fee, and the total cost of the order.

Take these statements and convert them to an outline of the basic steps:

  • Get the number of Widgets from the customer and check to make sure it's not zero.
  • If the number of Widgets is not equal to zero then prompt the customer for ground delivery or overnight delivery.
  • If the customer entered a 1 then the shipping fee is $5.00.
  • Else if the customer entered a 2 then prompt the customer for 2 day delivery or overnight.
  • If the customer entered a 3 then the shipping fee is $10.00.
  • Else if the customer entered a 4 then the shipping fee is $20.00
  • Compute the cost of the Widgets, the sales tax, the total cost of Widgets, and the total cost of the order.
  • Display the number of Widgets, the sub-total, the sales tax, the total cost of Widgets, the shipping fee, and the total cost of the order.

2. Next we list the Data Requirements :

Begin with the input - the data you need to "get" in order to solve the problem. We will also list the name we will give each item and the data type.

Input:

Input Dat a

Descriptio n

Variable Nam e

Data Typ e

Number of Widgets

Total number of Widgets the customer purchases.

widgets

int

Delivery Option

The delivery option that the customer wants.

delivery

int

Now we determine the data that we will be displaying. This will be our output.

Output:

Output Dat a

Descriptio n

Variable Nam e

Data Typ e

Number of Widgets

Total number of Widgets the customer purchases.

widgets

int

Cost of Widgets

Sub-total cost

subTotal

double

Sales Tax

Tax for Widgets

salesTax

double

Total Cost of Widgets

Sub-total + tax

totalCost

double

Shipping Fee

Fee for delivery option

shippingFee

double

Total Order

Cost of entire order

orderCost

double

Additional:

Additonal Dat a

Descriptio n

Variable Nam e

Data Typ e

Tax Rate

Tax rate as a decimal

taxRate

double

Ground Rate

Ground delivery option

ground

double

Two Day Rate

Two Day delivery option

twoDay

double

Overnight Rate

Overnight deliver option

overnight

double

Cost of one Widget

The price of one Widget

widgetPrice

double

3. Formulas :

We also list out any formulas we need.

Data Typ e

Resul t

Formul a

double

subTotal

widgets * widgetPrice

double

salesTax

subTotal * taxRate

double

totalCost

subTotal + salesTax

double

orderCost

totalCost + shippingFee

4. Create a Refined Algorithm :

We start with the Initial Algorithm and fill in the details of HOW each step will be executed to create our Refined Algorithm.

  • Prompt the customer for the number of Widgets
  • Scan the number.
  • IF the customer entered a zero then
  • Display an error message
  • ELSE
  • Prompt the customer for ground delivery or overnight delivery.
  • IF the customer entered a 1 then
  • The shipping fee is $5.00.
  • ELSE
  • Prompt the customer for 2 day delivery or overnight.
  • IF the customer entered a 3 then the shipping fee is $10.00.
  • ELSE
  •  The shipping fee is $20.00
  • END IF step i.
  • END IF step b
  • Compute the cost of the Widgets.
  • Subtotal = widgets * widgetPrice
  • Compute the sales tax.
  • salesTax = subTotal * taxRate
  • Compute the total cost of Widgets
  • totalCost = subTotal + salesTax
  • Compute and the total cost of the order.
  • orderCost = totalCost + shippingFee
  • Display the number of Widgets, the sub-total, the sales tax, the total cost of Widgets, the shipping fee, and the total cost of the order.
  • END IF step 2

  • Note the use of indentation in the refined algorithm. 
  • This shows where you would place code within each IF or ELSE section. 
  • If you don’t keep the indented sections within the code for the IF and the ELSE sections then the logic will not be correct.

    

Also notice the section of the algorithm that is indented for step 3. 

None of this should be executed if the customer enters a zero for the number of Widgets. 

That is the purpose of the ELSE in step 3.