Lab 3 2 Examining and Formatting Output
Lab 3 2 Examining and Formatting Output
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:
• 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.
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.
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.
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.
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.
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: