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.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
C++ fundamentals.
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.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
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 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
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.
Unit -1 Objectives: Exposure to basics of object oriented mode, C++ programming and I/O in C++ INTRODUCTION: Differences Between C and C++, The Object.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
CS212: Object Oriented Analysis and Design
C Formatted Input/Output
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Introduction to C++.
Basic Input and Output Operations
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Introduction to C++ Programming
Advanced Input and Output
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Introduction to cout / cin
Managing console i/o operations
Chapter 3 Input output.
Introduction to C++ Programming
Chapter 4 INPUT AND OUTPUT OBJECTS
File I/O in C++ I.
Chapter 2 part #3 C++ Input / Output
C++ Programming Basics
Input/Output Streams, Part 1
File I/O in C++ I.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Programming Fundamental-1
Presentation transcript:

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 how to present the result in a desire form We used cin and cout function with the operators >> and << for the input and output operations But we have to know how to control the output is printed C++ supports a rich set of I/O output functions to do this C ++ uses the concept of Stream and Stream classes to implements its I/O output operations with console and disk files

C ++ Stream The I/O system in C++ is designed to work with a wide variety of devices including terminals , disks and tape derives Although each device is very different , the I/O system in c++ provide an interface to programmers . This interface is known as Stream.

A stream is a sequence of bytes A stream is a sequence of bytes . Its acts as a source from which the data can be obtained or as a destination to which the output data can be sent. The source stream that provides the data to the program is called “input stream” . The destination stream that receives output from the program is called “ Output Stream” The data in the “input stream “ can come from the keyboard or any other storage devices The data in the “output stream” can go to the screen or to different storage devices. A stream act as an interface between the program and input / output devices

C ++ contains several pre-define streams that are automatically opened when a program begins its executions These include CIN and COUT which have been used very often.

C ++ Stream Classes IOS ( General Input/output Stream class) : Contains basic facilities that are used by all other input and output classes. Declare constant and function that are necessary for handling formatted input and output operations Istream ( input stream) : Inherits the properties of IOS. Declares input function such as get() , getline() and read(). Contains overloaded extraction operator >>

OStream ( output stream ) : Inherits the properties of IOS. Declares output functions put() and write(). Contains overloaded insertion operator << Iostream ( Input/output stream ) : Inherits the properties of IOS , istream and ostream through multiple inheritance . So contains all input and output functions

Unformatted I/O Operations We have used the objects cin and cout ( pre defined in the iostream file) for the input and out of various data type. This has been made possible by overloading the operator >> and << to recognize the all basic type of c++. The >> operator is overloaded in the istream calss and << is overloaded in the ostream class.

Genral format for reading data from the keyboard. cin >> variable1 >> variable 2 ……. >> variableN Variable1 , varable2 are the valid c++ variables names that have been declared already This statement will cause the computer to stop the execution and look for input data from the keyboard The >> operator reads the data character by character and assigns it to the indicated location

The general form for display the data on the screen The reading for a variable will be terminated at the encounter of a white space or a character that does not match the destination type. for example int code ; cin >> code ; If the following data is given 4258D ( The operator will read the character upto 8 and the value 4258 is assigned to code . The character D will remains in the input stream and will be input to the next CIN statement The general form for display the data on the screen Cout << item1 << item 2 <<…….. << itemN

Function put() and get() The classes istream and ostream define two member function get() and put() to handle the single character input and output operations void main() { int count =0; char c; cout<< “Input text:”; cin.get(c); while ( c !=‘\n’) { cout.put(c); cout ++; } Cout << “Number of character= “<< cout; }

Getline() and write function() We can read and write a line of text more efficiently using the line oriented input / output functions getline() and write().. The getline() reads a whole line of text and ends with a new line character (enter key). The function can be invoked by using the object cin as follows Cin.getline(line,size) char name[20]; cin.getline(name,20);

If we enter “ object oriented programming” ( press enter); in the above case only 19 character will be saved and white space are also taken into account. Cin can read strings that do not contain the white spaces . So using cin we can store only a one word string After reading the string ,cin automatically adds the terminating null character to the character array.

Int main() Example of Getline () function { int size =20; char city[20]; Cout << “enter the city name :->”; Cin>> city; Cout << “City name :-> “<<city; Cout <<“ Enter the city name again:->”; Cin.getline(city,size); Cout <<“ city name now :->” << city; Cout << “Enter the city name again:->”; Cout<<“ New city name :->”<< city; }

Formatted Console I/O Operations C++ supports a number of features that could be used for formatting the output. IOS class functions and flags Manipulators User define output function.

Ios class functions and flags In C++ IOS class contains a large number of member function to format the output Width() To specify the required field size for displaying an output value. Precision() To specify the number of digits to be displayed after the decimal point of a float value. Fill() To specify a character that is used to fill the unused portion of a field. Setf() To specify format flag that can control the form of output such as left justification and right justification

Width() function We can use the width() function to define the width of a field necessary for the out put of an item It is member function Cout.width(w)  here w is the field width( number of columns) the output will be printed in a field of w characters wide at the right side of the field. Can specify the field width of one item which flows immediately . Cout.width(5); Cout << 543 << 12 <<“\n” will print following output:->

The value 543 is printed right justified in the first five column. The specification width(5) does not retains the setting for printing number 12 . This can improved as following Cout.width(5); Cout<< “543”; Cout<< “12”<<“\n”;

int value= item[i] * cost[i]; Cout.width(15); #include<iostream.h> void main() { int item[4]={10,8,12,15}; int cost[4]={75,100,60,90}; cout.width(5); cout<< “Items”; cout.width(8); cout<< “cost”; cout.width(15); cout<<“ Total Value:->”<<“\n”; Int sum; For( int i=0;i<4; i++) cout.item[i]; cout<< cost[i]; int value= item[i] * cost[i]; Cout.width(15); Cout << value <<“ \n”; Sum =sum + value; } Cout <<“\n Grand total :->”; Cout.width(2); Cout << sum << “\n”;

Precision () By default the floating numbers are printed with six digits after the decimal point. We can fix the number of digits using the precision function.