1 / 24

Today’s Objectives

28-Jun-2006. Today’s Objectives. 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

Download Presentation

Today’s Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 28-Jun-2006 Today’s Objectives • 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)

  2. Review Quiz #2and Midterm Exam

  3. Progress Review

  4. Progress Review So far, we have learned… • 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

  5. 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

  6. Class string andString Stream Processing Chapter 18

  7. Class string and String Stream Processing (Deitel, 884–906) Class string • 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

  8. 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"

  9. 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"

  10. 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

  11. 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

  12. 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

  13. 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();

  14. 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();

  15. 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();

  16. Using cin to Get Input Containing Whitespace

  17. 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

  18. Using cin Potential Problem 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);

  19. Checking for Input Errorswith cin

  20. 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: "; cin >> input; } If the user enters a char here, then the value of “input” will be unusable

  21. 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: "; cin >> input; } 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.

  22. Demo A Simple Library Application(Continued)

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

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

More Related