CSIS
9781111531003_PPT_ch14

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

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

A First Book of C++ Chapter 14 The string Class and Exception Handling
Image of page 1

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

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
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
Image of page 3

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

The string Class (cont'd.) A First Book of C++ 4th Edition 4
Image of page 4
string Class Functions A First Book of C++ 4th Edition 5
Image of page 5

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

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
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
Image of page 7

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

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
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
Image of page 9

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

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
Image of page 10
string Input and Output (cont'd.) A First Book of C++ 4th Edition 11
Image of page 11

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

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.
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '14
  • Kristina Shroyer
  • Subroutine, Enter key, C++ 4th Edition

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern