C++ Basic Input and Output (I/O)

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
CSC 200 Lecture 2 Matt Kayala 1/25/06. Lecture Objectives More detailed intro to C++ Variables, Expressions, and Assignment Statements Console Input/Output.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
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.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 1 C++ Basics Copyright © 2012 Pearson Addison-Wesley. All rights reserved.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
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.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CPS120: Introduction to Computer Science Formatted I/O.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
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 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.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
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.
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.
C Formatted Input/Output
C++ First Steps.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
TMF1414 Introduction to Programming
Output Stream Formatting
Lecture 2-3 C++ Basic Constructs
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Standard Input/Output Streams
Standard Input/Output Streams
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
Let’s all Repeat Together
What Actions Do We Have Part 1
Chapter 2 part #3 C++ Input / Output
C++ Programming Basics
Introduction to cout / cin
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Introduction to C++ computers and programming CMPT 129 © Janice Regan, CMPT 129 2017

C++ Basic Input and Output (I/O) To print to your computer’s screen (console) or the read data typed into your keyboard you will need to use objects from the C++ iostream library. To access the objects in the iostream library you must use a #include pre-processor directive to include their declarations: #include <iostream> This directive tells C++ to use appropriate library so we can use the I/O objects cin, cout, cerr, endl needed for simple input and output © Janice Regan, CMPT 129 2017

C++ Spaces inside “ “ cout << fixed << setprecision(2) << "The balance is $" << balance << endl; cout << “the interest is “ << percent << “ % ” ; cout << setprecision(5) << percent/100 << endl; PRINTS The balance is $78.50 The interest is 12.45% 0.12450 Thee is no space between $ and the final quote in code, there is a space between % and the final “ in the code. Therefore, in the output $ is immediately followed by the value (no space) but % is followed by a space. © Janice Regan, CMPT 129 2017

C++ endl When your code executes the instruction Consider the output window (the window in which the output from your program, and the input from your keyboard is displayed.) When your code executes the instruction cout << endl; The cursor in the output window is moved to the beginning of the next line © Janice Regan, CMPT 129 2017

C++: Moving to a new output line C++ style: endl C++ can also use C style: "\n" escape sequence for the char "newline“ cout << "Hello World" << endl; cout << "Hello World\n"; In C++ both the statements print string literal "Hello World" to display, then move to next line © Janice Regan, CMPT 129 2017

C++ Output Window Output What can you print to your computer’s output window using the insertion operator <<? string name=“John”; const double scale_factor = 2.2; cout << scale_factor // Prints 2.2, the value of constant scale_factor cout << name; // Prints John, the name stored in variable name cout << “This is a string literal”; // Prints This is a string literal Each cout above prints the value of one variable or constant © Janice Regan, CMPT 129 2017

C++ Output window Output You can print the values of more than one variable or constant in a single cout statement cout << “the number of games was ” << numGames << endl << “our team won “ << numWin << “ games” << endl; Given that the variables have the values numGames = 23 and numWin = 15 This single cout statement prints the number of games was 23 our team won 15 games to the output window © Janice Regan, CMPT 129 2017

C++ 3 Equivalent Outputs cout << “the number of games was ” << numGames << endl << “ out team won “ << numWin << “ games” << endl; // One cout statement printing many values in succession cout << “the number of games was ” << numGames << endl; cout << “ out team won “ << numWin << “ games” << endl; // Two cout statements, each printing half the values cout << “the number of games was ” ; cout << numGames; cout << endl; cout << “ out team won “ ; cout << numWin ; cout << “ games” ; cout << endl; // each value printed using a single cout statement © Janice Regan, CMPT 129 2017

Number of digits after decimal There are two ways in C++ to specify how many digits to print after the decimal point Method 1 uses setprecision() and Uses <iostream> library only Uses “magic formula” to set number of digits after the decimal point The other method uses manipulators Uses <iomanip> and <iostream> libraries Syntax is simpler to remember © Janice Regan, CMPT 129 2017

Explain the “magic formula” cout.setf(ios::fixed); //Tells C++ to display output in fixed format xxx.yyy cout.setf(ios::showpoint); //Tells C++ to always print the decimal point //Tells C++ to print trailing 0’s cout.precision(3); //Tells C++ to print 3 digits after the decimal point Sets your program so all floating point numbers printed after these statements will print in fixed format with 3 digits after the decimal point Adding another cout.precision(N) statement later in your code will cause all floating point numbers after that statement to print with N digits after the decimal point … © Janice Regan, CMPT 129 2017

Extend the “magic formula” cout.unsetf(ios::fixed); //Tells C++ to stop displaying in fixed format xxx.yyy cout.setf(ios::scientific); //Tell C++ to start displaying in scientific notation xxx.yyy You can switch from fixed point notation to scientific notation (or from scientific notation to fixed point notation) First you must unset the flag telling C++ to print using floating point. Then you must set the flag to tell C++ to print using scientific notation. (Or unset scientific and set fixed) Results will not be predictable if you set both flags simultaneously © Janice Regan, CMPT 129 2017

What else can you specify? The programmer can explicitly specify how C++ should format the numbers output by their programs using the <iomanip> library In particular you can specify The number of digits printed The number of digits printed after the decimal point width of field (how many spaces to leave for a value) Fixed point 123.4 or scientific notation 1.234 E2 © Janice Regan, CMPT 129 2017

C++ Manipulators fixed scientific setw() setprecision() left right Used to control how output is formatted Require user to include <iomanip> library fixed scientific setw() setprecision() left right © Janice Regan, CMPT 129 2017

Manipulators: fixed + setprecision() fixed, prints number as fixed point, xx.yyy setprecision(2) indicates 2 digits after the decimal point Continues to use precision 2 and fixed until told to change in another cout command cout << "$" << fixed << setprecision(2) << 10.3 << " "<< "$" << 20.512 << endl; Prints $10.30 $20.51 © Janice Regan, CMPT 129 2017

Manipulators: scientific + setprecision() scientific and setprecision() manipulators: setprecision(4) indicates 4 digits after the decimal point cout << “population is " << scientific << setprecision(4) << mypop << endl; If mypop has value 333445.0 cout statement prints: population is 3.3345 e+005 scientific prints number in scientific notation, xxx.yy Ezz © Janice Regan, CMPT 129 2017

Changing Format: Example cout << fixed << setprecision(2) << "the interest is " << 12.33333 << "% or $”<< dollars << endl; cout << scientific << setprecision(4) << "The amount is " << 111.234567 << endl; Format changes after scientific is used. The code above print the following when dollars has value 33.12: the interest is 12.34% or $33.12 The amount is 1.1123e+002 © Janice Regan, CMPT 129 2017

Manipulator: setw() setw() Note: setw() affects only NEXT value output Sets the width in characters of the output field By default output will be right justified in the output field If the output has the same number of characters as the number or spaces available within the field it will fill the field If the output has fewer characters as the number or spaces available within the field it will by default be right justified within the field Note: setw() affects only NEXT value output Must include setw() manipulator before each item output © Janice Regan, CMPT 129 2017

Manipulator Example: setw() setw() manipulator: cout << “xxxxxxxxxxxxxxxxxxx”; cout << endl<<"Start" << setw(5) << 10 << setw(4) << 20 << setw(6) << 30; Prints: xxxxxxxxxxxxxxxxxxxx Start 10 20 30 © Janice Regan, CMPT 129 2017

Manipulator Example: left, right left and right manipulators: cout << fixed << left << setw(20) << interest << " %"; cout << endl << setw(20) << balance << endl; cout << setw(20) << right << 2333345.45678; Prints: xxxxxxxxxxxxxxxxxxxxxx 2.300000 % 23.456000 2333345.456780 © Janice Regan, CMPT 129 2017

Input Using cin Differences: cin >> num; ">>" (extraction operator) points opposite Think of it as "pointing toward where the data goes" Object name "cin" used instead of "cout" No literals allowed for cin Must input "to a variable" cin >> num; Waits on-screen for keyboard entry Value entered at keyboard is "assigned" to num © Janice Regan, CMPT 129 2017

Prompting for Input: When using console output and keyboard input always "prompt" user for input cout << "Enter number of dragons: "; cin >> numOfDragons; No endl in cout means that the prompt "waits" on same line for keyboard input as follows: Enter number of dragons: Waits here for input © Janice Regan, CMPT 129 2017

Prompting for Input: When using console output and keyboard input always "prompt" user for input cout << "Enter number of dragons: “ << endl; cin >> numOfDragons; endl in cout means that the prompt "waits" on next line for keyboard input as follows: Enter number of dragons: Waits here for input © Janice Regan, CMPT 129 2017

Variable types and input Be careful to give the correct type of data when responding to the prompt in a program. Items from the keyboard will be converted but this may still not give the results you expect Because of overflow Because of conversion © Janice Regan, CMPT 129 2017

Input the correct type of data Think of all the information you type in as a continuous stream of characters If you are reading an integer and you read a decimal point you will stop reading at the decimal point because the decimal point is not a part of an integer The next time you read you will begin with the decimal point left over from the last input © Janice Regan, CMPT 129 2017

Incorrect input examples int one; double two; Int three; cout << "enter an integer"; cin >> one; cout << "enter a double "; cin >> two; cin >> three; cout << one << “ “ << two << “ " << three; enter an integer4 enter a double 2.4 enter an integer7 4 2.4 7 enter an integer5.6 enter a double enter an integer6 5 0.6 6 enter an integer12 enter a double 44 enter an integer88 12 44 88 © Janice Regan, CMPT 129 2017

Incorrect input examples int one; int two; int three; cout << "enter an integer "; cin >> one; cout << "enter integer 2"; cin >> two; cout<<"enter integer 3"; cin>>three; cout << "XX" << endl << one << " "<< two <<three; enter an integer 4.44 enter integer 2enter integer 3XX 4 -858993460-858993460 enter an integer hello -858993460 -858993460-858993460 © Janice Regan, CMPT 129 2017

C++ output by example Consider the code sampleIO.cpp posted with this set of notes. Lets discuss the examples given in the code © Janice Regan, CMPT 129 2017