Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.

Similar presentations


Presentation on theme: "C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics."— Presentation transcript:

1 C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics

2 C++ for Engineers and Scientists, Second Edition2 Objectives Exception Handling The string Class Character Manipulation Methods Input Data Validation Namespaces and Creating a Personal Library Common Programming Errors

3 C++ for Engineers and Scientists, Second Edition3 Exception Handling Exception: a value, variable, or object that contains information about the error that occurred Exception handler: code designed to deal with exceptions Throwing an exception: the process of generating and passing an exception at the point where the error was detected

4 C++ for Engineers and Scientists, Second Edition4 Exception Handling (continued) Two basic types of errors, caused by –Inability to obtain a required resource –Flawed data Catch an exception: receive a thrown exception and process it Try and catch keywords are used for throwing and catching exceptions

5 C++ for Engineers and Scientists, Second Edition5 Exception Handling (continued) try keyword starts the exception handling block of code Statements that may throw an exception are placed within the braces One or more catch blocks follow the try block Each catch block catches a unique exception type, identified in parentheses At least one catch block is required for a try block

6 C++ for Engineers and Scientists, Second Edition6 Exception Handling (continued) Syntax:

7 C++ for Engineers and Scientists, Second Edition7 Exception Handling (continued)

8 C++ for Engineers and Scientists, Second Edition8 The string Class Class: a user-created data type that defines a valid set of data values and a set of operations that can be used on them Object: storage area declared for a class string class permits string literals String literal: any sequence of characters enclosed in double quotation marks; also called string value, string, constant Figure 7.1 The storage of a string as a sequence of characters.

9 C++ for Engineers and Scientists, Second Edition9 The string Class (continued) string class provides functions for declaring, creating, and initializing a string Methods: functions in a class Constructor: the method which creates and initializes a string object string header file is required to use the string class

10 C++ for Engineers and Scientists, Second Edition10 The string Class (continued)

11 C++ for Engineers and Scientists, Second Edition11 The string Class (continued)

12 C++ for Engineers and Scientists, Second Edition12 The string Class (continued) Character positions within a string are numbered starting with zero

13 C++ for Engineers and Scientists, Second Edition13 The string Class (continued) Strings can be input from the keyboard and displayed on the screen using cout, cin, and getline getline continuously accepts and stores characters from the keyboard until the terminating key is pressed If the last argument is omitted, the Enter key will terminate the input Syntax: getline(cin, strObj, terminatingChar)

14 C++ for Engineers and Scientists, Second Edition14 The string Class (continued)

15 C++ for Engineers and Scientists, Second Edition15 The string Class (continued) Using cin and getline inputs together in the same program may cause problems cin accepts the input, but leaves the newline code from the Enter key in the buffer, which will be picked up by the getline as the end of its input

16 C++ for Engineers and Scientists, Second Edition16 The string Class (continued) Three possible solutions to this problem: –Do not use cin and getline inputs in the same program –Follow the cin input with cin.ignore() –Accept the Enter key into a character variable and then ignore it

17 C++ for Engineers and Scientists, Second Edition17 The string Class (continued) string class provides many methods for manipulating strings, including –length : returns the length of the string –at : returns the character at the specified index –compare : compares two strings –empty : tests if the string is empty –erase : removes characters from the string –find : returns the index of the first occurrence of a string in an object

18 C++ for Engineers and Scientists, Second Edition18 The string Class (continued) string class methods (continued): –insert : inserts one string into another string at the specified index –replace : replaces characters in an object at the specified index –substr : returns a portion of a string –swap : swaps characters in a string with a specified object Concatenation operator ( + ) combines two strings

19 C++ for Engineers and Scientists, Second Edition19 The string Class (continued) All relational operators may be used to compare strings; ASCII or UNICODE code values are used for comparison Figure 7.5 The initial strings used in Program 7.6.

20 C++ for Engineers and Scientists, Second Edition20 The string Class (continued)

21 C++ for Engineers and Scientists, Second Edition21 The string Class (continued)

22 C++ for Engineers and Scientists, Second Edition22 The string Class (continued) Example: Inserting characters in a string string str = “This cannot be”; str.insert(4, “ I know”);

23 C++ for Engineers and Scientists, Second Edition23 The string Class (continued) Example: Replacing characters in a string str.replace(12, 6, “to”); str += “ correct”;

24 C++ for Engineers and Scientists, Second Edition24 The string Class (continued)

25 C++ for Engineers and Scientists, Second Edition25 Character Manipulation Methods Character manipulation methods require the header file string or cctype character class provides methods for character manipulation, including –Character type detection: isalpha, isalnum, isdigit, isspace, isprint, isascii, isctrl, ispunct, isgraph, isupper, islower –Character case conversion: toupper, tolower

26 C++ for Engineers and Scientists, Second Edition26 Character Manipulation Methods (continued)

27 C++ for Engineers and Scientists, Second Edition27 Character Manipulation Methods (continued) Basic character I/O methods, requiring the header file cctype, include –cout.put : put a character on the output stream –cin.get : extract a character from the input stream –cin.peek : assign the next character from the input stream to a variable without extracting it –cin.putback : pushes a character back onto the input stream –cin.ignore : ignores input characters

28 C++ for Engineers and Scientists, Second Edition28 Character Manipulation Methods (continued) When a character is requested after the user has already input some other data, get() method may pick up the Enter key instead of the new data Two ways to solve this problem: –Use a cin.ignore() after the cin.get() –Accept Enter key and do not process it

29 C++ for Engineers and Scientists, Second Edition29 Character Manipulation Methods (continued)

30 C++ for Engineers and Scientists, Second Edition30 Input Data Validation User-input validation includes checking each entered character to ensure it is the expected data type Can accept user input as a string, and then check each character for proper type Example: input of an integer number –Data must contain one or more characters –If the first character is a + or -, there must be at least one digit –Only digits from 0 to 9 are acceptable

31 C++ for Engineers and Scientists, Second Edition31 Namespaces and Creating a Personal Library Namespaces provide the ability to group a set of classes, objects, or functions under a name Syntax : namespace name { functions and/or classes in here }// end of namespace A file can contain more than one namespace

32 C++ for Engineers and Scientists, Second Edition32 Namespaces and Creating a Personal Library (continued) To use the namespace file, add a preprocessor directive containing the namespace path to the program Example: #include using namespace dataChecks; If more than one namespace is in the file, only the requested namespace will be loaded

33 C++ for Engineers and Scientists, Second Edition33 Common Programming Errors Failure to include the string header file when using string class objects Failure to remember that the newline character, ‘\n’, is a valid data input character Failure to convert a string class object using the c_str() method when converting to numerical data types

34 C++ for Engineers and Scientists, Second Edition34 Summary String literal: any sequence of characters enclosed in double quotation marks string class can be used to construct a string string class objects are used when comparing, searching, examining, or extracting a substring, or replacing, inserting, or deleting characters cin object terminates input when a blank is encountered

35 C++ for Engineers and Scientists, Second Edition35 Summary (continued) getline() method can be used for string class data input cout object can be used to display string class strings


Download ppt "C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics."

Similar presentations


Ads by Google