I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
C++ Streams Lecture-2.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Chapter 2 Data types, declarations, and displays.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Fundamental File Processing Operations C++
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Student Book Input / Output in C++
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
#include int main() { char *string1=”C++”; char *string2=”Program”; int m=strlen(string1); int n=strlen(string2); for(int i=1;i
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.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Streams & Files in C++:Stream Classes, stream errors, disk file I/O with streams, File Pointers, Error handling in file I/O, File I/O with member functions,
C++ ReviewEECE 3521 EECS 352 Data Structures and Algorithms José M. Vidal Office: 3A47.
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.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Chapter 21 - C++ Stream Input/Output
Formatting Screen Input/Output
Advanced Input and Output
Manipulators CSCE 121 J. Michael Moore
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Managing console i/o operations
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Programming with ANSI C ++
C++ Stream Input/Output
Presentation transcript:

I/O and Program Control Statements Dick Steflik

Overloading C++ provides two types of overloading –function overloading the ability to use the same function name to have multiple definitions –operator overloading the ability to define multiple definitions for any (almost) of the graphical symbols used for operators (+,-,*, >…)

But First... I/O To enable your program to do input and output you must include the IOSTREAM header file –if you are using namespace std #include –if you are not using a namespace #include –or tell the compiler explicitly std::cout << ……...

more on I/O... Once iostream has been included we can use the predefined objects cout, cin and cerr. cout, cin and cerr are predefined objects of class iostream; they are not I/O statements in the sense of program statements, they are objects that we will invoke methods on.

more on I/O... The class iostream allows us to define something called a stream, a stream is nothing more than a sequence of bytes –in the case of cin it is the sequence of bytes coming from sysin (the keyboard) –in the case of cout it is the sequence of bytes being transferred to sysout (the display)

the insertion operator <<... For iostream the “shift left” operator << has been overloaded and is called insertion << is used to insert (write) characters into the stream being used –cout << “Dick Steflik”; inserts “Dick Steflik” into the console output stream –cout << “your grade is : “ << grade;

the extraction operator >>... For iostream the “shift right” operator >> has been overloaded and is called extraction >> is used to extract (read) information from the console input stream (keyboard) and assign it to program variables –int x ; cin >> x; read an integer from the input stream into the variable x

iomanip.h iomanip.h provides a number of stream manipulators that when placed inline with our cin/cout method calles affect the way the commands are handled –setbase(n) will set the base to be used for insertion and extraction to n cout << setbase(8) << n; // will cause n to be printed out as an octal number cout << oct << n; // will accomplish the same thing cout << hex << n; // will print n as a hexadecimal number cout <<dec << n; // will print n as a decimal number –setw or width - will set the field width of subsequent fields cout.setw(10); cout<< n; // n will be printed right justified in a field 10 char wide cout << setw(12) << n; // same thing

Formatting flags setf, unsetf and flags control I/O flag settings ios::skipws - skip white space on input stream ios::left - left justify output in a field, pad to right (if needed) ios::right - right justify in a field, pad on left (if needed) ios::internal - left justify the sign, right justify value ios::dec - treat integers as base 10 ios::oct - treat integers as octal ios::hex - treat integers as hexadecimal ios::showbase - display the base ahead of number (0 for octal, 0x for hex) ios::showpoint - display floating point numbers with a decimal point ios::fixed - display floating point numbers with specified number of decimal digits ios::uppercase - show upper case (ABCDEF) letters in hexadecimal numbers ios::scientific - show floating point numbers in scientific notation

Using Formatting flags The flags member function must specify a value for each formatting flag (seldom used) The setf member function and the stream manipulator setiosflags are used to set one or more formating flags; likewise unsetf and resetioflags will reset one or more formatting flags. –cout << setf( ios::hex | ios::uppercase) << x; displays x in hexadecinal format with uppercase letters for letters

Deitel & Deitel Review Chapter 11 of the Deitel & Deitel C++ How to Program book for a good discussion on Stream I/O