getline() function with companion ignore()

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Getting Input in Python Assumes assignment statement, typecasts.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
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.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 3: Input/Output
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
1 Chapter 4 Program Input and the Software Design Process.
Exposure C++ Chapter VII Program Input and Output.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
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.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Program Input and the Software Design Process ROBERT REAVES.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
C++ Basic Input and Output (I/O)
Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 3: Expressions and Interactivity.
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.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Introduction to C++ Programming
Today in CS161 Week #3 Learn about… Writing our First Program
Learning Objectives String Class.
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 3: Input/Output
Introduction to cout / cin
Introduction to C++ Programming
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
Using string type variables
Dale/Weems/Headington Program Input and the Software Design Process
Introduction to cout / cin
Chapter 1 c++ structure C++ Input / Output
getline() function with companion ignore()
Presentation transcript:

getline() function with companion ignore() Inputting an entire line of data, spaces and all

Example 1: Input a string with cin >>

Example 2: Input a string with cin >>

Example 3: Simple Fix

Would you like to know more? Keep Reading!

How the >> operator works cin >> variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and press [ENTER] 2. Any leading whitespace is extracted and discarded. 3. Characters are extracted until another whitespace is found; the whitespace is not extracted. 4. The extracted characters are converted to the data type of the variable (may succeed or fail). 5. If the conversion is successful, the variable is set equal to the converted value.

Whitespace is a sequence of one or more characters consisting of: What is a Whitespace? Whitespace is a sequence of one or more characters consisting of: (additional characters depending on platform) Name C++ Literal Space ' ' Vertical Tab '\v' Tab (Horizontal) '\t' Carriage Return '\r' Newline '\n' Form Feed '\f'

What happened in Example 2? cin >> name; - the buffer was empty, so the program suspended awaiting user input. - the user typed Sam_Smith[←] (_ represents BLANK here) - the program resumed. - Sam was extracted from the buffer, leaving _Smith[←] in the buffer - Sam was assigned to the variable name (name = "Sam");

What happened in Example 2? cout << "Enter your age: "; - Enter your age is printed; no '\n' means the cursor stays on this line. cin >> age; - the buffer is NOT empty, so the program does NOT halt to allow more input. - the blank is extracted from the buffer and discarded. - Smith is extracted, leaving only the [←] in the buffer - "Smith" can not be converted to an integer; so it is discarded and the value of age is not changed.

What happened in Example 2? cout << name << " is " << age << "years old!\n"; - So name="Sam" and age=-81234234 because it was never set equal to anything. (the cin >> age should have set it equal to the user input, but could not because the user entered string instead of integer). - Sam is -81234234 years old! is printed.

Potential Problem: spaces in input string name; int age; cout << "Enter your name: "; cin >> name; cout << "Enter your age: "; cin >> age; - The program halts and waits for the user to press ENTER. - Assume the user enters Sam Smith[enter]

Potential Problem: spaces in input string name; int age; cout << "Enter your name: "; cin >> name; cout << "Enter your age: "; cin >> age; - The program halts and waits for the user to press ENTER. - Assume the user enters Sam Smith[enter] - The characters are placed in the keyboard buffer, and the cin resumes. - The chars Sam are extracted, but NO MORE, since the next char is SPACE. - Sam is placed in the variable name; [space]Smith[enter] remains in the buffer.

Potential Problem: spaces in input string name; int age; cout << "Enter your name: "; cin >> name; cout << "Enter your age: "; cin >> age; - When the second cin executes, there is already data in the buffer, so: - The program does not halt. - The [SPACE] is extracted and discarded. - Smith is extracted. Extraction halts because the next char is [ENTER] - Since Smith is a string, it can not be stored in integer age, so the program fails at this point.