Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Advertisements

Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Chapter 10.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chapter 2 Basic Elements of Fortan
Chapter 15.
Chapter 9: Arrays and Strings
Chapter 2: Introduction to C++.
C++ for Engineers and Scientists Third Edition
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Chapter 3 Assignment and Interactive Input
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Chapter 12: Adding Functionality to Your Classes.
Chapter 12: Exception Handling
計算機程式語言 Lecture 09-1 國立臺灣大學生物機電系 9 9 Completing the Basics.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Chapter 9 I/O Streams and Data Files
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
CPS120: Introduction to Computer Science Functions.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
1 Original Source : and Problem and Problem Solving.ppt.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 15 Strings as Character Arrays
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ for Engineers and Scientists Second Edition
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
A First Book of ANSI C Fourth Edition Chapter 9 Character Strings.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Objectives You should be able to describe: Interactive Keyboard Input
Chapter 2 Assignment and Interactive Input
A First Book of ANSI C Fourth Edition
Copyright © 2003 Pearson Education, Inc.
Input and Output Chapter 3.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 3: Input/Output
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Chapter 9: Completing the Basics

In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character manipulation functions – Input data validation – Namespaces and creating a personal library – Common programming errors Objectives C++ for Engineers and Scientists, Fourth Edition

Exception Handling C++ for Engineers and Scientists, Fourth Edition Traditional C++ approach to error handling uses a function to return a specific value to indicate specific operations Latest C++ compilers have added a technique designed for error detection and handling referred to as exception handling When an error occurs while a function is executing, an exception is created An exception is a value, a variable, or an object containing information about the error at the point the error occurs

Exception Handling (continued) C++ for Engineers and Scientists, Fourth Edition Throwing an exception: Process of generating an exception In general two fundamental types of errors can cause C++ exceptions –Those resulting from inability to obtain a required resource, over which the programmer has no control –Errors than can be checked and handled, over which the programmer has control

Exception Handling (continued) C++ for Engineers and Scientists, Fourth Edition Table 9.1 Exception-Handling Terminology

General syntax of code required to throw and catch and exception: Exception Handling (continued) C++ for Engineers and Scientists, Fourth Edition

Exceptions and File Checking C++ for Engineers and Scientists, Fourth Edition Error checking and processing with exception handling is used extensively in C++ programs that use one or more files

Exceptions and File Checking (continued) C++ for Engineers and Scientists, Fourth Edition A rigorous check is usually required when opening output file –If it exists, file will be found –If it does not exist, file will be created In some cases, file can be opened for input and, if file is found, a further check can be made to ensure that user explicitly approves overwriting it

Opening Multiple Files C++ for Engineers and Scientists, Fourth Edition To open two files at the same time, assume you want to read data from a character-based file one character at a time and write this data to a file

Opening Multiple Files (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.2 The file copy stream structure

The string Class C++ for Engineers and Scientists, Fourth Edition The string class permits string literal values String literal: Any sequence of characters enclosed in quotation marks By convention, first character in string is always designated as position zero This position value is also referred to as both the character’s index value and its offset value

The string Class (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.3 The storage of a string as a sequence of characters

string Class Functions C++ for Engineers and Scientists, Fourth Edition string class provides a number of functions for declaring, creating, and initializing a string In earlier versions of C++, process of creating a new object is referred to as instantiating an object The methods that perform the tasks of creating and initializing are called constructor methods, or constructors, for short

string Class Functions (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.4 The character positions of the string Hot Dog

String Input and Output C++ for Engineers and Scientists, Fourth Edition In addition to string being initialized with constructors listed in Table 9.2, strings can be input from the keyboard and displayed on the screen

String Input and Output (continued) C++ for Engineers and Scientists, Fourth Edition Table 9.3 string Class Input and Output

String Input and Output (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.5 Inputting a string with getline()

String Input and Output (continued) C++ for Engineers and Scientists, Fourth Edition

Caution: The Phantom Newline Character C++ for Engineers and Scientists, Fourth Edition Seemingly strange results can happen when cin input stream and getline() function are used together to accept data, or when cin is used by itself to accept characters Don’t mix cin with getline() inputs in same program (preferred solution) Follow cin input with a call to cin.ignore() Accept Enter key in a character variable and then ignore it

Caution: The Phantom Newline Character (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.6 Typed characters are first stored in a buffer

String Processing C++ for Engineers and Scientists, Fourth Edition Strings can be manipulated using the string class functions or the character-at-a-time functions described in Section 9.4 Most commonly used function in Table 9.4 is length(), which returns number of characters in the string Two string expressions can be compared for equality using the standard relational operators

String Processing (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.7 The initial strings used in Program 9.9

String Processing (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.8 Initial storage of a string object Figure 9.9 The string after the insertion

String Processing (continued) C++ for Engineers and Scientists, Fourth Edition Figure 9.10 The string after the replacement Figure 9.11 The string after the append

Character Manipulation Functions C++ for Engineers and Scientists, Fourth Edition In addition to string functions provided by string class, the C++ language provides several useful character class functions Function declaration (prototype) for each function is contained in the header file string or cctype, which must be included in any program using these functions

Character I/O C++ for Engineers and Scientists, Fourth Edition Table 9.6 Basic Character I/O Functions (Require the header file cctype )

Character I/O (continued) C++ for Engineers and Scientists, Fourth Edition Table 9.6 Basic Character I/O Functions (Require the header file cctype ) (continued)

Phantom Newline Character Revisited C++ for Engineers and Scientists, Fourth Edition

Phantom Newline Character Revisited (continued) C++ for Engineers and Scientists, Fourth Edition

Phantom Newline Character Revisited (continued) C++ for Engineers and Scientists, Fourth Edition

A Second Look at User-Input Validation C++ for Engineers and Scientists, Fourth Edition Sign of well-constructed, robust program: –Code that validates user input and ensures that program doesn’t produce unintended results caused by unexpected input User-input validation: Basic technique for handling invalid data input and preventing seemingly innocuous code from producing unintended results –Validates entered data during or after data entry and gives the user a way of reentering data if it is invalid

Input Data Validation C++ for Engineers and Scientists, Fourth Edition Validating user input is essential Successful programs anticipate invalid data and prevent it from being accepted and processed A common method for validating numerical input data is accepting all numbers as strings After string is validated it can be converted to the correct type

Input Data Validation (continued) C++ for Engineers and Scientists, Fourth Edition Table 9.7 C-String Conversion Functions

A Closer Look: Namespaces and Creating a Personal Library C++ for Engineers and Scientists, Fourth Edition First step in creating a library is to encapsulate all specialized functions and classes into one or more namespaces and then store the complete code (with or without using a namespace) The syntax for creating a namespace:

C++ for Engineers and Scientists, Fourth Edition A Closer Look: Namespaces and Creating a Personal Library (cont’d)

C++ for Engineers and Scientists, Fourth Edition A Closer Look: Namespaces and Creating a Personal Library (cont’d)

C++ for Engineers and Scientists, Fourth Edition

Common Programming Errors C++ for Engineers and Scientists, Fourth Edition Forgetting to include string header file when using string class object Forgetting that newline character, ‘\n’, is a valid input character Forgetting to convert string class object by using c_str() function when converting string class objects to numerical data types

Summary C++ for Engineers and Scientists, Fourth Edition String literal is any sequence of characters enclosed in quotation marks –Referred to as a string value, a string constant, and, more conventionally, a string String can be can be constructed as an object of the string class string class is commonly used for constructing strings for input and output purposes Strings can be manipulated by using functions of the class they’re objects of or by using the general purpose string and character functions

Summary (continued) C++ for Engineers and Scientists, Fourth Edition cin object tends to be of limited usefulness for string input because it terminates input when a blank is encountered For string class data input, use the getline() function cout object can be used to display string class strings