Today’s Objectives

24
1 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) 28-Jun-2006

description

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 - PowerPoint PPT Presentation

Transcript of Today’s Objectives

1

Today’s ObjectivesToday’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)

28-Jun-200628-Jun-2006

2

Review Quiz #2and Midterm Exam

Review Quiz #2and Midterm Exam

3

Progress ReviewProgress Review

4

So far, we have learned…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

Progress ReviewProgress Review

5

Next…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

Progress ReviewProgress Review

6

Class string andString Stream Processing

Class string andString Stream Processing

Chapter 18

7

Class stringClass string

C++ string class is an object-oriented approach to strings

#include <string>using std::string;

Creating a string objectstring s1;string s2("Test");string s3 = "Test";

Finding the lengthint len = s2.length(); //len is 4

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

8

String AssignmentString Assignment

Assigningstring s1, s2 = "Test";s1 = s2;s1.assign(s2); //Same as s1 = s2

Accessing individual charss2[0] = 'R'; //Changed to "Rest"s2.at(0) = 'B'; //Changed to "Best"

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

9

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

10

Comparing stringsComparing strings

C++ strings can be compared with ==, !=, <, >, <=, and >=string s1 = "Hello", s2 = "World";if( s1 == s2 ) cout << "equal";

Also with the compare functionint 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 substringsint result = s1.compare( 0, 1, s2, 0, 1 );• This example compares the first char of s1 to the first char of s2

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

11

SubstringsSubstrings

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

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

12

Finding and Replacing charsFinding 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" );

Index of the starting char

Number of chars to replace

Replacement string

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

13

Conversion to C-Style StringsConversion to C-Style Strings

Use the c_str function

string s1 = "Hello";const char *ps1 = s1.c_str();

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

14

String Stream ProcessingString 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();

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

15

Applications of String StreamsApplications 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();

Class string and String Stream Processing (Deitel, 884–906)Class string and String Stream Processing (Deitel, 884–906)

16

Using cin to Get Input Containing WhitespaceUsing cin to Get Input Containing Whitespace

17

Get a String Containing WhitespaceGet 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);

Using cin (Lippman)Using cin (Lippman)

Gets input from the keyboard up to the \n, and puts this input into the char array, including the whitespace

18

Potential ProblemPotential 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);

Using cinUsing cin

19

Checking for Input Errorswith cin

Checking for Input Errorswith cin

20

Problem:User Enters Wrong Datatype

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;}

Checking for errors with cinChecking for errors with cin

If the user enters a char here, then the value of “input” will be unusable

21

Problem:User Enters Wrong Datatype

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;}

Checking for errors with cinChecking for errors with cin

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

DemoDemo

A Simple Library Application(Continued)

23

Library DemoLibrary Demo

Progress review• Implemented two classes in the first version

– Book class– BookList class

TODO• Implement the

Library class

24

ReferencesReferences

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