LAB 3.1 WORKING WITH THE cin STATEMENT

 

Copy and paste the following program into Visual Studio IDE.

 

 

// This program will read in the quantity of a particular item and its price.

// It will then print out the total price.

// The input will come from the keyboard and the output will go to

// the screen.

 

#include <iostream>

using namespace std;

 

int main()

{

    int quantity;          // contains the amount of items purchased

    float itemPrice;       // contains the price of each item

    float totalBill;       // contains the total bill.

 

    cout << "Please input the number of items bought" << endl;

 

    // Fill in the input statement below to bring in the quantity.

 

    // Fill in the output statement below to print a prompt to ask for the price.

 

    // Fill in the input statement below to bring in the price of each item.

 

    // Fill in the assignment statement below to determine the total bill.

 

    // Fill in the output statement below to print the total bill, with an appropriate message to the screen.

 

    return 0;

}

 

 

Exercise 1: Complete the program so that a sample run inputting 22 for the number of items bought and $10.98 for the price of each item will produce the results below.

 

Sample run of the program.

 

Please input the number of items bought

22

Please input the price of each item

10.98

The total bill is $241.56

 

 

Exercise 2: Now alter the program (use function getline ) so that the program first asks for the name of the product (which can be read into a string variable), so that the following sample run of the program will appear.

 

Please input the name of the item

Chocolate Ice Cream

Please input the number of items bought

4

Please input the price of each item

1.97

The item that you bought is Chocolate Ice Cream

The total bill is $7.88

 

(Print out the program and the running result for Exercise 2, and hand them in with the rest of the lab.)

 

 

LAB 3.2 FORMATTING OUTPUT

 

Look at the following table:

PRICE

QUANTITY

1.95

8

10.89

9

 

Assume that from the left margin, the price takes up fifteen spaces. We could say that the numbers are right justified in a 15-width space. Starting where the price ends, the next field (quantity) takes up twelve spaces. We can use the statement setw(n) where n is some integer to indicate the width to produce such tables.

 

Copy and paste the following program into Visual Studio IDE.

 

 

// This program will bring in two prices and two quantities of items

// from the keyboard and print those numbers in a formatted chart.

 

#include <iostream>

#include ______________    // Fill in the code to bring in the library for formatted output.

 

using namespace std;

 

int main()

{

    float price1, price2;                // The price of 2 items

    int   quantity1, quantity2;          // The quantity of 2 items

 

    cout << setprecision(2) << fixed << showpoint;

    cout << "Please input the price and quantity of the first item" << endl;

 

// Fill in the input statement below that reads in price1 and quantity1 from the keyboard.

 

    // Fill in the output statement below that prints a prompt for the second price and quantity.

 

    // Fill in the input statement below that reads in price2 and quantity2 from the keyboard.

 

    cout << setw(15) << "PRICE" << setw(12) << "QUANTITY" << endl;

 

// Fill in the output statement below that prints the first price and quantity.

// Be sure to use setw() statements.

 

    // Fill in the output statement below that prints the second price and quantity.

 

    return 0;

}

 

Exercise 1: Finish the code above by filling in the blanks and the instructions necessary to execute the following sample run. Note that two or more data items can be input at one time by having at least one blank space between them before hitting the enter key.

 

Please input the price and quantity of the first item

1.95  8

Please input the price and quantity of the second item

10.89  9

      PRICE           QUANTITY

       1.95                  8

      10.89                  9

 

(Print out the program and the running result for Exercise 1, and hand them in with Lab 3.1)