Programming Languages Chapter 3 Programming From Problem Analysis Program Design Eighth Edition Inputoutput Guide This

subject Type Homework Help
subject Pages 9
subject Words 1329
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-1
Chapter 3
Input/Output
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings, you will find lecture notes that summarize the section, Teacher
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
Instructor’s Manual Table of Contents
Overview
Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
page-pf2
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-2
Lecture Notes
Overview
Chapter 3 explores I/O operations in more detail. Students will learn how I/O statements
extract input from the standard input device and send output to the standard output
device. They will become more familiar with using predefined functions and learn
about several manipulators that are used to format output. Finally, students will enhance
their knowledge of I/O by extending the operations associated with standard
input/output devices to files.
Objectives
In this chapter, the student will:
Learn what a stream is and examine input and output streams
Explore how to read data from the standard input device
Learn how to use predefined functions in a program
Explore how to use the input stream functions get, ignore, putback, and peek
Become familiar with input failure
Learn how to write data to the standard output device
Discover how to use manipulators in a program to format output
Learn how to perform input and output operations with the string data type
Learn how to debug logic errors
Become familiar with file input and output
Teaching Tips
I/O Streams and Standard I/O Devices
1. Review the three major operations a program performs: get data, manipulate data, and
3. Introduce the concept of an I/O stream as a sequence of bytes from a source to a
destination. Define the terms input and output stream, and the terms common input and
common output.
Teaching
Tip
You might set aside some extra time to discuss streams, as they may be a new
concept for some students. Draw an analogy to how traffic travels across a
network as a sequence of bytes.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-3
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
cin and the Extraction Operator >>
1. Explain how the extraction operator works. Point out that it is a binary operator in
6. Step through Examples 3-1 through 3-3 with your students to illustrate various ways to
use cin and the extraction operator.
Teaching
Tip
The extraction operator is probably fascinating to your students because of its
multifaceted functionality. Use this opportunity to briefly introduce the concept
of operator overloading.
Using Predefined Functions in a Program
1. Discuss the role of functions in a C++ program. Explain that the main function in C++
executes automatically when a program is run, but other functions are activated when
called. Define the terms function call and arguments/parameters.
3. Describe some of the functions available in the cmath and string header files. Use
Example 3-4 to illustrate how to use these functions in a program.
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-4
Teaching
Tip
The following sections introduce several functions that are useful for I/O
operations. However, the syntax for calling functions may be new for students.
Before beginning this section, briefly explain how to access functions using the
dot operator and how to indicate function arguments in parentheses. You might
want to refer to the material in the last section of this heading, and then review
those concepts again later. This is also a good opportunity to introduce object-
oriented concepts by mentioning that istream and ostream are actually
objects with member functions.
cin and the get Function
cin and the ignore Function
1. Explain how the ignore function is useful when processing partial data.
The putback and peek Functions
1. Describe the purpose and syntax of the putback function.
The Dot Notation Between I/O Stream Variables and I/O Functions: A Precaution
Quick Quiz 1
1. An expression such as pow(2,3) is called a(n) ____________________.
2. Which header file contains the sqrt function?
3. What is the syntax of the ignore function?
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-5
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Answer: cin.ignore(intExp, chExp);
4. The get function presented in this chapter only reads values of type
____________________.
Input Failure
1. Explain that input failure occurs when the input data does not match the corresponding
The clear Function
1. Describe the purpose of the clear function. Use Example 3-9 to demonstrate how the
clear function can be used with the ignore function to restore an input stream from
failure.
Teaching
Tip
Point out that it is important to write the program so that it can recover from as
many potential errors as possible.
Output and Formatting Output
1. This section describes how to format output, particularly floating-point numbers.
setprecision Manipulator
1. Describe how to use setprecision to format the number of decimal places in
fixed Manipulator
1. Explain the purpose of the fixed and scientific manipulators. Use Example 3-10
showpoint Manipulator
page-pf6
1. Explain how showpoint can be used with fixed to force the display of a decimal
C++14 Digit Separator
1. C++14 introduces a digit separator character (single-quote character). This allows you
setw
Quick Quiz 2
1. Write a statement to format the output of decimal numbers to two decimal places.
2. To force the output to show the decimal point and trailing zeros of a decimal number,
use the ____________________ manipulator.
3. True or False: The setw manipulator cannot use a string as an argument.
4. The header file ____________________ needs to be included to use the setw function.
Additional Output Formatting Tools
1. This section describes additional formatting tools to give the programmer more control
over output.
setfill Manipulator
1. Explain that setfill is used with setw to fill unused columns with a specified
page-pf7
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-7
left and right Manipulators
Input/Output and the string Type
1. Explain that cin can be used to read a string. However, emphasize that getline
should be used instead if you would like to read a string containing blanks. Go over the
syntax of the getline function.
Teaching
Tip
Discuss specific situations in which using getline is preferable to cin, and
vice versa.
Debugging: Understanding Logic Errors and Debugging with cout
Statements
1. Explain how the cout statement can be used to help find and remove logic errors from
File Input/Output
2. Explain how the fstream header file provides file I/O through the ifstream and
3. Describe the five-step process of file I/O. Explain how to use ifstream and
4. Step through the movie and student grade Programming Examples at the end of the
page-pf8
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-8
Teaching
Tip
It might be helpful to show students how to create a simple data file and discuss
appropriate file extensions. Test the input data file in class with a short program.
Quick Quiz 3
1. Define a file.
2. True or False: File stream variables are predefined in the fstream header file and
associated with input/output sources.
3. The stream member function ____________________ is used to open files.
4. Write a statement that closes the file stream outData.
Class Discussion Topics
1. Why do several functions in iostream and fstream have identical names? When is
it advantageous to reuse function names?
2. Some of the input/output functions employ syntax that is difficult to remember. Ask
Additional Projects
1. Ask students to write a program that asks the user for their name, favorite movie,
2. Ask students to write a program that calculates the body mass index of the user. After
outputting the BMI, the program should display possible BMI ranges and their meaning.
page-pf9
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 3-9
Additional Resources
Key Terms
used for input and output
Function call: an expression that transfers control from the main function to the first
statement in the body of the function, such as pow(2,3)
Input failure: a situation in which a program either fails to compile, or yields incorrect
results, because the input data does not match the corresponding variables in the

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.