C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.

Slides:



Advertisements
Similar presentations
C++ Plus Data Structures
Advertisements

1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and 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.
SW Engineering. 2 Goals Describe the general activities in the software life cycle Describe the goals for "quality" software Explain the following terms:
1 Programming Style Files Lecture 1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY-Fredonia.
Chapter 3: Input/Output
Program Input and the Software Design Process ROBERT REAVES.
C++ Vs. Java Who will win?. Look Ma, no classes! C++ was conceived as an object-oriented extension to C C was (is) purely procedural language –Functions.
1 Chapter 4 Program Input and the Software Design Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process CS185/09 - Introduction to Programming Caldwell College.
Chapter 1 Software Engineering Principles. Lecture 2.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream 1.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 4 Program Input and the Software Design Process.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
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 CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
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.
1 Chapter 4 Program Input and the Software Design Process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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 4 Program Input and the Software Design Process
What Actions Do We Have Part 1
Copyright © 2003 Pearson Education, Inc.
Chapter 1 Software Engineering Principles
File I/O.
Lecture 5A File processing Richard Gesick.
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 with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
Standard Input/Output Stream
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 4 Program Input and the Software Design Process
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
What Actions Do We Have Part 1
Dale/Weems/Headington Program Input and the Software Design Process
Presentation transcript:

C++ plus

2 Goals Some general C++ tips

3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin Screen an ostream object named cout an ostream object named cerr used for error reports

4 C++ Tips Insertion Operator ( << ) An output (ostream) operator that takes 2 operands –The left operand is a stream expression, such as cout –The right operand is an expression describing what to insert into the output stream cout << "The book costs" << cost;

5 C++ Tips Extraction Operator ( >> ) An input (istream) operator that takes 2 operands –The left operand is a stream expression, such as cin –The right operand is a variable of simple type Operator >> attempts to extract the next item from the input stream and store its value in the right operand variable cin >> cost; The value being keyed in for cost must be the same type as that declared for variable cost

6 C++ Tips Whitespace characters Characters such as blanks, tabs, line feeds, form feed, carriage returns, and other characters that you cannot see on the screen Extraction operator >> “skips” leading whitespace characters before extracting the input value from the stream Use function get to read the next character in the input stream cin.get(inputChar); get returns one character

7 C++ Tips #include int main( ) { using namespace std; int partNumber; float unitPrice; cout << “Enter part number followed by return: “ << endl ; // prompt cin >> partNumber ; cout << “Enter unit price followed by return: “ << endl ; cin >> unitPrice ; cout << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ; return 0; }

8 C++ Tips File input/output #include provides ofstream (output file) uses << ifstream (input file) uses >> ifstream myFile; myFile.Open("Data"); internal nameexternal name Open binds internal name to external name

9 C++ Tips ofstream yourFile; yourFile.Open("DataOut"); internal name external name yourFile.Close(); closes the file properly

10 C++ Tips Statements for using file I/O #include using namespace std; ifstream myInfile; // declarations ofstream myOutfile; myInfile.open(“A:\\myIn.dat”);// open files myOutfile.open(“A:\\myOut.dat”); myInfile.close( );// close files myOutfile.close( );

11 C++ Tips What does opening a file do? –associates the C++ identifier for your file with the physical (disk) name for the file –if the input file does not exist on disk, open is not successful –if the output file does not exist on disk, a new file with that name is created –if the output file already exists, it is erased –places a file reading marker at the very beginning of the file, pointing to the first character in it

12 C++ Tips Stream failure File enters the fail state –further I/O operations using that stream are ignored –Computer does not automatically halt the program or give any error message Possible reasons for entering fail state include – invalid input data (often the wrong type) – opening an input file that doesn’t exist – opening an output file on a diskette that is already full or is write-protected.

13 C++ Tips #include using namespace std; int main( ) {// CHECKS FOR STREAM FAIL STATE ifstream inFile; inFile.open(“input.dat”);// try to open file if ( !inFile ) { cout << “File input.dat could not be opened.”; return 1; } // rest of the program return 0; }

14 C++ Tips Reading in file names ifstream inFile; string fileName cout << "Enter the name of the input file" << endl; cin << fileName; inFile.open(fileName.c_str()); Why not just fileName?