CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1434 -1435 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.

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

 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C++ Programming
Introduction to C++ Programming
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Input & Output: Console
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Input, Output, and Processing
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Week 1 Algorithmization and Programming Languages.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 05 (Part III) Control Statements: Part II.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 2 part #1 C++ Program Structure
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
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.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
 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.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Chapter 2 Assignment and Interactive Input
Chapter 2 part #3 C++ Input / Output
Chapter 2 – Getting Started
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 3: Input/Output
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 part #3 C++ Input / Output
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 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel 2013 Edit: Ghadah Al hadba

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

Output Stream

The Insertion Operator (<<)  To send output to the screen we use the insertion operator << on the object cout  Format: cout << something; This something can be: 1. Literal e.g cout<<5; 2. Expression e.g cout<<(5+4); 3. Variable or constant e.g cout<<num;

The Insertion Operator (<<): Literal  << is overloaded to output built-in types ( ex. int, float,…etc) as well as user-defined types (i.e. user defined classes).  The compiler figures out the type of the object and prints it out appropriately e.g(1): cout << 5; // Outputs 5 e.g(2): cout << 4.1; // Outputs 4.1 e.g(3): cout << “Hello”; // Outputs Hello e.g(4): cout << ‘\n’; // move the cursor to a newline String literals should be surrounded with double quotation marks “ “ character literals should be surrounded with single quotation marks ‘ ‘ Important: escape sequence characters ( such as : \n, \t)can be either embedded into a string or printed alone as e.g (4)

The Insertion Operator (<<): Expression  Printing an expression is similar to printing a values since the compiler will calculate then print the resulted value.  Thus, the expression operands should be previously declared and had a value e.g.(1): cout << (5 + 1); // Outputs 6 e.g.(2): int x = 5, y; y = 3; cout << ((x + y)/ 2); // Outputs 4 Important: With expressions make sure to use parenthesis (), to get the expected result (to be explained in detail Later in lect6)

The Insertion Operator (<<): Variables and constants  To print the content of a variable of a constant identifier we simply place the identifier name after the insertion operator  cout knows the type of data to output, will be printed probably e.g.(1): int x = 5; const double RATE = 3.14; cout << x; // Outputs 5 cout << RATE; // Outputs 3.14 Important: Must not confuse printing text with printing variables, i.e. don’t surround the variable name with a “ “: int m =12; cout << m; // Outputs 12 cout << “m”; // Outputs m

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”; Output for both Sarah is 25 Years Old e.g. int age=25; cout<<“Sarah is “; cout<<age; cout<<“Years Old”; Compilers start printing from top to down Compilers start printing from left to right

Cascading Stream-Insertion Operators  Allows creating a single output statement that prints 1 or more type of data e.g. (2) cout << "How" << " are" << " you?"; How are you? Output

Input Stream

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 e.g. int X; float Y; cin >> X; // Reads in an integer cin >> Y; // Reads in a float

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

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;

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;

Extraction/Insertion Example cout << “Hello world! ”; int i=5; cout << “The value of i is “ << i << endl; //endl puts a new lineOUTPUT: Hello World! The value of i is 5 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

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

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

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