CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.

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

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Three types of computer languages
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.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
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.
Chapter 3: Input/Output
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
COMPUTER SCIENCE I C++ INTRODUCTION
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
1 CS161 Introduction to Computer Science Topic #3.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 C++ Basics.
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 
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
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
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
LESSON 2 Basic of C++.
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Lecture 3: Getting Started & Input / Output (I/O)
C++ Basics Lecture 2.
Variables, Identifiers, Assignments, Input/Output
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 C++ Basics. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow.
Chapter 2 C++ Basics.
Topic Pre-processor cout To output a message.
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basics (Variables, Assignments, I/O)
Basic Elements of C++ Chapter 2.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Basics (Variables, Assignments, I/O)
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Programming Funamental slides
Variables, Identifiers, Assignments, Input/Output
Chapter 3: Input/Output
What Actions Do We Have Part 1
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi

Introduction For inputting and outputting data we use library function.the important of these functions are getch( ), putchar( ), scanf( ), printf( ), gets( ), puts( ). For using these functions in a C-program there should be a preprocessor statement #include. A preprocessor statement is a statement before the main program, which begins with # symbol. stdio.h is a header file that contains the built in program of these standard input output function.

Introduction A data stream is a sequence of data. Typically in the form of characters or numbers. An input stream is data for the program to use usually originates from, either the keyboard or a file. An output stream is the program’s output destination would usually be, either the monitor a file.

Definitions cout: The identifier cout is a predefined object that corresponds to standard output stream. cin: The identifier cin is a predefined object that corresponds to standard input stream. Escape sequence/ Escape character: Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits is called “escape sequence”.

I/O operators The input operator(>>) is used to read value from standard input The output operator(<<) is used to direct a value to standard output

Example #include using namespace std; void main( ) { int a,b,c; float d; cout<<”Enter three numbers”; cin>>a>>b>>c; d=(a+b+c)/3; cout<<”The average= ”<<d; } Output:

Input Using cin cin is an input stream bringing data from the keyboard. The extraction operator (>>) removes data to be used. Syntax: cin>>variable_name; Example: cout << "Enter the number of bars in a package\n"; cout << " and the weight in ounces of one bar.\n"; cin >> number_of_bars; cin >> one_weight; This code prompts the user to enter data then reads two data items from cin. The first value read is stored in number_of_bars. The second value read is stored in one_weight. Data is separated by spaces when entered

Reading Data From cin Multiple data items are separated by spaces. Data is not read until the enter key is pressed. Allows user to make corrections. Example: cin >> v1 >> v2 >> v3; Or cin>>v1; cin>>v2; cin>>v3; Requires three space separated values User might type

Output using cout cout is an output stream sending data to the monitor. The insertion operator "<<" inserts data into “cout”. Example: cout << number_of_bars << " candy bars\n"; This line sends two items to the monitor. The value of number_of_bars. The quoted string of characters " candy bars\n". Notice that the space before the ‘c’ in candy. The ‘\n’ causes a new line to be started following the ‘s’ in bars. A new insertion operator is used for each item of output.

Examples This produces the same result as the previous sample cout << number_of_bars ; cout << " candy bars\n"; Here arithmetic is performed in the cout statement cout << "Total cost is $" << (price + tax); Quoted strings are enclosed in double quotes ("Walter"). Don’t use two single quotes ('). A blank space can also be inserted with cout << " " ; if there are no strings in which a space is desired as in " candy bars\n"

Designing Input and Output Prompt the user for input that is desired. cout statements provide instructions cout << "Enter your age: "; cin >> age; Echo the input by displaying what was read gives the user a chance to verify data cout << age << " was entered." << endl;

Example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

Exercises Write a single C++ statement to accomplish each of the following: Print the message “This is a C++ program” on one line. Print the message “This is a C++ program” on two lines. End the first line with C++. Print the message “This is a C++ program” with each word on a separate line. Print the message “This is a C++ program” with each word separated from the next by a tab. Declare the variable age to be of type int. Prompt the user to enter his/her age. End your prompting message with : followed by space. Read and integer from the user and store the value entered in the variable age.