Program Input and the Software Design Process ROBERT REAVES.

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
File streams Chapter , ,
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
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.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
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 3: Input/Output
Input and Output Chapter 3. 2 Chapter Topics  I/O Streams, Devices  Predefined Functions  Input Failure  Formatting Output  Formatting Tools  File.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
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 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
1 Chapter 4 Program Input and the Software Design Process.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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 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.
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
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Chapter 4 Program Input and the Software Design Process.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
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.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
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.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
1 Chapter 4 Program Input and the Software Design Process.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
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++ 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.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
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
Topic 2 Input/Output.
Chapter 3: Expressions and Interactivity.
Basic Input and Output Operations
getline() function with companion ignore()
Data Streams.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
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
Chapter 3: Input/Output
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.
Chapter 3: Expressions and Interactivity
Chapter 4 Program Input and the Software Design Process
Formatted Input, Output & File Input, Output
CHAPTER 4 File Processing.
Using string type variables
Dale/Weems/Headington Program Input and the Software Design Process
Input/Output Streams, Part 1
getline() function with companion ignore()
Presentation transcript:

Program Input and the Software Design Process ROBERT REAVES

Input Stream  The process of placing values from an outside data set into variables in a program is called input.  Computer reads data into variables.  This is from the standard input device, the keyboard.  NOTE: We will look at file input later.

Input Stream  Think of the input stream as a doorway through which characters come into your program from an input device.  Include inside of the iostream header file.  #include  cin is the stream that is associated with the standard input device.  cin >> name;  cin uses the extraction operator (>>), sometimes pronounced “get from”.  (>>) takes two operands.  Left hand: stream expression (so, this means cin)  Right hand: is a variable we store the input data in.

Input Stream  We can use the (>>) operator several times in a single input statement.  cin >> length >> width;  SAME AS:  cin >> length;  cin >> width;  The (>>) will skip leading whitespace or newlines, but terminate when reads one upon reading input.  You may confuse the (>>) operator and the (<<) operator. Just remember to use the operator that points in the direction which the data is going!

Reading Marker and the Newline Character  Reading Marker keeps track of the point in the input stream where the computer should continue reading.  This can be thought of kind of like a bookmark on your input. =)  The (>>) operator leaves the reading marker on the character following the last piece of data read.  Where does the newline character come from and what is it?

Get Function  We said that the (>>) operator skips whitespace, but what happens if we want an input to be some kind of whitespace?  With the (>>) operator that isn’t possible.  We can use the get function!  Gets the very next character in the input stream without skipping any whitespace characters.  cin.get(ch);  This is associated with the cin, or istream data type. So you must use the dot notation to make this function call.

Ignore Function  Isn’t used very much, but something you will love when you have to use it!  Ignore function is used to skip, read and discard, characters in the input stream.  cin.ignore(200, ‘\n’);  First argument to.ignore() is an int expression and the second being a char value.  int expression: amount of input characters to skip unless char value is found.  char value: skip int expression characters or until this is found.

Reading String Data  How to input a character string into a string variable. Two options.  We can use our (>>)  Stops when it reads whitespace.  string firstname;  string lastname;  cin >> firstname >> lastname;  This is a widely used for input purposes, but it has a drawback.  Cannot be used to input a string with blanks in it.

Getline Function  Getline function does not skip leading whitespace characters and continues until it teaches the newline character.  Doesn’t use dot notation, requires two arguments.  First being your istream or cin in this case, second being your string variable.  getline(cin, string);  Reads and stores the entire input line, embedded blanks and all.  NOTE: newline character is consumed, but not stored into your string.

File Input and Output  Ifstream and ofstream are included inside the fstream header file.  #include  We have learned how to use each one of these with cout and cin. We can now define our own input and output file streams using the ifstream and ofstream type!