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.

Slides:



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

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data types and variables
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
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
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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Data Types, Declarations, and Displays.
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.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
C++ Streams Lecture-2.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
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.
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.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 2 part #1 C++ Program Structure
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 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 2 C++ self study 2nd Semester
Chapter 3: Input/Output
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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.
Bill Tucker Austin Community College COSC 1315
C Formatted Input/Output
Chapter 1.2 Introduction to C++ Programming
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
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
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Introduction to C++ Programming
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 3: Input/Output
Chapter 3 Input output.
Chapter 2 part #3 C++ Input / Output
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

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

Outline  Input / Output Operations  Using iostream  Output Stream  Input Stream  Common Programming Errors 2 2/17/2012Fatimah Alakeel

Input/Output Operations  Input operation  an instruction that copies data from an input device into memory.  Input Stream: A stream ( numbers, characters, strings..etc) that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory.  Output operation  an instruction that displays information stored in memory to the output devices (such as the monitor)  Output Stream: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection) 3 2/17/2012Fatimah Alakeel

Using iostream  The C++ iostream library provides hundreds of I/O capabilities.  Standard iostream objects: cout - object providing a connection to the monitor cin - object providing a connection to the keyboard  To perform input and output we send messages to one of these objects 2/17/ Fatimah Alakeel

Output Stream 5 2/17/2012Fatimah Alakeel

The Insertion Operator (<<)  To send output to the screen we use the insertion operator on the object cout  Format: cout << Expression;  The compiler figures out the type of the object and prints it out appropriately cout << 5; // Outputs 5 cout << 4.1; // Outputs 4.1 cout << “String”; // Outputs String cout << ‘\n’; // Outputs a newline 2/17/ Fatimah Alakeel

Stream-Insertion Operator  << is overloaded to output built-in types ( ex. int, float,…etc)  Can also be used to output user-defined types (i.e. user defined classes).  cout << ‘\n’; Prints newline character  cout << endl; endl is a stream manipulator that issues a newline character and flushes the output buffer  cout << flush; flush flushes the output buffer 2/17/ Fatimah Alakeel a buffer is just a pre-allocated area of memory where you store your data while you're processing it.

Cascading Stream-Insertion Operators  << : Associates from left to right, and returns a reference to its left-operand object (i.e. cout ).  This enables cascading cout << "How" << " are" << " you?"; Make sure to use parenthesis: cout << "1 + 2 = " << (1 + 2); NOT cout << "1 + 2 = " << 1 + 2; 2/17/ Fatimah Alakeel

Printing Variables 9  cout << someVariable; cout knows the type of data to output  Must not confuse printing text with printing variables:  int x =12;  cout << x; // prints 12  cout << “x”; // prints x 2/17/2012Fatimah Alakeel

Formatting Stream Output  Performs formatted and unformatted output I. Output of numbers in decimal, octal and hexadecimal using manipulators. II. Display numbers on different width, filling spaces with characters III. Varying precision for floating points IV. Formatted text outputs 2/17/ Fatimah Alakeel

I. Manipulators  C++ manipulators  C++ provides various stream manipulators that perform formatting tasks.  Manipulators are functions specifically designed to be used in conjunction with the insertion ( >) operators on stream objects.  must include iomanip to use  several are provided to do useful things  you can also create your own 2/17/ Fatimah Alakeel

Output Manipulators (no args) Manipulators included like arguments in extraction endl - outputs a new line character, flushes output dec - sets int output to decimal hex - sets int output to hexadecimal oct - sets int output to octal Example: #include using namespace std; int x = 20; cout << x<<“ in hexadecimal is: “<<hex << x << endl; cout << x<<“ in octal is: ”<< oct << x << endl; cout << x<<“ in decimal is: ”<<dec << x << endl; 2/17/ Fatimah Alakeel

II. Setting the Width  You can use the width(int) function to set the width for printing a value, but it only works for the next insertion command (more on this later): int x = 42; cout.width(5); cout << x << ‘\n’; // Outputs 42 OR cout << setw (10); cout << 77 << endl; // prints 77 on 10 places 2/17/ Fatimah Alakeel

II. Setting the Fill Character Use the fill(char) function to set the fill character. The character remains as the fill character until set again. int x = 42; cout.width(5); cout.fill(‘*’); cout << x << ‘\n’; // Outputs ***42 OR cout << setfill ('x') << setw (10); cout << 77 << endl; // prints xxxxxxxx77 2/17/ Fatimah Alakeel

III. Significant Digits in Float Use function precision(int) to set the number of significant digits (the number of digits to the right of the decimal point) printed (when the float number is in the fixed point format): A call to this function sets the precision for all subsequent output operations until the next precision. float y = 12.32; cout<<fixed; //Specify that the value is in fixed-point notation cout.precision(1); cout << y << '\n'; // Outputs 12.3 cout.precision(2); cout << y << '\n'; // Outputs cout.precision(3); cout << y << '\n'; // Outputs /17/ Fatimah Alakeel

III. Significant Digits in Float If the fixed point format is not specified, the precision field specifies the maximum number of digits to be displayed in total counting both those before and those after the decimal point. (may convert from fixed to scientific to print): float y = ; cout.precision(1); cout << y << '\n'; // Outputs 2e+01 cout.precision(2); cout << y << '\n'; // Outputs 23 cout.precision(3); cout << y << '\n'; // Outputs /17/ Fatimah Alakeel

Using showpoint/noshowpoint 2/17/2012Fatimah Alakeel 17  showpoint specify that floating-point numbers (even for whole numbers) should be output with a decimal point, even if they’re zeros. Following the decimal point, as many digits as necessary are written to match the precision.  This setting is reset with stream manipulator noshowpoint.  When the showpoint manipulator is not set, the decimal point is only written for non-whole numbers.

Using showpoint/noshowpoint #include using namespace std; int main () { double a, b, pi; a=30.0; b= ; pi= ; cout.precision (5); cout << showpoint << a << '\t' << b << '\t' << pi << endl; cout << noshowpoint << a << '\t' << b << '\t' << pi << endl; return 0; } 2/17/ Fatimah Alakeel

IV. Formatting Text 19  To print text you need to include “” around the text  Cout <<“This is a Beautiful Day” ;  You can add escape sequence for further options. 2/17/2012Fatimah Alakeel

Escape Sequence 20 2/17/2012Fatimah Alakeel

Examples cout<<"Please enter the student's grades:”; Please enter the student's grades: cout<<"The class average is “<< average; The class average is 95.5 cout<<“The total area is “<< area<< “and the total cost is “<< cost << “ S.R.”; The total area is 60.2 and the total cost is 4530 S.R. Cout<<"The student received an ”<< grade << “ grade in the course."; The student received an A grade in the course. 21 2/17/2012Fatimah Alakeel Average = 95.5 area = 60.2 cost = 4530 grade = ‘A’

Examples (Con.) Cout<<”The grade is << grade << gradesymb; The grade is A+ Cout<<"I am the first line\n”; Cout<<“\n I am the second line\n"; I am the first line I am the second line 22 2/17/2012Fatimah Alakeel grade = ‘A’ gradesymb = ‘+’

Input Stream 23 2/17/2012Fatimah Alakeel

The Extraction Operator (>>)  To get input from the keyboard we use the extraction operator and the object cin  Format: cin >> Variable;  The compiler figures out the type of the variable and reads in the appropriate type int X; float Y; cin >> X; // Reads in an integer cin >> Y; // Reads in a float 2/17/ Fatimah Alakeel

Syntax 25 cin >> someVariable ; cin knows what type of data is to be assigned to someVariable (based on the type of someVariable ). 2/17/2012Fatimah Alakeel

Stream Input  >> (stream-extraction)  Used to perform stream input  Normally ignores whitespaces (spaces, tabs, newlines) in the input stream.  Returns zero ( false ) when EOF is encountered, otherwise returns reference to the object from which it was invoked (i.e. cin ) This enables cascaded input cin >> x >> y; 2/17/ Fatimah Alakeel

Stream Input  cin inputs ints, chars, null-terminated strings, string objects  but terminates when encounters space (ASCII character 32) ‏  workaround? use the “get” method [ will see that later] 2/17/ Fatimah Alakeel

Chaining Calls  Multiple uses of the insertion and extraction operator can be chained together: cout << E1 << E2 << E3 << … ; cin >> V1 >> V2 >> V3 >> …;  Equivalent to performing the set of insertion or extraction operators one at a time  Example cout << “Total sales are $” << sales << ‘\n’; cin >> Sales1 >> Sales2 >> Sales3; 2/17/ Fatimah Alakeel

Extraction/Insertion Example cout << “Hello world! ”; int i=5; cout << “The value of i is “ << i << endl;OUTPUT: Hello World! The value of i is 5 //endl puts a new line Char letter; cout << “Please enter the first letter of your name: “; cin >> letter; cout<< “Your name starts with “ << letter;OUTPUT: Please enter the first letter of your name: F Your name starts with F 2/17/ Fatimah Alakeel

Common Programming Errors 30 2/17/2012Fatimah Alakeel

Common Programming Errors  Debugging  Process removing errors from a program  Three (3) kinds of errors :  Syntax Error a violation of the C++ grammar rules, detected during program translation (compilation). statement cannot be translated and program cannot be executed 31 2/17/2012Fatimah Alakeel

Common Programming Errors cont…  Run-time errors An attempt to perform an invalid operation, detected during program execution. Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected 32 2/17/2012Fatimah Alakeel

Common Programming Errors cont…  Logic Error/Design Error An error caused by following an incorrect algorithm Very difficult to detect - it does not cause run-time error and does not display message errors. The only sign of logic error – incorrect program output Can be detected by testing the program thoroughly, comparing its output to calculated results To prevent – carefully desk checking the algorithm and written program before you actually type it 33 2/17/2012Fatimah Alakeel