King Saud University College of Applied studies and

  • Slides: 29
Download presentation
King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah

King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436

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

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

Input/Output Operations 3 Input operation an instruction that copies data from an input device

Input/Output Operations 3 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) Fatimah Alakeel 2/17/2012

Using iostream 4 The C++ iostream library provides hundreds of I/O capabilities. Standard iostream

Using iostream 4 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 Fatimah Alakeel 2/17/2012

5 Output Stream Fatimah Alakeel 2/17/2012

5 Output Stream Fatimah Alakeel 2/17/2012

The Insertion Operator (<<) 6 To send output to the screen we use the

The Insertion Operator (<<) 6 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 4. 1; // Outputs 4. 1 “String”; // Outputs String ‘n’; // Outputs a newline Fatimah Alakeel 2/17/2012

Stream-Insertion Operator 7 << is overloaded to output built-in types ( ex. int, float,

Stream-Insertion Operator 7 << 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’; cout << endl; Prints newline character endl is a stream manipulator that issues a newline character and flushes the output buffer cout << flush; flushes the output buffer a buffer is just a pre-allocated area of memory where you store your data while you're processing it. Fatimah Alakeel 2/17/2012

Cascading Stream-Insertion Operators 8 << : Associates from left to right, and returns a

Cascading Stream-Insertion Operators 8 << : 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 Fatimah Alakeel + 2; 2/17/2012

Printing Variables 9 cout << some. Variable; cout knows the type of data to

Printing Variables 9 cout << some. Variable; 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 Fatimah Alakeel 2/17/2012

Formatting Stream Output 10 Performs formatted and unformatted output I. II. Display numbers on

Formatting Stream Output 10 Performs formatted and unformatted output I. II. Display numbers on different width , filling spaces with Varying precision for floating points Formatted text outputs Fatimah Alakeel 2/17/2012

III. Significant Digits in Float 11 If the fixed point format is not specified,

III. Significant Digits in Float 11 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 = 23. 1415; cout. precision(2); cout << y << 'n'; // Outputs 23 cout. precision(3); cout << y << 'n'; // Outputs 23. 1 Fatimah Alakeel 2/17/2012

Using showpoint/noshowpoint 12 showpoint specify that floating-point numbers (even for whole numbers) should be

Using showpoint/noshowpoint 12 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. Fatimah Alakeel 2/17/2012

Using showpoint/noshowpoint 13 #include <iostream> using namespace std; int main () { double a,

Using showpoint/noshowpoint 13 #include <iostream> using namespace std; int main () { double a, b, pi; a=30. 0; 30. 000 10000. 3. 1416 30 10000 3. 1416 b=10000. 0; pi=3. 14165; cout. precision (5); cout << showpoint << a << 't' << b << 't' << pi << endl; cout << noshowpoint << a << 't' << b << 't' << pi << endl; return 0; } Fatimah Alakeel 2/17/2012

IV. Formatting Text 14 To print text you need to include “” around the

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

Escape Sequence 15 Fatimah Alakeel 2/17/2012

Escape Sequence 15 Fatimah Alakeel 2/17/2012

Examples 16 cout<<"Please enter the student's grades: ”; Please enter the student's grades: cout<<"The

Examples 16 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 Average = 95. 5 cout<<“The total area is “<< area<< “and the total cost is “<< cost << “ S. R. ”; area = 60. 2 The total area is 60. 2 and the total cost is 4530 S. R. cost = 4530 Cout<<"The student received an ”<< grade << “ grade in the course. "; The student received an A grade in the course. Fatimah Alakeel 2/17/2012 grade = ‘A’

Examples (Con. ) 17 Cout<<”The grade is << gradesymb; The grade is A+ grade

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

Cascading Stream-Insertion Operators Allows creating a single output statement that prints 1 or more

Cascading Stream-Insertion Operators Allows creating a single output statement that prints 1 or more type of data e. g. (1) int age=25; cout<<“Sarah is “<<age<<“Years Old”; Compilers start printing from left to right e. g. int age=25; cout<<“Sarah is “; cout<<age; cout<<“Years Old”; Compilers start printing from top to down Output for both Sarah is 25 Years Old

19 Input Stream Fatimah Alakeel 2/17/2012

19 Input Stream Fatimah Alakeel 2/17/2012

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

The Extraction Operator (>>) 20 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 Fatimah Alakeel 2/17/2012

Syntax 21 cin >> some. Variable; cin knows what type of data is to

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

Stream Input 22 >> (stream-extraction) Used to perform stream input Normally ignores whitespaces (spaces,

Stream Input 22 >> (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; Fatimah Alakeel 2/17/2012

Stream Input 23 cin inputs ints, chars, null-terminated strings, string objects but terminates when

Stream Input 23 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] Fatimah Alakeel 2/17/2012

Chaining Calls 24 Multiple uses of the insertion and extraction operator can be chained

Chaining Calls 24 Multiple uses of the insertion and extraction operator can be chained together: cout << E 1 << E 2 << E 3 << … ; cin >> V 1 >> V 2 >> V 3 >> …; Equivalent to performing the set of insertion or extraction operators one at a time Example cout << “Total sales are $” << sales << ‘n’; cin >> Sales 1 >> Sales 2 >> Sales 3; Fatimah Alakeel 2/17/2012

Extraction/Insertion Example 25 cout << “Hello world! ”; int i=5; cout << “The value

Extraction/Insertion Example 25 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 Fatimah Alakeel 2/17/2012

26 Common Programming Errors Fatimah Alakeel 2/17/2012

26 Common Programming Errors Fatimah Alakeel 2/17/2012

Common Programming Errors 27 Debugging Process removing errors from a program Three (3) kinds

Common Programming Errors 27 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 Fatimah Alakeel 2/17/2012

28 Common Programming Errors cont… Run-time errors An attempt to perform an invalid operation,

28 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 Fatimah Alakeel 2/17/2012

29 Common Programming Errors cont… Logic Error/Design Error An error caused by following an

29 Common Programming Errors cont… Logic Error/Design Error An error caused by following an incorrect algorithm Very difficult to detect - it does not cause runtime 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 Fatimah Alakeel 2/17/2012 actually type it