How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 6: User-Defined Functions I
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 2: Basic Elements of C++
Chapter 3: Input/Output
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 2: Basic Elements of C++
Chapter 3: Input/Output
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
Chapter 8 Arrays and Strings
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Chapter 3: Input/Output
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Chapter 2: Basic Elements of C++
Topic 2 Input/Output.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 3: Expressions and Interactivity.
User-Defined Functions
Chapter 2: Basic Elements of C++
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input and Output Chapter 3.
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Chapter 4: Control Structures I (Selection)
Chapter 3: Input/Output
Chapter 3 Input output.
Chapter 6: User-Defined Functions I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 part #3 C++ Input / Output
Chapter 1 c++ structure C++ Input / Output
Programming Fundamental-1
Presentation transcript:

How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM

Objectives 2 In this chapter, you 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 and ignore

Objectives (cont'd.) 3 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

I/O Streams and Standard I/O Devices 4 I/O: sequence of bytes (stream of bytes) from source to destination – Bytes are usually characters, unless program requires other types of information Stream: sequence of characters from source to destination Input stream: sequence of characters from an input device to the computer Output stream: sequence of characters from the computer to an output device

I/O Streams and Standard I/O Devices (cont'd.) 5 Use iostream header file to extract (receive) data from keyboard and send output to the screen  Contains definitions of two data types:  istream : input stream  ostream : output stream  Has two variables:  cin : stands for common input  cout : stands for common output

I/O Streams and Standard I/O Devices (cont'd.) 6 To use cin and cout, the preprocessor directive #include must be used Variable declaration is similar to:  istream cin;  ostream cout; Input stream variables: type istream Output stream variables: type ostream

cin and the Extraction Operator >> 7 The syntax of an input statement using cin and the extraction operator >> is: The extraction operator >> is binary  Left-side operand is an input stream variable  Example: cin  Right-side operand is a variable

cin and the Extraction Operator >> (cont'd.) 8 No difference between a single cin with multiple variables and multiple cin statements with one variable When scanning, >> skips all whitespace – Blanks and certain nonprintable characters >> distinguishes between character 2 and number 2 by the right-side operand of >> – If type char or int (or double ), the 2 is treated as a character or as a number 2

cin and the extraction operator>> 9 Entering a char value into an int or double variable causes serious errors, called input failure

cin and the Extraction Operator >> (cont'd.) 10 When reading data into a char variable  >> skips leading whitespace, finds and stores only the next character  Reading stops after a single character To read data into an int or double variable  >> skips leading whitespace, reads + or - sign (if any), reads the digits (including decimal)  Reading stops on whitespace non-digit character

cin and the Extraction Operator >> (cont'd.) 11

cin and the Extraction Operator >> (cont'd.) 12

cin and the Extraction Operator >> (cont'd.) 13

Using Predefined Functions in a Program 14 Function (subprogram): set of instructions  When activated, it accomplishes a task main executes when a program is run Other functions execute only when called C++ includes a wealth of functions  Predefined functions are organized as a collection of libraries called header files

Using Predefined Functions in a Program 15 Header file may contain several functions To use a predefined function, you need the name of the appropriate header file  You also need to know:  Function name  Number of parameters required  Type of each parameter  What the function is going to do

Using Predefined Functions in a Program 16 To use pow (power), include cmath  Two numeric parameters  Syntax: pow(x,y) = x y  x and y are the arguments or parameters  In pow(2,3), the parameters are 2 and 3

Using Predefined Functions in a Program 17

Using Predefined Functions in a Program 18 Sample Run: Line 1: 2 to the power of 6 = 64 Line 4: 12.5 to the power of 3 = Line 5: Square root of 24 = Line 7: u = Line 9: Length of str = 20

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19 'A' is stored in ch1, character '2' is stored in ch2, 5 is stored in num. However, what if you intended to store 'A' in ch1, the blank in ch2, and 25 in num?

cin and the get Function 20 The get function  Inputs next character (including whitespace)  Stores in memory location indicated by its argument The syntax of cin and the get function: varChar  Is a char variable & Is the argument (parameter) of the function  The variable cin can access the stream function get

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21 To store 'A' in ch1, the blank in ch2, and 25 in num The preceding form of the get function reads values of only the char data type.

cin and the ignore Function 22 ignore function – Discards a portion of the input The syntax to use the function ignore is:  intExp is an integer expression  chExp is a char expression If intExp is a value m, the statement says to ignore the next m characters or all characters until the character specified by chExp which comes first

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23 cin.ignore(100, '\n'); This statement says to ignore the next 100 characters or ignore the input until it encounters the character specified by New Line, whichever comes first cin.ignore(100, 'A'); results in ignoring the first 100 characters or all characters until the character 'A' is found, whichever comes first.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25

The Dot Notation Between I/O Stream Variables and I/O Functions: A Precaution 26 In the statement cin.get(ch); cin and get are two separate identifiers separated by a dot Dot separates the input stream variable name from the member, or function, name In C++, dot is the member access operator

Input Failure 27 Things can go wrong during execution If input data does not match corresponding variables, program may run into problems Trying to read a letter into an int or double variable will result in an input failure If an error occurs when reading data  The input stream then enters a state called the fail state.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28

The clear Function 29 Once in a fail state, all further I/O statements using that stream are ignored The program continues to execute with whatever values are stored in variables  This causes incorrect results The clear function restores input stream to a working state  Here, istreamVar is an input stream variable, such as cin. After using the function clear to return the input stream to a working state, you still need to clear the rest of the garbage from the input stream. This can be accomplished by using the function ignore.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30

Output and Formatting Output 31 Syntax of cout when used with << Expression is evaluated Value is printed Manipulator is used to format the output  Example: endl

Understanding Logic Errors and Debugging with cout statements 32 Syntax errors  Reported by the compiler Logic errors  Typically not caught by the compiler  Spot and correct using cout statements  Temporarily insert an output statement  Correct problem  Remove output statement