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 3 – Inventory Application: Introducing Variables,

Similar presentations


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

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables, Input, Memory Concepts and Arithmetic Outline 3.1 Test-Driving the Inventory Application 3.2 Variables 3.3 Performing Stream Input Using cin 3.4 Performing a Calculation and Displaying the Result 3.5 Memory Concepts 3.6 Arithmetic 3.7 Using the Debugger: Breakpoints 3.8 Web Resources 3.9 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: –Define variables. –Enable your applications to accept input that the user types at the keyboard. –Use arithmetic operators. –Use cin to store user input in an int. –Apply basic memory concepts using variables. –Use the precedence rules of arithmetic operators. –Set breakpoints to debug applications.

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

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.1 Test-Driving the Inventory Application (Cont.) Figure 3.1 Inventory application with quantities entered.

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.1 Test-Driving the Inventory Application (Cont.) Figure 3.2 Calculating the total in the Inventory application. Result of calculation

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.2 Variables A variable is like a box in the computer's memory. It is used to hold a piece of data, for instance, a number or a string. A variable has a name, so that it can be referred to, like a box with a label on it. The name… –Can contain letters, digits and underscores (Number_5 is valid) –Cannot begin d with a digit (“4thVariable” is invalid) –Cannot be the same as a C++ reserved keyword (“true” is invalid) –Identifiers are case sensitive (“A” and “a” are different) A variable has a piece of data placed in it using an equals sign: x = 2.6 Naming Conventions –Use underscore for word separator (my_variable) –Alternative is to use camel case (myVariable) for word seperator

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.2 Variables (Cont.) Figure 3.3 Defining variables in the main function. Variable definitions int type –This tells C++ that the variable x is going to be an integer. –This means a whole number, which can be positive (any value up to +32767), negative (down to -32768) or zero. Defining a Variable Specify a variable type (integer) and size (16 bits) Can also specify an optional initialization value

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundamental Data Types When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers. Source: http://www.cplusplus.com/doc/tutorial/variables/http://www.cplusplus.com/doc/tutorial/variables/

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundamental Data Types (Cont.) Summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one.

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundamental Data Types (Cont.) The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems. For other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.2 Variables (Cont.) On May 2000 the stdint.h header file was added to the ANSI C standard library – “a set of typedefs that specify exact-width integer types, together with the defined minimum and maximum allowable values for each type, using macros.” http://en.wikipedia.org/wiki/Stdint.hhttp://en.wikipedia.org/wiki/Stdint.h As stdint.h is not shipped with Visual Studio C++ products prior to Visual Studio 2010, 3rd- party implementations are available.

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.3 Performing Stream Input Using cin Stream input –Stream extraction operator >> –Input stream object cin –Use both together to retrieve input from the keyboard –Example: cin >> integer1 (“ cin inputs a value into integer1 ”)

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.3 Performing Stream Input Using cin (Cont.) Figure 3.5 Prompt user for input and read value. Prompt user for number of cartons Place value entered by user in the cartons variable

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.3 Performing Stream Input Using cin (Cont.) Figure 3.6 Prompt user for input and store the value in the items variable. Prompt user for items per cartons Place value entered by user in the items variable

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.4 Performing a Calculation and Displaying the Result Figure 3.7 Using multiplication in the Inventory application. Multiplication operator * Assignment operator = Multiply the integer values and store the result in result

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.4 Performing a Calculation and Displaying the Result (Cont.) Figure 3.8 Displaying the result. Display the result Stream manipulator endl Displays a new line and flush the output buffer Input stream operator cin also flushes the output buffer Concatenating stream insertion operations Use multiple stream insertion operators in a single statement cout << “\nThe total number of items is: “ << result << endl; or cout << “\nThe total number of items is: “ << result << ‘\n’ << flush;

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.4 Performing a Calculation and Displaying the Result (Cont.) Figure 3.9 Executing the completed Inventory application.

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Inventory.cpp (1 of 2) Use the int keyword to define variables inside a function Assigns user input to variable

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Inventory.cpp (2 of 2) Assigns user input to variable Calculate a result

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.5 Memory Concepts Figure 3.11 Memory location showing name and value of variable cartons. Every variable has a name, size, type and value

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.5 Memory Concepts (Cont.) Figure 3.12 Memory locations after assigning values to cartons and items. Writing to a memory location is destructive

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.5 Memory Concepts (Cont.) Figure 3.13 Memory locations after a multiplication operation. Reading a value from a memory location is nondestructive

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.6 Arithmetic x y + and – unary operators allow programmers to specify positive ( +9 ) and negative ( -19 ) numbers Numbers are positive by default ( 9 is the same as +9 ) Integer division truncates (discards the remainder) Arithmetic expressions must be written in straight-line form

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.6 Arithmetic (Cont.) Precedence is the order in which operators are applied Redundant parentheses make expressions easier to read

25 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints Logic errors (also called bugs) –Do not prevent code from compiling –Cause applications to produce erroneous results Breakpoint –Pauses execution before executing a specified line –If a breakpoint is placed on a non-executable line, execution is paused before the next executable line –Add a breakpoint by: Clicking inside the margin indicator bar or Right-clicking a line of code and selecting Insert Breakpoint

26 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.16 Setting Solution Configuration to Debug. Down arrow Solution Configuration ComboBox

27 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.17 Setting two breakpoints. Breakpoints Margin indicator bar Margin indicator bar is the gray bar left of the code field When the debugger pauses execution at a breakpoint, the application is said to be in break mode

28 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.18 Inventory application running.

29 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.20 Application execution suspended at the first breakpoint. Yellow arrow Next executable statement The yellow arrow points to the line containing the next statement to execute

30 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.21 Displaying a variable value by placing the mouse pointer over a variable name. Quick Info box displays the result variable’s value

31 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.22 Setting a breakpoint at line 31 prevents the application from exiting immediately after displaying its result.

32 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.23 Application output. When there are no more breakpoints, application will execute to completion

33 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.7 Using the Debugger: Breakpoints (Cont.) Figure 3.24 Disabled breakpoint. Disabled breakpoint Disabling a Breakpoint Right-click the line of code and select Disable Breakpoint Removing a Breakpoint –Click the maroon circle in the margin indicator bar or –Right-click the line of code and select Remove Breakpoint How to Single-step your program.

34 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 3 − Inventory Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 3 Questions at the end of the Chapter. Always write the question followed by the answer. Remember to highlight the answer. Exercises 3.11, 3.12, and 3.13 Due next Wednesday

35 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3.8 Web Resources Richard Bowles’ Beginners Guide to C++ www.cplusplus.com www.cppreference.com www.cpp-home.com www.cprogramming.com www.research.att.com/~bs/C++.html msdn.microsoft.com/visualc


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

Similar presentations


Ads by Google