INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.

Slides:



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

1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
C++ Programming: Basic Elements of C++.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
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 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Basic Elements Skill Area 313 Part B. Lecture Overview Basic Input/Output Statements Whitespaces Expression Operators IF Statements Logical Operators.
Constants, Data Types and Variables
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.
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
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Dr. Shady Yehia Elmashad
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Dr. Shady Yehia Elmashad
Chapter 2 part #3 C++ Input / Output
DATA HANDLING.
Basic Elements of C++ Chapter 2.
Dr. Shady Yehia Elmashad
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Basics (Variables, Assignments, I/O)
Introduction to C++ Programming
Programming Funamental slides
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.
Programming Funamental slides
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to cout / cin
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
Chapter 2 part #3 C++ Input / Output
C++ Programming Basics
Introduction to cout / cin
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam

Standard Input and Output The process of giving something to computer from the keyboard is known as standard input. Program need instruction from the user and perform operations on that accordingly. On the other hand, the process of taking some thing from computer to the monitor is known as standard output. C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen or the keyboard. A stream is an object where a program can either insert or extract characters to/from it.

Standard Input and Output The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared. Therefore, this header file iostream.h must be included in all c++ programs that uses input and output statements. Program uses stream object to insert and extract data.. 10/20/2016

Standard Output (cout) By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout. cout is used in conjunction with the insertion operator, which is written as << (two "less than" signs). The << operator inserts the data that follows it into the stream preceding it. Whenever we want to use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variable names. For example, these two sentences have very different results: The insertion operator (<<) may be used more than once in a single statement:

Standard Output (cout) The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable: It is important to notice that cout does not add a line break after its output unless we explicitly indicate it, either by : \n operator or by endl manipulator: 10/20/2016

Standard Input (cin) The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example: cin can only process the input from the keyboard once the RETURN key has been pressed. You must always consider the type of the variable that you are using as a container with cin extractions. If you request an integer you will get an integer, if you request a character you will get a character and if you request a string of characters you will get a string of characters. 10/20/2016

Standard Input (cin) // i/o 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; } 10/20/2016

Standard Input and Output //program that display a message, integer and character variables //output.cpp #include void main() { clrscr(); int n=10; char ch='*'; cout<<"this is output.."<<endl; cout<<n<<endl; cout<<ch; getch(); }

Standard Input and Output //program to add two floating point numbers and shows the sum on screen. //add-num.cpp #include void main() { clrscr(); float var1, var2, res; var1=24.27; var2=41.50; res= var1+var2; cout<<var1<<" + "<<var2<<" = "<<res; getch(); }

Standard Input and Output //program to calculate and print the area of square with given height and width //area-square.cpp #include void main() { clrscr(); int height, width, area; height=10; width=5; area= height*width; cout<<"area of square= "<<area; getch(); }

Definitions Extraction Definition with initialization Area.cpp #include using namespace std; int main() { // Extract length and width cout << "Rectangle dimensions: "; float Length; float Width; cin >> Length >> Width; // Compute and insert the area float Area = Length * Width; cout << "Area = " << Area << " = Length " << Length << " * Width " << Width << endl; return 0; }

C++ Manipulators Manipulators are instructions to the output stream that modify the output in various ways. or manipulators are operators used with the insertion operator << to modify—or manipulate—the way data is displayed. include header file “iomanip.h” in the program when using these operators. Several types of manipulators are available in C++ such as: endl, setw(), setprecision(), fixed(), showpoint() and setfill(). We’ve already seen the endl manipulator; now we’ll look at another one: setw(), which changes the field width of output. 10/20/2016Reference source OOP by Lafore

Setw() Manipulator //Program without setw manipulator // setw-need.CPP #include int main() { long pop1= , pop2=47, pop3=9761; cout << "LOCATION " << "POP." << endl << "Portcity " << pop1 << endl << "Hightown " << pop2 << endl << "Lowville " << pop3 << endl; getch(); return 0; } 10/20/2016 // demonstrates setw manipulator //setw.CPP #include #include // heater file for setw #include int main() { long pop1= , pop2=47, pop3=9761; cout << setw(8) << "LOCATION" << setw(18) << "POPULATION" << endl << setw(8) << "Portcity" << setw(18) << pop1 << endl << setw(8) << "Hightown" << setw(18) << pop2 << endl << setw(8) << "Lowville" << setw(18) << pop3 << endl; getch(); return 0; }

Setw() Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where n is the argument to setw(n). The value is right-justified within the field. 10/20/2016

setprecision() Manipulator The precision determines the maximum number of digits that shall be output on insertion operations to express floating-point values, counting both the digits before and after the decimal point. Sets the decimal precision to be used to format floating-point values on output operations. //setpre.cpp #include // Declarations of cin, cout,... #include // Manipulator setw() #include int main() { string label; double price; cout << "\nPlease enter an article label: "; cin >> setw(16); cin >> label; cout << "\n Enter the price of the article: "; cin >> price; // Input the price 10/20/2016 // Controlling output: cout << setprecision(2) << "\nArticle:" << "\n Label: " << label << "\n Price: " << price << endl; //... The program to be continued getch(); return 0; }