Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml.

Slides:



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

1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
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.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Input & Output: Console
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
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:
Chapter 3: Assignment, Formatting, and Interactive Input.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
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 3: Input/Output
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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.
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.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
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
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Computing Fundamentals
Chapter 2 part #3 C++ Input / Output
DATA HANDLING.
Input and Output Chapter 3.
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
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
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 2 part #3 C++ Input / Output
Introduction to cout / cin
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR

Objectives The contents we are going to cover in this chapter:  Input and Output  Standard Output  Escape Sequences  C++ Manipulators  Standard Input

Input & Output Input Operation The process of giving something to computer usually with the help of keyboard is called input. A program may need certain type of input data to perform its functionality. The term standard input refers to input via keyboard. Output Operation The process of getting something from computer usually on monitor screen is called output. The term standard output is refers to output displayed on the monitor. As, the result of a program is the output of the program displayed on the monitor screen, In C++, different streams are available by the developers of the language to perform certain type of input output operation. Stream – Flow Of Data These are the objects used by the program to insert or extract characters. C++ library includes the header file iostream.h Iostream.h contains the declarations of all the standards input and output objects. So header file iostream.h must be included in a program to perform standard input and output. CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input

Standard Output As the term Standard Output refers to the output displayed on the monitor. C++ uses cout object (stands for “console output” & read as “see-out”) to display standard output. The cout object is used with insertion operator ( << ). Any string, value or constant followed by the << is displayed on the screen. Syntax: cout << variable / constant / expression ; Example includes as follows ocout << var1;// variable ocout << 10;// constant ocout <<“C++ Strong Programming Language!”; // string value CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Message << cout

Standard Output Examples CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Output: testing output…10* Output: = 65.77

Standard Output CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Escape Sequences Escape sequence are special character used in Control String. These are used to modify the format of output. These are not displayed on the screen in output. These special characters are comes with the combinations of back slash ‘/’ known as escape character Following table shows the Escape Sequences in C++

Standard Output Examples CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Using one cout-statement print Asterisks in this format. * * * * * * * * * * * Solution: (we use escape sequence new line i.e. “\n” ) #include int main() { cout<<“*\n**\n***\n****\n*****”; return 0; }

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input C++ Manipulators C++ manipulators are used to format the output in different styles. These the common controls to handle the output issues. These manipulators enable the user to make output in desired format which one is appropriate. Following table contains some important C++ manipulators: Now we discuss each of the manipulators one by one in details

C++ Manipulators endl() Manipulator (iostream.h) The word ‘endl’ stands for end-of-line. The endl manipulator is used to move the cursor to beginning of the next line in the output. It works similar to ‘\n’ escape sequence. It requires no parameters. Syntax: cout << variable / expression / constant << endl; Example is as follows #include Int main() { clrscr(); cout << “Hello”<<endl<<“World!”; cout << endl << endl << “Hello World!”; return 0; } CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Hello World! Hello World!

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input setw() Manipulator (iomanip.h) The word setw stands for set width. The setw manipulator is used to display the value of an expression in specified columns. The value to displayed can be a string or any numeric value i.e. integer, floating-point number, CONSTANT etc… If the value of the expression is less than the additional columns are left blank from left side. The output automatically uses the required columns if the output is larger than the specified columns. (In this case the setw manipulator has no effect on the output) Syntax: setw( int n); The integer n indicates the number of columns in which the value is to be displayed. It can be unsigned positive integer constant, variable or expression. Suppose variable a = 33 and variable b = 7132 a)cout << setw(4) << a << setw(5) << b << setw(4) << “Hi”; b)cout << setw(2) << a << setw(4) << b << setw(2) << “Hi”; c)cout << setw(6) << a << setw(5) << b << setw(3) << “Hi”; d)cout << setw(1) << a << setw(5) << b << setw(1) << “Hi”; Note: This table is just for illustration purpose otherwise in real scenario screen of a normal monitor is 80 cells in width and 25 cells in height i.e. 80x25 characters

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Setw( int n ) Example #include int main() { int a = 33; int b = 7132; cout << a << b << "Hi" <<endl; cout << a <<" "<< b <<" "<< "Hi" <<endl; cout << "\nNow we use C++ Manipulators in different ways\n" <<endl; cout << setw(4) << a << setw(5) << b << setw(4) <<"Hi"<<endl; cout << setw(3) << a << setw(5) << b << setw(3) <<"Hi"<<endl; cout << setw(6) << a << setw(5) << b << setw(3) <<"Hi"<<endl; cout << setw(1) << a << setw(5) << b << setw(1) <<"Hi"<<endl; return 0; } Hi Now we use C++ Manipulators in different ways Hi Press any key to continue...

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input setprecision() Manipulator The setprecision manipulator is used to set the number digits to be displayed after decimal point. It is applied to all subsequent floating-point numbers written to that output stream. The value is rounded with the use of this manipulator. Syntax: cout << setprecision(int) << variable ; // accepts positive integers Example Press any key to continue... #include int main() { double result, n1 = , n2 = 26.91; result = 0.0; result = n1 / n2 ; cout << setprecision(10)<<result<<endl; cout << setprecision(9)<<result<<endl; cout << setprecision(8)<<result<<endl; cout << setprecision(7)<<result<<endl; cout << setprecision(6)<<result<<endl; cout << setprecision(5)<<result<<endl; cout << setprecision(4)<<result<<endl; cout << setprecision(3)<<result<<endl; cout << setprecision(2)<<result<<endl; cout << setprecision(1)<<result<<endl; return 0; }

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input fixed() Manipulator The fixed manipulator is used to further control the output of the floating point numbers. It displays the floating point number in a fixed decimal format. To use this manipulator we execute statement as follows cout << setiosflags(ios::fixed) << variable; All floating point number displayed afterwards – will maintain their fixed decimal format i.e. their will not any other format like scientific or exponential format. To disable this type of flags we execute similar statement as follows cout << resetiosflags(ios::fixed) << variable; Example: In C++ on the basis of default settings - The value is displayed as 1e+07 in exponential format which is quite harder to understand in first look. Here is the situation where we have to manage output in understandable format by using appropriate manipulators provided by C++ Developers. Solution: simple we use fixed manipulator as cout << setiosflags(ios::fixed) << ; // output as mentioned

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input fixed manipulator example #include int main() { long double var1 = ; cout << "Without using fixed manipulator" <<endl; cout << "var1 = " <<var1 << endl; cout << "\nNow we use fixed manipulator" <<endl; cout << "var1 = "<< setiosflags(ios::fixed)<<var1<<endl; cout << "\nNow we use fixed and setprecision(2) manipulators" <<endl; cout << "var1 = “ <<setiosflags(ios::fixed)<<setprecision(2)<<var1<<endl; cout << "\nWe Change the value of var1 = "<<endl; var1 = ; cout <<"Now we use fixed and setprecision(2) manipulators" <<endl; cout <<"var1 = “ <<setiosflags(ios::fixed)<<setprecision(2)<<var1<<endl<<endl; return 0; }

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input showpoint() Manipulator By default decimal part of a floating point number is not displayed if it is zero. In a situation where it is necessary to show decimal part also to achieve accuracy i.e. in temperature records or to show CGPA (Cumulative Grade Point Average) of students Syntax: cout << setiosflags(ios::showpoint)<<variable or value; #include int main() { float points = 3.0; cout<<"Without using Manipulators"<<endl; cout<<"CGPA = "<<points<<endl; cout<<"\nUsing setiosflags(ios::showpoint)"<<endl; cout<<"CGPA = "<<setiosflags(ios::showpoint)<<points<<endl; cout<<"\nUsing two manipulators: setprecision(3) and setiosflags(ios::showpoint)"<<endl; cout<<"CGPA = "<<setprecision(3)<<setiosflags(ios::showpoint)<<points<<endl; cout<<"\nCGPA has been changed from 3 to 3.5"<<endl; points = 3.5; cout<<"\nUsing two manipulators: setprecision(3) and setiosflags(ios::showpoint)"<<endl; cout<<"CGPA = "<<setprecision(3)<<setiosflags(ios::showpoint)<<points<<endl<<endl; return 0; }

C++ Manipulators CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input setfill() Manipulator The setfill() manipulator is used to replace the leading or trailing blanks in the output by the specified characters. It requires one parameter to specify the fill character. The parameter can be a letter, digit or constant as a character i.e. in single quotes like ‘0’ or #include int main() { int var1 = 20; cout << var1 << endl; cout << setw(5) << var1 << endl; cout << setfill('#') << setw(05) << var1 <<endl; cout << setfill(' ') << setw(20) << var1 <<endl; cout << setfill('0') << setw(20) << var1 <<endl; return 0; }

Standard Input CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input The term standard input refers to the input given via keyboard. C++ uses cin (stands for C onsole In put and read as “see-in”) stream object to perform standard input operation. C++ handles standard input operation by applying extraction operator “>>” with cin object. The “>>” is called extraction operator because it extracts the value from input stream and store it in the memory. The “>>” operator must be followed by a variable. The variable very next to extraction operator “<<” is used to store the value. Syntax: cin >> var1 >> var2 >> varN; If there are more than one variable on the left of >> as shown here then the first value will store in var1 and so on… #include int main() { char name[20], cityName[20]; int age = 0; cout <<"Please Enter Your Name: "; cin >> name; cout <<"Please Enter Your Age: "; cin >> age; cout <<"Please Enter name of your City: "; cin >> cityName; cout <<"\n\nHello \""<<name<<"\"! How are you?"; cout <<"\nYour age is \""<<age<<"\" thats good."; cout <<"\nYour City is \""<<cityName<<"\" - Have a nice day!"<<endl; return 0; } Please Enter Your Name: Ali Please Enter Your Age: 22 Please Enter name of your City: GUJRANWALA Hello "Ali"! How are you? Your age is "22" thats good. Your City is "GUJRANWALA" - Have a nice day! Press any key to continue...

Standard Input CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Standard Input Example Input two Integers, print them on screen then swap them and again print on screen. #include int main() { int num1, num2; int temp; // used while swapping cout << "Enter First Number: "; cin >> num1; cout << "Enter Second Number: "; cin >> num2; cout << "\nFirst Number : "<<num1<<endl; cout << "Second Number : "<<num2<<endl; temp = num1; num1 = num2; num2 = temp; cout << "\nAfter Swapping\n\nFirst Number : "<<num1<<endl; cout << "Second Number : "<<num2<<endl; cout<<endl<<endl; return 0; } Enter First Number: 10 Enter Second Number: 18 First Number : 10 Second Number : 18 After Swapping First Number : 18 Second Number : 10 Press any key to continue...

Standard Input CONTENTS Input & Output (I/O) Standard Output Escape Sequences C++ Manipulators endl setw setprecision fixed showpoint setfill Standard Input Standard Input Example #include 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; } Please enter an integer value: 702 The value you entered is 702 and its double is 1404.