We can even use the operator << to chain the output request as:

Similar documents
I/O Streams and Standard I/O Devices (cont d.)

C++ Input/Output: Streams

Object Oriented Programming Using C++ UNIT-3 I/O Streams

Chapter 3 - Notes Input/Output

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Introduction to C++ (Extensions to C)

UNIT- 3 Introduction to C++

CHAPTER 3 Expressions, Functions, Output

Getting started with C++ (Part 2)

Chapter 21 - C++ Stream Input/Output

Consider the following example where a base class has been derived by other two classes:

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

CS2141 Software Development using C/C++ Stream I/O

BITG 1233: Introduction to C++

CS3157: Advanced Programming. Outline

Chapter 12 - C++ Stream Input/Output

We will exclusively use streams for input and output of data. Intro Programming in C++

Understanding main() function Input/Output Streams

Objects and streams and files CS427: Elements of Software Engineering

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CHAPTER-6 GETTING STARTED WITH C++

Come and join us at WebLyceum

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

File I/O Christian Schumacher, Info1 D-MAVT 2013

Collected By Anonymous

Object Oriented Programming In C++

Streams - Object input and output in C++

For Teacher's Use Only Q No Total Q No Q No

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

OBJECT ORIENTED DESIGN WITH C++ AN INTRODUCTION

CS201 Solved MCQs.

CS242 COMPUTER PROGRAMMING

Chapter 21 - C++ Stream Input/Output

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

WARM UP LESSONS BARE BASICS

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Come and join us at WebLyceum

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Fundamentals of Programming CS-110. Lecture 2

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Unit 4. Input/Output Functions

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Input/Output Streams: Customizing

Lecture 4 Tao Wang 1

Module 11 The C++ I/O System

What we will learn about this week:

Introduction to Programming EC-105. Lecture 2

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

Module C++ I/O System Basics

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

Software Design & Programming I

Programming. C++ Basics

Chapter 1 Introduction to Computers and C++ Programming

Sequential Program Execution

Chapter 3: Input/Output

Engineering Problem Solving with C++, Etter/Ingber

Sample Paper - II Subject Computer Science

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

C++ PROGRAMMING BASICS

Chapter 11 Customizing I/O

TEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010

Fig: iostream class hierarchy

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C++ Input/Output Chapter 4 Topics

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Chapter 3. Numeric Types, Expressions, and Output

CSE 333 Lecture 9 - intro to C++

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Programming with C++ as a Second Language

Chapter 15 - C++ As A "Better C"

Chapter 2: Introduction to C++

COMP322 - Introduction to C++

Chapter 11 Customizing I/O

READ THIS NOW! Do not start the test until instructed to do so!

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

COMP322 - Introduction to C++

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

This watermark does not appear in the registered version - Slide 1

Streams. Rupesh Nasre.

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Transcription:

IO Management Writing to the stream: cout << X; In this statement we have used the operator << as an insertion operator. This operator takes two arguments; left hand operand is cout which is an object of ostream class, and the right-hand side is an operand whose value is to be written/printed on screen. That is the operator << writes the value of the right-hand operand into the left-hand operand. We can even use the operator << to chain the output request as: cout << enter the value << endl; endl is a manipulator that has the effect of newline. Reading from the stream cin >> X; In this statement we use the >> extraction operator the operator takes the istream as its lefthand operand and an object as its right-hand operand. i.e. it reads from the left-hand ( cin ) istream operand and stores the value read into the right-hand operand. >> returns its left hand operand as a result. We can chain the input request to have a combined effect to read two or multiple values as: cin >> x >> y >> z >>.. example: use of insertion and extraction operator int int_var; float float_var; char char_var; cout << enter an integer value << : ; cin >> int_var;

cout << enter a float variable << : ; cin >> float_var; cout << enter a character value << : ; cin >> char_var; cout << You have entered << int_var<< \t <<float_var<< \t <<char_var<<endl; Single character Input and output functions getchar(), and putchar(char) are built in C style of function. To make use of them the header file stdio.h must be included. The getchar function return a single character entered through the standard input device (keyboard). The function take no argument and its syntax is : char mychar; mychar = getchar(); putchar(char): This function is used to display a single character on the standard display device that is the monitor. This function takes the character variable as an argument and returns nothing. The syntax is given below: putchar(mychar); Example of input and output of a single character: #include <iostream.h> #include<stdio.h> Void main() char mychar; cout<< input a character \t:\t ; mychar = getchar()

cout<< \nyou have inputted \t:\t <<mychar; String input and output function We use the gets() and puts(string) function to transfer the string between computer and standard input/output device. We must include the header file stdio.h in the source program to make use of gets() and puts() functions. gets() function need any argument of string or character array, this function read the string from standard input (keyboard) device. The syntax is : char mystrin[20]; gets(mystring); mystring. //this will return a string of size less or equal to 20 characters into Displaying the string: The function puts () is used to display the string on to the standard output device (that is monitor). This function requires an argument of type string. The syntax and usage is: puts(mystring); //will display the string mystring on the display. Example: #include<stdio.h> char mystring[10],myname[10]; cout <<"Input your name :\t"; gets(myname); cout<<"\nyour name is \t:"; puts(myname);

I/O Manipulators Manipulators are used to change certain characteristics of input/output. They change format flag and value of stream. To format the input / output. C++ supports following special functions as i/o manipulators: ENDL SETBASE() Setw(int w) Setfill(char ) Setprecession(int) Ends Flush() Generate carriage return or line feed character Used to change base of one numeric value into another base. The common base converters are: dec, hex, oct Used to set width of a variable Fill the unused field of value Used to control the number of decimal digits after the decimal point Used to insert a null terminating character at the end of the string Used to flush(clear)the output stream int num; cout <<"enter a number "; cin>>num; cout<<"the number \t:"<<num<<" in octal is \t : "<<oct<<num<<endl; // now num is in octal cout<<"the number \t:"<<num<<"in hexa is \t:" <<hex<<num<<endl; // num converted oct to hex

Example: use of setw() for formatting the output int num; cout <<"enter a number "; cin>>num; cout<<"number"<<"\t\t\tnumber"<<endl; cout<<setw(8)<<num<<setw(14)<<oct<<num<<"decimal to octal"<<endl; cout<<setw(8)<<num<<setw(14)<<hex<<num<<"octal to in hexa"<< endl; cout.flush(); Example: use of setfill() for formatting the output int num; cout <<"enter a number "; cin>>num;

cout<<setfill( * )<<endl; cout<<"number"<<"\t\t\tnumber"<<endl; cout<<setw(8)<<num<<setw(14)<<oct<<num<<"decimal to octal"<<endl; cout<<setw(8)<<num<<setw(14)<<hex<<num<<"octal to in hexa"<< endl; cout.flush(); SetPrecision() Used to set the number of digit after the decimal point in floating point number //int b,c; float a,b,c; cout<<"enter two number"; cin>>b>>c; cout<<setprecision(6); a=b/c; cout<<b<<"/"<<c<<"="<<a; getch();