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:

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
CPS120: Introduction to Computer Science INPUT/OUTPUT.
I/O Streams as an Introduction to Objects and Classes
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
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.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
C++ Vs. Java Who will win?. Look Ma, no classes! C++ was conceived as an object-oriented extension to C C was (is) purely procedural language –Functions.
© 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.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Streams Lecture-2.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
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.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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 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.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
CS221 C++ Basics. C++ Data Types structured array struct union class address pointer reference simple integral char short int long bool floating float.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
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
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
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.
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Introduction to cout / cin
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 ++
Formatted Input, Output & File Input, Output
Presentation transcript:

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: formatting whitespace structured inputs - getting data into the correct internal form However, do not fall into the beginner’s traps: brilliant I/O with incorrect functionality thinking functionality is wrong because I/O is too complicated to get right forgetting that random tests are often better than user dependent I/O

Scientific Notation #include Using namespace std; main() { float z = ; cout << z << endl; }

Outline Simple input/output (iostream) cin and cout output insertion operator (<<) extraction operator (>>) Advanced input/output object flags (setf, unsetf) input status bits manipulators (iomanip.h) file input/output (fstream.h) opening/closing files

Setting Format Flags The object cout has flags that determine how objects are printed. To change how things are printed we access and change these flags To set a flag(s) we use the setf() function which is associated with objects such as cout and cin To call setf() we say cout.setf(flags) –the setf function is a field of the object cout –Q: But what flags? A: C++ predefines them

Setting Format Flags (cont) But in order to be able to set flags we often have to unset other flags first, to do so we use the unsetf() function: cout.unsetf(flags) C++ also provides a short-hand to combine both operations: cout.setf(OnFlags,OffFlags) –First turns off the flags OffFlags –Then turns on the flags OnFlags

Explicit precision manipulation // Example 10 #include Using namespace std; int main ( void){ float myNumber = ; cout.setf ( ios::fixed, ios::floatfield );// decimal format cout.setf ( ios::showpoint ) ; // print decimal point //ios is c++ standard library reference cout << "Number is " << setprecision ( 3 ) << myNumber << endl ; return 0 ; } 7 Number is Output:

setw(n) requires #include and appears in an expression using insertion operator (<<) affects only the very next item displayed “set width” specifies n as the number of total columns to display a number. The number of columns used is expanded if n is too narrow. Useful to align columns of output

Setw() Example //Example 11 #include Using namespace std; int main ( void) { float myNumber = ; float yourNumber = ; cout.setf ( ios::fixed, ios::floatfield ) ; cout.setf ( ios::showpoint ) ; cout << "Numbers are: " << setprecision (4) << endl << setw ( 10 ) << myNumber << endl << setw ( 10 ) << yourNumber << endl ; return 0 ; } 9 Numbers are: Output:

Whitespace Characters Include... blanks tabs end-of-line (newline) characters The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or “\n” in a program.

Another way to read char data The get( ) function can be used to read a single character. It obtains the very next character from the input stream without skipping any leading white space characters. 11

char first ; char middle ; char last ; cin.get ( first ) ; cin.get ( middle ) ; cin.get ( last ) ; NOTE: The file reading marker is left pointing to the space after the ‘B’ in the input stream. firstmiddlelast At keyboard you type: A [ space]B [ space ] C [ Enter] firstmiddlelast ‘A’‘ ’‘B’ 12

Example program – I/O Formatting #include int main() { int n; float f; double d; char s[100]; cin >> n; // input an integer cout << n << endl; // print an integer, no formatting cout << setw(6) << n << endl; // print an integer, padded on left with spaces to total 6 chars cout << setw(-6) << n << endl; // print an integer, padded on right with spaces to total 6 chars cin >> s; // input a string (whitespace delineated) cout << s << endl; // print a string, no formatting cout << setw(20) << s << endl; // print a string, padded with spaces on left to 20 chars cout << setiosflags(ios::left) << setw(20) << s << endl; // print a string, padded with spaces on right to 20 chars cin >> f; // input a single precision floating point number cout << setiosflags(ios::fixed) << f << endl; // print a float, default precision is 6 places cin >> d; // input a double precision floating point number cout << d << endl; // print a double, default precision is 6 places cout << setprecision(2) << d << endl; // print a double, 2 places of precision cout << setw(10) << setprecision(2) << d << endl; // print a double, 2 places of precision, padded with space to 10 }