31
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 2 nd Semester 1432 -1433 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel

Chapter 2 part #3 C++ Input / Output

  • Upload
    luella

  • View
    70

  • Download
    0

Embed Size (px)

DESCRIPTION

King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel. Chapter 2 part #3 C++ Input / Output. 2 nd Semester 1432 -1433. Outline. Input / Output Operations Using iostream Output Stream Input Stream Common Programming Errors. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 2  part #3 C++ Input / Output

CHAPTER 2 PART #3C++ INPUT / OUTPUT2nd Semester 1432 -1433

King Saud University College of Applied studies and Community ServiceCSC1101By: Fatimah Alakeel

Page 2: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

2

Outline Input / Output Operations Using iostream Output Stream Input Stream Common Programming Errors

2/17/2012

Page 3: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

3

Input/Output Operations Input operation

an instruction that copies data from an input device into memory

Input Stream: A stream 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)

2/17/2012

Page 4: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

4

Using iostream.h Include iostream instead of stdio.h 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/2012

Page 5: Chapter 2  part #3 C++ Input / Output

5

Fatimah Alakeel

Output Stream

2/17/2012

Page 6: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

6

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 appropriatelycout << 5; // Outputs 5cout << 4.1; // Outputs 4.1cout << “String”; // Outputs Stringcout << ‘\n’; // Outputs a newline

2/17/2012

Page 7: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

7

Stream-Insertion Operator

<< is overloaded to output built-in types Can also be used to output user-defined types 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/2012

a buffer is just a pre-allocated area of memory where you store your data while you're processing it.

Page 8: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

8

Cascading Stream-Insertion/Extraction Operators

<< : Associates from left to right, and returns a reference to its left-operand object (i.e. cout). This enables cascadingcout << "How" << " are" << " you?";

Make sure to use parenthesis:

cout << "1 + 2 = " << (1 + 2);

NOT

cout << "1 + 2 = " << 1 + 2; 2/17/2012

Page 9: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

9

Printing Variables 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/2012

Page 10: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

10

Formatting Stream Output

Performs formatted and unformatted outputI. Output of numbers in decimal, octal and hexadecimal

using manipulators.II. Display numbers on different width , filling spaces with

charactersIII. Varying precision for floating pointsIV. Formatted text outputs

2/17/2012

Page 11: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

11

I. Manipulators C++ manipulators

Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects.

must include iomanip to use several are provided to do useful things you can also create your own

2/17/2012

Page 12: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

12

Output Manipulators (no args)Manipulators included like arguments in extraction

endl - outputs a new line character, flushes outputdec - sets int output to decimalhex - sets int output to hexadecimaloct - sets int output to octal

Example:#include <iostream>#include <iomanip>using namespace std;int x = 42;cout << oct << x << endl; // Outputs 52\ncout << hex << x << endl; // Outputs 2a\ncout << dec << x << endl; // Outputs 42\n

2/17/2012

Page 13: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

13

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 42Cout << x << ‘\n’; // Outputs 42ORcout << setw (10); cout << 77 << endl; // prints 77 on 10 places

2/17/2012

Page 14: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

14

II. Setting the Fill CharacterUse 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 ***42ORcout << setfill ('x') << setw (10); cout << 77 << endl; // prints xxxxxxxx77

2/17/2012

Page 15: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

15

III. Significant Digits in FloatUse function precision(int) to set the

number of significant digits printed (may convert from fixed to scientific to print):float y = 23.1415;cout.precision(1);cout << y << '\n'; // Outputs 2e+01cout.precision(2);cout << y << '\n'; // Outputs 23cout.precision(3);cout << y << '\n'; // Outputs 23.1

2/17/2012

Page 16: Chapter 2  part #3 C++ Input / Output

16

Fatimah Alakeel

Using showpoint/noshowpoint

#include <iostream>using namespace std; int main () {double a, b, pi;a=30.0; b=10000.0; pi=3.1416; cout.precision (5); cout << showpoint << a << '\t' << b << '\t' << pi << endl; cout << noshowpoint << a << '\t' << b << '\t' << pi << endl; return 0; }

2/17/2012

30.000 10000. 3.1416 30 10000 3.1416

Page 17: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

17

IV. Formatting Text 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/2012

Page 18: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

18

Escape Sequence

2/17/2012

Page 19: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

19

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.

2/17/2012

Page 20: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

20

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 lineI am the second line

2/17/2012

Page 21: Chapter 2  part #3 C++ Input / Output

21

Fatimah Alakeel

Input Stream

2/17/2012

Page 22: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

22

The Extraction Operator (>>) To get input from the keyboard we use

the extraction operator and the object cin Format: cin >> Variable; No need for & in front of variable The compiler figures out the type of the

variable and reads in the appropriate typeint X;float Y;cin >> X; // Reads in an integercin >> Y; // Reads in a float

2/17/2012

Page 23: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

23

Syntaxcin >> someVariable;

cin knows what type of data is to be assigned to someVariable (based on the type of someVariable).

2/17/2012

Page 24: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

24

Stream Input

>> (stream-extraction) Used to perform stream input Normally ignores whitespaces (spaces, tabs,

newlines) Returns zero (false) when EOF is encountered,

otherwise returns reference to the object from which it was invoked (i.e. cin) This enables cascaded inputcin >> x >> y;

2/17/2012

Page 25: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

25

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/2012

Page 26: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

26

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

Examplecout << “Total sales are $” << sales << ‘\n’;cin >> Sales1 >> Sales2 >> Sales3;2/17/2012

Page 27: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

27

Extraction/Insertion Examplecout << “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: FYour name starts with F

2/17/2012

Page 28: Chapter 2  part #3 C++ Input / Output

28

Fatimah Alakeel

Common Programming Errors

2/17/2012

Page 29: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

29

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

2/17/2012

Page 30: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

30

Common Programming Errors cont…

Run-time errorsAn 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

2/17/2012

Page 31: Chapter 2  part #3 C++ Input / Output

Fatimah Alakeel

31

Common Programming Errors cont…

Logic Error/Design ErrorAn error caused by following an

incorrect algorithmVery 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 2/17/2012