Programming Challenge #8: TOTAL PURCHASE | Starting Out With C++ | Chapter 2

Programming Challenge #8: TOTAL PURCHASE | Starting Out With C++ | Chapter 2 comes from the “Review Questions and Exercises” section at the end of Chapter 2 in Tony Gaddis’ book Starting Out With C++ : From Control Structures Through Objects/8th Edition.

>_ The Problem

A customer in a store is purchasing five items. The prices of five items are

Price of item 1 = $15.95
Price of item 2 = $24.95
Price of item 3 = $6.95
Price of item 4 = $12.95
Price of item 5 = $3.95

Write a program that holds the prices of the five items in five variables. Display each item’s price, the subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 7%.

>_ The Solution

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float item1 = 15.95,
          item2 = 24.95,
          item3 = 6.95,
          item4 = 12.95,
          item5 = 3.95,
          subtotal = item1 + item2 + item3 + item4 + item5,
          totalSalesTax = subtotal * .07;

    cout << endl;
    cout << setprecision(2) << fixed;
    cout << "Item 1 = $" << item1 << endl;
    cout << "Item 2 = $" << item2 << endl;
    cout << "Item 3 = $" << item3 << endl;
    cout << "Item 4 = $" << item4 << endl;
    cout << "Item 5 = $" << item5 << endl;
    cout << "Subtotal = $" << subtotal << endl;
    cout << "Sales tax = $" << totalSalesTax << endl;
    cout << "Grand Total = $" << subtotal + totalSalesTax << endl;
    cout << endl;

    return 0;
}

Topics Used:

2.1  The Parts of a C++ Program

CONCEPT: C++ programs have parts and components that serve specific purposes.

2.2  The cout Object

CONCEPT: Use the cout object to display information on the computer’s screen.

2.3  The #include Directive

CONCEPT: The #include directive causes the contents of another file to be inserted into the program.

2.4  Variables and Literals

CONCEPT: Variables represent storage locations in the computer’s memory, while literals are constant values that are assigned to variables.

2.5  Identifiers

CONCEPT: Choose variable names that indicate what the variables are used for.

2.9  Floating-Point Data Types

CONCEPT: Floating-point data types are used to define variables that can hold real
numbers.

2.12 Variable Assignments and Initialization

CONCEPT: An assignment operation assigns, or copies, a value into a variable. When a value is assigned to a variable as part of the variable’s definition, it is called an initialization.

2.13 Scope

CONCEPT: A variable’s scope is the part of the program that has access to the variable.

2.14 Arithmetic Operators

CONCEPT: There are many operators for manipulating numeric values and performing arithmetic operations.

2.16 Named Constants

CONCEPT: Literals may be given names that symbolically represent them in a program.

2.17 Programming Style

CONCEPT: Programming style refers to the way a programmer uses identifiers, spaces, tabs, blank lines, and punctuation characters to visually arrange a program’s source code. These are some, but not all, of the elements of programming style.

That’s it for Programming Challenge #8: TOTAL PURCHASE | Starting Out With C++ | Chapter 2!

Thank you for taking interest in this post. I hope it was helpful for you as much as it helped me along my journey in learning to code!

Also Check Out:

Category: Starting Out With C++ | Chapter 2 | Programming Challenge Solutions

Leave a Reply