Lab 3 2 Examining and Formatting Output

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab 3.

2 Examining and Formatting Output

C++ has various output functions and manipulators you can use to format output. The
floating-point number default output is in scientific notation, which is difficult to read. The
manipulator setprecision formats the number of decimal places of floating-point
numbers according to the number in the argument and requires the header file iomanip.
The manipulator fixed formats floating-point to decimal output rather than scientific
notation. Once set, the fixed-decimal format will apply throughout the program unless
disabled. You can disable the manipulator fixed using the stream member function
unsetf. The manipulator showpoint shows a decimal point and trailing zeros when the
decimal amount is zero. The manipulator setw outputs the value of the next expression in
an output statement in the number of columns specified by the argument and requires the
iomanip header file. If the number of columns specified by the manipulator setw
exceeds the number of columns required, the field width expands to accommodate the size.
Note: The manipulator setw controls the output of only the next expression. The
manipulator flush outputs all data in the buffer without including a newline character.

To get more formatting control, use the fill function or the manipulator setfill to fill
the unused columns with the character used as the argument in the function call. The
setfill manipulator works like the fill function except that it follows the stream
manipulator in the cout statement and requires the iomanip header file. The
manipulators left and right will left-justify or right-justify the output in the column
specified by setw.

Objectives
In this lab, you become acquainted with the output formatting functions and manipulators
and the flush function.
After completing this lab, you will be able to:

• Write programs using the functions and manipulators setprecision, fixed,


showpoint, setw, fill, setfill, left, and right for formatting output.
• Use the flush function to clear the buffer.

Estimated completion time: 50–60 minutes


Lab 3.2 Steps: Examining and Formatting Output
Answer the following questions by circling the correct answer:
1. Given the following statements:
int number;
double value;
cout << “Enter two numeric values: “;
cin >> number >> value;
cout << number << ' ' << value << endl;
if the user enters 8.5 6, which of the following is output?

a. 8 .5 b. 8.5 6 c. there is no output; input failure occurs

2. Given the following statements:


int number = 6;
double value = 4.2
cout << setprecision(3) << number << ' ' << value
<< endl;
which of the following is output?

a. 6 4.200 b. 6.000 4.200 c. 6 4.2

3. Given the following statements:


int number = 6;
double value = 4.2
Cout << showpoint << number << ' ' << value << endl;
which of the following is output?

a. 6 4.2 b. 6.0 4.2 c. 6 4.20

4. Given the following statements:


int number = 6;
double value = 4.2
Cout << fixed << number << ‘ ’ << value << endl;
which of the following is output?

a. 6 4.2 b. 6.00 4.20 c. 6 4.20


5. Given the following statements:
string begin = “abc”, end = “xyz”;
Cout << “1234567890\n”
<< setw(5) << begin << end << endl;
which of the following is output?

a. 1234567890 b. 1234567890 c. 1234567890 d. 1234567890


abc xyz Abc xyz abcxyz abc xyz

6. Given the following statements:


string begin = “abc”, end = “xyz”;
Cout.fill(‘*’);
Cout << “1234567890\n” << setw(5) << begin << end
<< endl;
which of the following is output?

a. 1234567890 b. 1234567890 c. 1234567890 d. 1234567890


abc**xyz abc****xyz **abcxyz **abc**xyz

Circle the correct answer

7. The manipulator setw is used to output the value of an True False


expression in specific columns.
8. Both the flush and setw manipulators position the True False
cursor at the beginning of the next line on the output
device.
9. Once the setw manipulator is used, the column width is True False
set until setw is reset.
10a. Critical Thinking Exercise: (If you completed the program realtor.cpp, which you
stored in the Chap02 folder of your Student Data Files, redesign that program in this
exercise.) Design a program to produce a real estate report. As a real estate agent,
you keep a record of homes you list, and you want to create a report that is easy to
read. The report should also indicate the costs associated with selling the home,
which include the total commission, the listing agency commission, the selling
agency commission, and the selling agent’s commission.

The information needed to sell the home includes the following:

• The last name of the owner of the home.


• The selling price of the home.

The costs are based on the following information:

• Home commissions paid to sell a home through multiple listings are 6%.
• The listing agency receives 3%, and the selling agency receives 3%.
• Of that 3%, each agency gets 1.5%, and the selling agent or listing agent gets
1.5%.

Your data is the sales amount entered as a double number with no fractional
decimal value. Your calculations should consider the following:

• Calculate the amount paid by the homeowner to sell the home and the
amount of the commission you will receive as the selling agent (not agency).
• Use a constant for the commission rate.

Display your output using the following guidelines:

• Display your output in columns using the setw function.


• Your first row of output should be headings. Use setw(15) for each item in
the first row. When writing your string literals, be sure to include a blank at
the end of the string to make sure that one string does not display directly
next to another string.
• The second row should list your variables. For the second row, use a fixed
format and two decimal places, and show the decimal point even when the
fractional decimal value is 0.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
10b. Write a C++ program based on the design you created in Exercise 10a and name it
realtor1.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code
is executed. (Include line numbers as documentation only. Do not use line numbers
when entering your final program.) To fill out the memory table, use one or two lines
for each variable. On one line, enter declaration information. Write the name of the
declared variable, its data type, and the line number at declaration.

Variable Data Value in Memory Line Number at Line Number when


Name Type Declaration Initialized

In the following space, show what is displayed on the screen after executing the
output message.

10c. Enter, compile, link, and execute realtor1.cpp. Then copy the output and save it in a
block comment at the end of your program. Save realtor1.cpp in the Chap03 folder
of your Student Data Files.

The following is a copy of the screen results that might appear after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Please enter owner's last name: Garcia


Please enter the sales price of the home: 100000
Home Owner Price of Home Seller's Cost Agent's Commission
Garcia 100000.00 6000.00 1500.00
10d. The report output shows columns of values that could easily be altered. Change the -
realtor1.cpp program to use the fill function or the manipulator setfill to put the
‘*’ character in the blank spaces before all numbers so that the numbers cannot be
altered. Save the program as realtor2.cpp in the Chap03 folder of your Student Data
Files. Enter, compile, link, and execute realtor2.cpp. Then copy the output and save it
in a block comment at the end of your program.

The following is a copy of the screen results that might appear after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Please enter owner's last name: Garcia


Please enter the sales price of the home: 100000
Home Owner Price of Home Seller's Cost Agent's Commission
Garcia*******100000.00*********6000.00*************1500.00

10e. Although it is easier to read values when they are right-justified, it is difficult to read
columns of strings that are right-justified. Rewrite realtor2.cpp to left-justify the first
field and right-justify all remaining fields using the left and right manipulators.
Additionally, to test the flush function, remove one of your endl manipulators and call
the flush function. Save the revised program as realtor3.cpp in the Chap03 folder of
your Student Data Files. Enter, compile, link, and execute realtor3.cpp. Then copy the
output and save it in a block comment at the end of your program.

The following is a copy of the screen results that might appear after running your
program, depending on the data entered. The input entered by the user is in bold.

Please enter owner's last name: Garcia


Please enter the sales price of the home: 100000
Home Owner Price of Home Seller's Cost Agent's Commission
Garcia***********100000.00*********6000.00*************1500.00

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy