Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT 1033 1.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 2: Basic Elements of C++
Chapter 3: Input/Output
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
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 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
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
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 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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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:
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Chapter 3: Input/Output
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Chapter 05 (Part II) Control Statements: Part II.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 3: Expressions and Interactivity.
Chapter 2: Basic Elements of C++
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
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.
Chapter 3: Input/Output
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
C++ for Engineers and Scientists Second Edition
Programming Fundamental-1
Presentation transcript:

Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT

Objectives In this chapter, you will: Discover how to input data into memory using input statements Explore how to read data from the standard input device Learn how to debug a program using cout statements Learn how to debug a program by understanding error messages 2

3 Objectives (continued) Explore how to use the input stream functions get, ignore, and clear Become familiar with input failure Learn how to perform input and output operations with the string data type Learn how to format output using the manipulators fixed, showpoint, setprecision, setw, left, and right Become familiar with file input and output

4 Input (Read) Statement The syntax of an input (read) statement using cin and the stream extraction operator >> is: During programming execution, if more than one value is entered in a line, these values must be separated by at least one blank or tab –Or, one value per line can be entered

Input (Read) Statement (continued) Must be included to use cin So we can refer to cin without using std:: 55

Variable Initialization C++ does not automatically initialize variables when they are declared Ways to initialize a variable –By using the assignment statement feet = 35; –By using a read statement cin >> feet; More versatile 66

Creating a C++ Program (Revisited) In skeleton form, a C++ program looks like this: preprocessor directives to include header files using statement declare named constants, if necessary int main() { variable declaration statements return 0; } 77

88

Programming Style and Form (Revisited) In Chapter 1, you learned about syntax, semantics, errors, and how to format a program properly In this section, we extend our capabilities to include prompt lines in a program 99

Prompt Lines Prompt lines: executable statements that tell the user what to do to interact with the program cout << "Please enter a number between 1 and 10 and then " << " press Enter." << endl; cin >> num; Prompt lines should include sufficient information about what input is acceptable 10

Debugging – Sprinkling with cout s Walk-throughs may fail to reveal bugs Inserting temporary strategically located cout statements can aid in the debugging process int fahrenheit = 32; int celsius = 5 / 9 * (fahrenheit - 32); cout << fahrenheit << " degrees " << "Fahrenheit is " << celsius << " degrees " << "Celsius. " << endl; cout << "5 / 9 is " << 5 / 9 << " and fahrenheit - 32 is " << fahrenheit - 32 << endl; 11

Debugging – Understanding Error Messages The C++ compiler will find syntactic errors in your program and will provide messages describing the errors –Provide a good indication of where to look for the problem(s) –Look carefully in the immediate vicinity of the reported error Finding and reporting errors is like peeling away layers of an onion 12

13 Input Failure Things can go wrong during execution If input data does not match corresponding variables, program may run into problems Trying to read a letter into an int or double variable will result in an input failure If an error occurs when reading data: –Input stream enters the fail state

Input Failure (continued) 14

Input Failure (continued) Sample run 1 Line 9: Enter four integers: 34 K Line 12: The numbers you entered are: Line 13: a = 34, b = 20, c = 30, d = 40 Sample run 2 Line 9: Enter four integers: Line 12: The numbers you entered are: Line 13: a = 37, b = 653, c = 30, d = 40 15

16 Formatting Output Other than writing efficient programs, generating the desired output is one of a programmer’s highest priorities In this section, you will learn about various output functions and manipulators that allow you to format your output in a desired way

17 setprecision Manipulator Syntax is: Outputs decimal numbers with up to n decimal places Must include the header file iomanip : #include

18 fixed Manipulator fixed outputs floating-point numbers in a fixed decimal format Disable by using the stream member function unsetf The manipulator scientific is used to output floating-point numbers in scientific format

19 showpoint Manipulator showpoint forces output to show the decimal point and trailing zeros Example cout << fixed << showpoint;

20

showpoint Manipulator (continued) Sample run Line 10: setprecision(2) Line 11: x = Line 12: y = Line 13: z = Line 14: setprecision(3) Line 15: x = Line 16: y = Line 17: z = Line 18: setprecision(4) Line 19: x = Line 20: y = Line 21: z =

22 setw Outputs the value of an expression in specific columns cout << setw(5) << x << endl; If number of columns exceeds the number of columns required by the expression: –Output of the expression is right-justified –Unused columns to the left are filled with spaces Must include the header file iomanip

23

setw (continued) Sample run Hi

25 left and right Manipulators left : left-justifies the output right : right-justifies the output

left and right Manipulators (continued) 26

left and right Manipulators (continued) Sample run Warm 27

28 Summary Input from the standard input device is accomplished by using cin and >> >> is the stream extraction operator When data is input in a program, the data items are usually separated by blanks, lines, or tabs When inputting data into a variable, >> skips all leading whitespace characters To use cin, include the header file iostream Prompt lines: executable statements that tell user what to do

29 Summary (continued) setprecision, fixed, showpoint, setw, left, and right can be used to format output Include iomanip to use setprecision and setw