getline() function with companion ignore()

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

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.
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.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
1 Chapter 4 Program Input and the Software Design Process.
February 14, 2005 Characters, Strings and the String Class.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
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.
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.
String and Character Manipulation.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
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.
Basic concepts of C++ Presented by Prof. Satyajit De
C++ Basic Input and Output (I/O)
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
getline() function with companion ignore()
Chapter 2 part #3 C++ 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.
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
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
What Actions Do We Have Part 1
Reading from and Writing to Files
Chapter 2 part #3 C++ Input / Output
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
Reading from and Writing to Files
Odds and Ends.
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.

String as a Sequence of Char Remember that a string is actually a sequence (list) of chars. Individual chars in a string can be accessed using the [ ] operator. The first char is [0], the second [1], etc. string name = "Hello"; Output: cout << name[0] << endl; cout << name[1] << endl; cout << name[2] << endl; cout << name[3] << endl; cout << name[4] << endl;

Converting to Upper Case Use the toupper function to convert a char to upper case (does not work on string!) string name = "Hello"; Output: char x = name[0]; // 'H' char y = name[1]; // 'e' x = toupper(x); // 'H' y = toupper(y); // 'E' cout << x << endl; cout << y << endl;