Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing."— Presentation transcript:

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing Logical Operators, char s and string s Outline 5.1 Test-Driving the Dental Payment Application 5.2 Constructing the Dental Payment Application 5.3 Introduction to char s and string s 5.4 Logical Operators 5.5 Wrap-Up

2 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Use char s to store user input as single characters. –Use string s to store user input as strings of characters. –Use logical operators to form more complex conditions.

3 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.1 Test-Driving the Dental Payment Application

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.1 Test-Driving the Dental Payment Application Figure 5.2 Error message displays when no patient name is entered. Error message Figure 5.1 Dental Payment application before input is entered.

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.1 Test-Driving the Dental Payment Application (Cont.) Figure 5.3 Error message displays when no services are selected. Error message

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.1 Test-Driving the Dental Payment Application (Cont.) Figure 5.4 Dental Payment application with input entered and total cost of services displayed.

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.2 Constructing the Dental Payment Application

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s Char s –Contain single character constant (or literal) values –These values are stored as integer values According to the ASCII character set String s –Contain strings of characters –Defined in the C++ Standard Library Must include standard library header file –Cannot be initialized with an integer or character constant Can be assigned a character constant value

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.6 Including the standard library header file. The header file enables access to classes and functions defined in the string class

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.7 Defining variables of the char and string types. Define the variables that store user input Unlike traditional c-strings, which are sequences of characters in a memory array, C++ string objects belong to a class with many built- in features to operate with strings in a more intuitive way.built- in features

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) The getline function –Gets a line of text from the input stream (first argument) and puts it in the string (second argument) Input is delimited (or terminated) at the first newline character –Necessary when string input might contain white space characters The stream extractor operator inputs up to the first white space character or newline character –For example, if the user enters “Bob Green”: Getline( cin, myString ) would store “Bob Green” cin >> myString would store “Bob” –“Green” would be left in the input stream

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.8 Prompting the user for the patient name and using the getline function to input the name into a string. Prompt the user for the patient’s name and input the response into the string variable patient

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.9 Prompting the user for and inputting responses for services performed. Prompt the user for and input services performed

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.10 Determining the total cost for the patient’s visit. Define double variable total and initialize its value to 0.0 If a service was performed, add its cost to total

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.11 Displaying the total cost in dollar format. Display total as currency

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.12 Application output with no patient name entered and no services selected. Application calculates a bill of $ 0.00 when no services are selected

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3 Introduction to char s and string s (Cont.) Figure 5.13 Application output with a service selected, but no patient name entered.

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators Figure 5.14 Adding an if statement to the main function. Verify that a patient name was entered An empty string (“”) contains no characters Member-access operator (. ) allows access to an object’s data and functions. Actually calls a getter method (function) that returns size property (attribute). string function size returns the number of characters in the string

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.15 Displaying an error message to the users. Add an error message

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.16 Adding an else part to an if statement. else statement Right brace to end the else statement

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.17 Application output without a name entered.

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.18 Application output with a name entered, but without any services selected. User must now enter a name Application calculates a bill of $ 0.00 when a name is entered but no services are selected

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Complex conditions combine multiple simple conditions AND operation evaluates to true only if both operand expressions evaluate to true Second operand is evaluated only if first operand evaluates to true (short-circuit evaluation) A truth table displays the outcome of a complex condition for all possible values for its simple condition components

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) OR operation evaluates to true if either or both operand expressions evaluate to true Second operand is evaluated only if first operand evaluates to false (short-circuit evaluation)

25 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) NOT operation reverses the truth value of the operand expression !( expression1 == expression2 ) can also be written as ( expression1 != expression2 )

26 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.22 Using multiple && logical operators. Insert a complex expression

27 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.23 Displaying an error message if no services are selected. Add an error message to the body of the if statement How could you rewrite this conditional statement to use the logical OR operator?

28 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.24 Adding an else part to the nested if statement. Add an else Right brace to end the nested else statement

29 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.25 Running the completed application with incorrect input.

30 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.4 Logical Operators (Cont.) Figure 5.26 Running the completed application with correct input.

31 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DentalPayment.cpp (1 of 4) Define variables Obtain patient name

32 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DentalPayment.cpp (2 of 4) Call string function size and determine whether the user entered a name

33 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DentalPayment.cpp (3 of 4) Use multiple logical operators to determine whether any services were selected

34 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DentalPayment.cpp (4 of 4) Format and display total

35 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 5 − Dental Payment Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 5 Questions 5.1 to 5.10. Always write the question followed by the answer. Remember to highlight the answer. Exercises 5.11, 5.12, and the Programming Challenge 5.17. For Exercises 5.11 start with C++ code written in Tutorial 5. For Exercises 5.12 start with C++ code written in Exercise 5.11. Turn in annotated source file with your own comments. Due next Wednesday


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing."

Similar presentations


Ads by Google