Today’s Objectives 28-Jun-2006 Announcements

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Classes and Data Abstraction Lecture 9. Objects Models of things in the real world Defined in classes  Class name is the object name Example: Library.
Chapter 10.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
Working with Strings Lecture 2 Hartmut Kaiser
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
String Class Mohamed Shehata 1020: Introduction to Programming.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
11 Introduction to Object Oriented Programming (Continued) Cats.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Strings.
Introduction to C++ (Extensions to C)
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Class string and String Stream Processing: A Deeper Look
Working with Strings Lecture 2 Hartmut Kaiser
C++, OBJECT ORIENTED PROGRAMMING
Characters, C-Strings, and More About the string Class
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
Quiz Next Monday.
Engineering 1020: Introduction to Programming Fall 2018
C++ STRINGS string is part of the Standard C++ Library
Summary of what we learned so far
Learning Objectives String Class.
Working with Strings Lecture 2 Hartmut Kaiser
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Class string and String Stream Processing
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Engineering Problem Solving with C++, Etter
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Standard Version of Starting Out with C++, 4th Edition
(Dreaded) Quiz 2 Next Monday.
character manipulation
An Introduction to STL.
getline() function with companion ignore()
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Today’s Objectives 28-Jun-2006 Announcements Midterm Exam answers will be posted on the discussion group page Review Quiz #2 and Midterm Exam Class string and String Stream Processing (Ch. 18) String assignment and concatenation Comparing strings Substrings Finding and replacing characters in a string Conversion to C-Style strings String stream processing Getting input from the keyboard with embedded white space Demo – A Simple Library Application (Continued)

Review Quiz #2 and Midterm Exam

Progress Review

So far, we have learned… Progress Review Object-Oriented Programming Basics of C++ Functions and argument passing Arrays Pointers and dynamic memory allocation C++ classes and objects Some C++ standard libraries and their namespace The STL vector class Programs that use objects to solve problems Debugging techniques Object-Oriented Design A simple software process Pseudocode algorithms UML class diagrams and use case diagrams

Next… The string class and the stringstream class Operator overloading Progress Review Next… The string class and the stringstream class Operator overloading Inheritance Polymorphism Templates Stream I/O Exception handling File processing Linked lists The g++ compiler

Class string and String Stream Processing Chapter 18

Class string and String Stream Processing (Deitel, 884–906) C++ string class is an object-oriented approach to strings #include <string> using std::string; Creating a string object string s1; string s2("Test"); string s3 = "Test"; Finding the length int len = s2.length(); //len is 4

String Assignment Assigning Accessing individual chars Class string and String Stream Processing (Deitel, 884–906) String Assignment Assigning string s1, s2 = "Test"; s1 = s2; s1.assign(s2); //Same as s1 = s2 Accessing individual chars s2[0] = 'R'; //Changed to "Rest" s2.at(0) = 'B'; //Changed to "Best"

String Concatenation Use operator + with strings and chars Class string and String Stream Processing (Deitel, 884–906) String Concatenation Use operator + with strings and chars string s1 = "Hello"; string s2 = "World"; s1 += ", "; string s3 = s1 + s2 + '!'; Use the append function string s4; s4.append( s3, 0, 5 ); cout << s4; //Prints "Hello"

Class string and String Stream Processing (Deitel, 884–906) Comparing strings C++ strings can be compared with ==, !=, <, >, <=, and >= string s1 = "Hello", s2 = "World"; if( s1 == s2 ) cout << "equal"; Also with the compare function int result = s1.compare(s2); result is 0 if s1 is equal to s2 result is a positive number if s1 is lexicographically greater than s2 result is a negative number if s1 is lexicographically less than s2 Comparing substrings int result = s1.compare( 0, 1, s2, 0, 1 ); This example compares the first char of s1 to the first char of s2

Substrings Use the substr function to extract a substring Class string and String Stream Processing (Deitel, 884–906) Substrings Use the substr function to extract a substring string s1 = "Hello, World!"; string s2 = s1.substr( 0, 5 ); Index of the starting char Length of the substring

Finding and Replacing chars Class string and String Stream Processing (Deitel, 884–906) Finding and Replacing chars Using find() string s1 = "It was the worst of times."; int pos = s1.find("worst"); if( pos == string::npos ) cout << "Not found"; else cout << "Found at position " << pos; Using replace() s1.replace( pos, 5, "best" ); Replacement string Index of the starting char Number of chars to replace

Conversion to C-Style Strings Class string and String Stream Processing (Deitel, 884–906) Conversion to C-Style Strings Use the c_str function string s1 = "Hello"; const char *ps1 = s1.c_str();

String Stream Processing Class string and String Stream Processing (Deitel, 884–906) String Stream Processing Input from strings – ostringstream class Output to strings – istringstream class Both input and output – stringstream class #include <sstream> using namespace std; Member function that returns a string stringstream ss; ss.str();

Applications of String Streams Class string and String Stream Processing (Deitel, 884–906) Applications of String Streams Use << to insert data to be printed to an ostringstream object, and then print it later, Fig. 18.11 page 903 Example: convert int or double data to a string int i = 42; stringstream ss; ss << i; string s = ss.str();

Using cin to Get Input Containing Whitespace

Get a String Containing Whitespace Using cin (Lippman) Get a String Containing Whitespace cout << "Enter your first and last name: "; const int BUFFSIZE = 1024; char buffer[BUFFSIZE]; cin.getline(buffer,BUFFSIZE); cout << buffer << endl; //Or create a string string s = string(buffer); Gets input from the keyboard up to the \n, and puts this input into the char array, including the whitespace

Potential Problem Using cin char input; cout << "Enter your selection: "; cin >> input; cin.ignore();//Sometimes needed if you use cin first cout << "Enter the title: "; const int BUFFSIZE = 1024; char buffer[BUFFSIZE]; cin.getline( buffer, BUFFSIZE ); string title = string(buffer);

Checking for Input Errors with cin

Problem: User Enters Wrong Datatype Checking for errors with cin Problem: User Enters Wrong Datatype int input; cout << "Enter the number of copies: "; cin >> input; while( !cin ){ cin.clear(); cin.ignore(); cout << "That was not an integer, try again: "; } If the user enters a char here, then the value of “input” will be unusable

Problem: User Enters Wrong Datatype Checking for errors with cin Problem: User Enters Wrong Datatype int input; cout << "Enter the number of copies: "; cin >> input; while( !cin ){ cin.clear(); cin.ignore(); cout << "That was not an integer, try again: "; } We test whether the user entered the wrong datatype, with “!cin”. If “!cin” is true, then we have to clear the cin object and ignore the next byte.

A Simple Library Application (Continued) Demo A Simple Library Application (Continued)

Library Demo Progress review TODO Implemented two classes in the first version Book class BookList class TODO Implement the Library class

References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005.