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.