These statements are referred to as the "magic formula" because they for C++ to output statements in a "standard" format. Note what each statement accomplishes. For more information, see section: 4.1.3: Decimal Formatting.

Compile and run your program again and verify the output looks like:

Enter the product name: Boeing_777
Price of the Boeing__777: 277345678
Total price: $277345678.00

Let us add a constant that we will use later in our program. Enter the following code after the magic formula and before the statement that prints the total price:

const int PERCENT = 100;

A constant variable (or constant) is a variable that cannot change after being assigned a value. Using a constant lets us avoid using a vague number. For more information, see section: 4.1.4: Constants and Magic Numbers.

Now we will add sales tax to the price of the product. Enter the following code after the constant and before the statement that prints the total price:

Add the series of if statements shown below into grader.cpp as shown below:

We are nesting if statements in the else clause. Nesting in the else clause makes each test condition of the if statement exclusive of the others because each test condition eliminates all the preceding conditions. Thus, in this scenario the order is important. For more information see section 4.2.3: When Order Matters.

Compile and run your modified program to make sure you made the changes correctly. When you run the program, the output should look like:

Enter a score: 80
B

Run your program a few times with different scores to verify that any score displays the correct letter grade.

Submit your final program source code to Canvas as part of assignment 4.

In this exercise we program multiple levels of decisions into a program. As an example, we will calculate an insurance premium according to the following table. As usual, verify your code by compiling after each step.

Marital Status

Age

Premium

Single

Under 21

1500

Single

21 to 29

1200

Single

Over 29

1000

Married

Under 21

1200

Married

21 to 29

1000

Married

Over 29

800

Specifications

Copy the following program into a text editor, save it as insurance.cpp, and then compile and run the starter program to make sure you copied it correctly.

In this exercise we test multiple alternatives in a program. As an example, we will calculate a simple guide of what to eat when you are hungry depending on the temperature. Verify your code by compiling after each step, as usual.

Specifications

Copy the following program into a text editor, save it as foodguide.cpp, and then compile and run the starter program to make sure you copied it correctly.

True or false? Order never matters in a sequence of if and else statements. (4.2.3)

In a game program, the scores of player A and B are stored in variables scoreA and scoreB. Assuming that the player with a larger score wins, write a sequence of conditional statements that prints our "A won", "B won" or "Game tied". (4.2.3)

What is the difference between if-else and nested if statements? (4.2.3 vs. 4.2.4

True or false? You can nest if statements within another if statement. (4.2.4)

Add the following code after the statement int guess = 0; and before the cout statement:

char repeat = 'y';

This is the initialization code that we will use for the test condition that comes next.

We want to repeat all the rest of the code in our program. For this we need to add a while statement such as:

while ('y' == repeat) {
// Place the rest of the code after the initialization
// and before the return 0 between these curly braces.
}

Statements inside the curly braces repeat while the test condition in the parenthesis, ('y' == repeat), evaluates to true. For more information, see section: 4.3.2: Coding while Statements.

Inside the while loop we need some way to change the test condition. We change the test condition by letting the user enter a value for the repeat variable by adding the following code at the end of the loop just before the closing curly brace:

cout << "\nDo you want to play again? (y/n) ";
cin >> repeat;

Without these two statements our loop would have no way to exit. A loop with no way to exit is known as an infinite loop. For more information, see section: 4.3.3: Understanding the while Loop.

Formatting a loop is important. Indent all the code within the curly braces of the while loop. For more information, see Formatting the Code in the section: 4.3.3: Understanding the while Loop.

As a final part of our program, we add the infamous phrase: "Game Over". Add the following statement after the closing curly brace of the while loop:

cout << "Game over\n";

Compile and run your program again and verify the output looks like:

I'm thinking of a number between 1 and 10.
Can you guess it?
Enter your guess: 3
Sorry, that is not correct.
Try again.
Do you want to play again? (y/n) y
I'm thinking of a number between 1 and 10.
Can you guess it?
Enter your guess: 7
*** Correct! ***
Do you want to play again? (y/n) n
Game over

Submit your program source code to Canvas as part of assignment 4.

Completed Program

When finished, your application should look like the following. Note especially the extra indentation within the curly braces of the while loop.

As time permits, read the following sections and be prepared to answer the Check Yourself questions in the section: 4.3.5: Summary.