Formatted Input, Output & File Input, Output

Slides:



Advertisements
Similar presentations
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Advertisements

File streams Chapter , ,
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Chapter 5: Loops and Files.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 Chapter 3 Expressions and Interactivity. 2 Topics 3.1 The cin Object 3.2 Mathematical Expressions 3.3 When You Mix Apples and Oranges: Type Conversion.
Chapter 3: Input/Output
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting 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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Chapter 3: Input/Output
Expressions and Interactivity. 3.1 The cin Object.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
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.
Program Input and the Software Design Process ROBERT REAVES.
1 Chapter 4 Program Input and the Software Design Process.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Lecture 3 Expressions, Type Conversion, Math and String
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Chapter 3: Expressions and Interactivity
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
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 5 Input and Output Streams
Expressions and Interactivity
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 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Standard Version of Starting Out with C++, 4th Edition
CHAPTER 4 File Processing.
Programming Fundamental-1
Presentation transcript:

Formatted Input, Output & File Input, Output

Formatting Output Can control how output displays for numeric, string data: size position number of digits Used to control how an output field is displayed Requires iomanip header file

Formatting Output: setw Used to ouput the value of an expression in a specific number of columns. setw(x): outputs the value of the next expression in x columns. The output is right-justified: For example: if you specify the number of columns to be 8 and the output requires only 4 columns, then the first four columns are left blank. If the number of columns specified is less than the number of columns required by the output, the output automatically expands to the required number of columns.

Example

left manipulators #include <iostream> #include <iomanip> using namespace std; int main() { int x=15; int y = 7634; cout<<left; cout<<setw(5)<<x<<setw(7)<<y<<setw(8)<<"Warm“ <<endl; cout<<right; system("PAUSE"); return 0;}

Floating point number output formatting fixed: use to output floating point numbers in a fixed decimal format. cout<<fixed;

Example #include <iostream> #include <iomanip> using namespace std; int main() { double x=15.674; double y = 235.73; double z = 9525.9874; cout<<fixed; cout<<x<<endl<<y<<endl<<z<<endl; system("PAUSE"); return 0; } The Output: 15.674000 235.730000 9525.987400

showpoint Manipulator To force the output to show the decimal point and trailing zeros. #include <iostream> #include <iomanip> using namespace std; int main() { double x=15.674; double y = 235.73; double z = 9525.9874; cout<<showpoint; cout<<x<<endl<<y<<endl<<z<<endl; system("PAUSE"); return 0;} The output: 15.6740 235.730 9525.99

Example 2 #include <iostream> #include <iomanip> using namespace std; int main() { double x=15.674; double y = 235.73; double z = 9525.9874; cout<<fixed<<showpoint; cout<<x<<endl<<y<<endl<<z<<endl; system("PAUSE"); return 0; } The output: 15.674000 235.730000 9525.987400

setprecision Manipulator To control the number of significant digits (or precision) of the output, i.e., the total number of digits before and after the decimal point. However, when used with fixed, it specifies the number of floating-points (i.e., the number of digits after the decimal point). Without fixed, and the setprecision is set to a lower value, it will print floating-point value using scientific notation.

setprecision Manipulator General syntax: setprecision(n) where n: the number of significant digits or the number of floating-point (if used with fixed )

Example #include <iostream> #include <iomanip> using namespace std; int main() { double x=156.74,y=235.765,z=9525.9874; cout<<setprecision(5) << x<<endl; cout<<setprecision(3) << x<<endl; cout<<setprecision(2) << x<<endl; cout<<setprecision(1) << x<<endl; cout<<fixed << setprecision(2); cout<<x<<endl<<y<<endl<<z<<endl; return 0; } The output: 156.74 157 1.6e+002 2e+002 235.76 9525.99

Example 2 #include <iostream> #include <iomanip> using namespace std; int main() { double x=15.674; double y = 235.73; double z = 9525.9874; cout<<setprecision(2); cout<<x<<endl<<y<<endl<<z<<endl; return 0; } The output: 16 2.4e+002 9.5e+003

Stream Manipulators

In-Class Exercise Do Lab 6, Exercise 2, No. 3 (pg. 76)

Formatted Input Can format field width for use with cin Useful when reading string data to be stored in a character array: const int SIZE = 10; char firstName[SIZE]; cout << "Enter your name: "; cin >> setw(SIZE) >> firstName; cin reads one less character than specified with the setw() manipulator

#include <iostream> #include <iomanip> using namespace std; int main() { const int SIZE = 10; char firstName[SIZE]; cout << "Enter your name: "; cin >> setw(SIZE) >> firstName; cout<<firstName<<endl; system("PAUSE"); return 0; } The output: Enter your name: abcdefghij abcdefghi The output: Enter your name: abc def abc

#include <iostream> #include <iomanip> using namespace std; int main() { string name; cout << "Enter your name: "; cin >> name; cout<<name<<endl; system("PAUSE"); return 0; } The output: Enter your name: abc def abc

Formatted Input To read an entire line of input, use cin.getline(); const int SIZE = 81; char address[SIZE]; cout << "Enter your address: "; cin.getline(address, SIZE); cin.getline takes two arguments: Name of array to store string Size of the array

In-Class Exercise Write C++ program to solve the flow chart.

Formatted Input To read a single character: Use cin: Use cin.get(): char ch; cout << "Strike any key to continue"; cin >> ch; Problem: will skip over blanks, tabs, <CR> Use cin.get(): cin.get(ch); Will read the next character entered, even whitespace

In-Class Exercise Refer to Lab 6, Exercise 2 No. 1 in pg. 74. What will be displayed if the following characters are entered in Program 6.2 & 6.3? Explain the program output with the following input. AV TY

Formatted Input Mixing cin >> and cin.get() in the same program can cause input errors that are hard to detect To skip over unneeded characters that are still in the keyboard buffer, use cin.ignore(): cin.ignore(); // skip next char cin.ignore(10, '\n'); // skip the next // 10 char. or until a '\n'

Hand Tracing a Program Hand trace a program: act as if you are the computer, executing a program: step through and ‘execute’ each statement, one-by-one record the contents of variables after statement execution, using a hand trace chart (table) Useful to locate logic or mathematical errors

In-Class Exercise Trace the following programs void main(){ //Prog 6_41 int x, y, z; x =10; y = 17; z = x + y; y = y - x; cout<<"x: "<<x<< " y: “ <<y<<" z: "<<z; x = y * z; z = x / 20; y = z % x; cout<<"\nx: "<<x<< " y: “ } void main(){//Prog 6_42 int n, m, x, y; m=10; n=m*2/(m+2); m%=n+2; cout <<"n: "<<n; cout <<"\nm: "<<m; x=4; y=x*2+10%3-1*x; x*=(y/m); cout<<"\nx: "<< x; cout<<"\ny: "<<y; }

Introduction to File Input and Output Can use files instead of keyboard, monitor screen for program input, output Allows data to be retained between program runs Steps: Open the file Use the file (read from, write to, or both) Close the file

Files: What is Needed Use fstream header file for file access File stream types: ifstream for input from a file ofstream for output to a file fstream for input from or output to a file Define file stream objects: ifstream infile; ofstream outfile;

Opening Files Create a link between file name (outside the program) and file stream object (inside the program) Use the open member function: infile.open("inventory.dat"); outfile.open("report.txt"); Filename may include drive, path info. Output file will be created if necessary; existing file will be erased first Input file must exist for open to work

Using Files Can use output file object and << to send data to a file: outfile << "Inventory report"; Can use input file object and >> to copy data from file to variables: infile >> partNum; infile >> qtyInStock >> qtyOnOrder;

Closing Files Use the close member function: infile.close(); outfile.close(); Don’t wait for operating system to close files at program end: may be limit on number of open files may be buffered output data waiting to send to file

In-Class Exercise Do Lab 6, Exercise 2 No. 2 (i-iv) in pg. 75-76.