CPS120: Introduction to Computer Science

Slides:



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

I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
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:
CPS120: Introduction to Computer Science Formatted I/O.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 CS161 Introduction to Computer Science Topic #4.
Chapter 3: Input/Output
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
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
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.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Introduction to C++. Introducing C++ C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Topics Designing a Program Input, Processing, and Output
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Chapter 3: Expressions and Interactivity.
Output Stream Formatting
Chapter 2 part #3 C++ Input / Output
Formatting Screen Input/Output
Input and Output Chapter 3.
Formatted Input/Output
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Introduction to C++ Programming
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 5 Input and Output Streams
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 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
What Actions Do We Have Part 1
Formatted Input/Output
Chapter 2 part #3 C++ Input / Output
Introduction to cout / cin
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ for Engineers and Scientists Second Edition
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

CPS120: Introduction to Computer Science Screen I/O Lecture 11

Console I/O console I/O refers to using the keyboard and screen as the standard input and output devices, respectively.

C-In (cin) cin is a stream of data flowing from an input device such as the keyboard (considered to be the standard input device) into your program.

Input Operations The operator >> is known as the input operator. It is also known as the extraction operator You use the input operator in statements like, cin >> numItems; which would allow the user to input a value to be stored in the variable numItems.

Multiple Input cin to allows the user to input several items with one C++ statement. The user however must type one or more spaces between each separate inputted value Example: cin >> value1 >> value2 >> value3; Inputs need to have spaces or tabs between them (can’t use a comma to delimit C++) However, this prevents you from properly giving the user individual prompt messages

Inputting Words >> can be used but once it hits a space or a tab, the rest of the string is ignored Use the get function for strings that contain ‘white space’ String ends when you press enter White space is defined as tabs or spaces Open getFunct.cpp

Complexities of Word Input Some things are done automatically with >> get does not skip over line breaks and spaces If the user enters a string longer than the length specified in the call to the get function, the remaining characters are left in the input stream Get always ignores the new line character (‘\n’) and leaves it in the stream Use the ignore function to flush the contents of the input stream cin.ignore(80, ‘\n’);

Output Operations The operator << is known as the output operator. It is also known as the insertion operator You use the output operator in statements like: cout << "Hello world";

C-Out (cout) cout is a stream which represents data flowing from one place to another. The statement: cout << "Hello world"; causes data to flow from your program to the screen. The stream, cout, leads to what your computer considers to be its standard output device (usually the screen)

Line Spacing In order to end a line in an output statement you may use the new line character, \n, instead of endl. Examples: cout << "Hello world" << '\n'; cout << "Hello world" << "\n"; cout << "Hello world\n"; These are practically equivalent to: cout << "Hello world" << endl;

Escape Sequences Other useful "escape sequences" (since the \ is the escape operator) are: \t to generate a tab \\ to print a backslash \' to print a single quote \" to print a double quote

Using setf and unsetf Each stream has format options that can be changed OPTION DESCRIPTION left Left-justifies the output right Right-justifies the output showpoint Displays decimal point and trailing zeros for floats uppercase Displays e in scientific as E showpos Displays a leading plus sign scientific Displays floating point number scientifically fixed Displays floating-point in normal notation Open formatOut.cpp

Using Format Options Format options are set immediately prior to the COUT statement float x = 24.0; cout << x << ‘\n’; // displays 24 cout.setf(ios::showpoint); cout << x << ‘\n’; // displays 24.00000 cout.unsetf(ios::showpoint); cout << x << ‘\n’; // displays 24

Using Manipulators You must include the <iomanip.h> header file at the top of your program in order to use the setprecision, setw, and other manipulators. You must use place the following compiler directive at the top of your program. #include <iomanip.h> I/O manipulators are placed directly in the output statement cout << setprecision(2) << price << ‘\n’;

Setting Precision The setprecision manipulator allows you to limit the number of digits that are displayed when a numeric data type is displayed: cout << setprecision(2) << price << '\n'; only allows the leading two digits of the value stored in the variable, price, to be displayed Open manipIO.cpp

More Precisely If the fixed format was set previously with the statement: cout.setf(ios::fixed); then the setprecision(2) manipulator would have the effect of rounding or truncating price (and all future floating-point values in the cout stream) to the hundredths place

Field Width The setw manipulator controls the width of the field when displaying a value. The statement: cout << setw(10) << umEndow << endl; sets the width of the field allocated for the variable, umEndow, to 10 characters

Formatted Output cout.setf(ios : : fixed) Print “fixed point” form, not in exponential form cout.setf(ios : : showpoint) Says to always print the decimal point cout.precison(2) Says to print out the two most significant decimal digits, after rounding to this precision