{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

9781111531003_PPT_ch14 - A First Book of C Chapter 14 The...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
A First Book of C++ Chapter 14 The string Class and Exception Handling
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Objectives In this chapter, you will learn about: – The string Class – Character Manipulation Methods – Exception Handling – Exceptions and File Checking – Input Data Validation – Common Programming Errors – Namespaces A First Book of C++ 4th Edition 2
Background image of page 2
The string Class Provides methods for declaring, creating, and initializing a string String literal : any sequence of characters enclosed in quotation marks • Examples: This is a string Hello World! Quotation marks identify the beginning and end of a string – Quotation marks are not stored with string A First Book of C++ 4th Edition 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The string Class (cont'd.) A First Book of C++ 4th Edition 4
Background image of page 4
string Class Functions A First Book of C++ 4th Edition 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
string Class Functions (cont'd.) String creation – Example: Program 14.1 #include <iostream> #include <string> using namespace std; int main() { string str1; // an empty string string str2("Good Morning"); string str3 = "Hot Dog"; string str4(str3); string str5(str4, 4); string str6 = "linear"; string str7(str6, 3, 3); A First Book of C++ 4th Edition 6
Background image of page 6
string Class Functions (cont'd.) – Example: Program 14.1 (cont d): cout << "str1 is: " << str1 << endl; cout << "str2 is: " << str2 << endl; cout << "str3 is: " << str3 << endl; cout << "str4 is: " << str4 << endl; cout << "str5 is: " << str5 << endl; cout << "str6 is: " << str6 << endl; cout << "str7 is: " << str7 << endl; return 0; } A First Book of C++ 4th Edition 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
string Class Functions (cont'd.) Output created by Program 14.1: str1 is: str2 is: Good Morning str3 is: Hot Dog str4 is: Hot Dog str5 is: Dog str6 is: linear str7 is: ear A First Book of C++ 4th Edition 8
Background image of page 8
string Input and Output In addition to methods listed in Table 14.1, strings can be: – Input from the keyboard – Displayed on the screen Additional methods include: cout : general-purpose screen output cin : general-purpose terminal input that stops reading when a whitespace is encountered A First Book of C++ 4th Edition 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
string Input and Output (cont'd.) Additional methods include: getline(cin, strObj) : general-purpose terminal input that inputs all characters entered into the string named strObj and stops accepting characters when it receives a newline character ( \n ) – Example: getline(cin, message) Continuously accepts and stores characters entered at terminal until Enter key is pressed – Pressing Enter key generates newline character, \n – All characters except newline are stored in string named message A First Book of C++ 4th Edition 10
Background image of page 10
string Input and Output (cont'd.) A First Book of C++ 4th Edition 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
string Input and Output (cont'd.) Sample run of Program 14.2: Enter a string: This is a test input of a string of characters. The string just entered is: This is a test input of a string of characters.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}