In the preceding section, you learned that the Quick Info feature allows you to examine the value of a variable. In this section, you will learn how to use the Locals window to assign new values to variables while your program is running. You will also use the Watch window to examine the value of more complex expressions.
1. |
Inserting breakpoints. Set a breakpoint at line 25 in the source code by clicking in the margin indicator bar to the left of line 25 (Fig. L.12). Set another breakpoint at line 28 of the code by clicking in the margin indicator bar to the left of line 28. Figure L.12. Setting breakpoints at lines 25 and 28. |
2. |
Starting debugging. Select Debug > Start. Type 13 at the Enter withdrawal amount for account1: prompt (Fig. L.13) and press Enter so that your program reads the value you just entered. The program executes until the breakpoint at line 25. Figure L.13. Entering withdrawal amount before breakpoint is reached. |
3. |
Suspending program execution. When the program reaches line 25, Visual C++ .NET suspends program execution and switches the program into break mode (Fig. L.14). At this point, the statement in line 22 (Fig. L.3) has input the withdrawalAmount that you entered (13), the statement in lines 2324 has output that the program will attempt to withdraw money and the statement in line 25 is the next statement that will be executed. Figure L.14. Program execution suspended when debugger reaches the breakpoint at line 25. (This item is displayed on page 1372 in the print version) |
4. |
Examining data. Once the program has entered break mode, you can explore the values of your local variables using the debugger's Locals window. To view the Locals window, select Debug > Windows > Locals. The values for account1 and withdrawalAmount (13) are displayed (Fig. L.15). Figure L.15. Examining variable withdrawalAmount. |
5. |
Evaluating arithmetic and boolean expressions. Visual Studio .NET allows you to evaluate arithmetic and boolean expressions using the Watch window. There are four different Watch windows, but we will be using only the first window. Select Debug > Windows > Watch > Watch 1. In the first row of the Name column (which should be blank initially), type (withdrawalAmount + 3) * 5, then press Enter. Notice that the Watch window can evaluate arithmetic expressions. In this case, it displays the value 80 (Fig. L.16). In the next row of the Name column in the Watch window, type withdrawalAmount == 3, then press Enter. This expression determines whether the value contained in withdrawalAmount is 3. Expressions containing the == symbol are treated as boolean expressions. The value returned is false (Fig. L.16), because withdrawalAmount does not currently contain the value 3. Figure L.16. Examining the values of expressions. |
|
|
6. |
Resuming execution. Select Debug > Continue to resume execution. Line 25 executes, debiting the account with the withdrawal amount, and the program is once again suspended at line 28. Select Debug > Windows > Locals. The updated account1 value is now displayed in red to indicate that it has been modified since the last breakpoint (Fig. L.17). The value in withdrawalAmount is not in red because it has not been updated since the last breakpoint. Click the plus box to the left of account1 in the Name column of the Locals window. This allows you to view each of account1's data member values individually. Figure L.17. Displaying the value of local variables. |
7. |
Modifying values. Based on the value input by the user (13), the account balance output by the program should be $37. However, you can use the debugger to change the values of variables in the middle of the program's execution. This can be valuable for experimenting with different values and for locating logic errors in programs. You can use the Locals window to change the value of a variable. In the Locals window, click the Value field in the balance row to select the value 37. Type 33, then press Enter. The debugger changes the value of balance and displays its new value in red (Fig. L.18). Figure L.18. Modifying the value of a variable. |
8. |
Setting a breakpoint at the return statement. Set a breakpoint at line 29 in the source code by clicking in the margin indicator bar to the left of line 29 (Fig. L.19). This will prevent the program from closing immediately after displaying its result. If you do not set this breakpoint, you will not be able to view the program's output before the console window closes. Figure L.19. Setting a breakpoint at line 29. (This item is displayed on page 1374 in the print version) |
9. |
Viewing the program result. Select Debug > Continue to continue program execution. Function main executes until the return statement on line 29 and displays the result. Notice that the result is $33 (Fig. L.20). This shows that the previous step changed the value of balance from the calculated value (37) to 33. Figure L.20. Output displayed after modifying the account1 variable. |
10. |
Stopping the debugging session. Select Debug > Stop Debugging. This will close the Command Prompt window. Remove all remaining breakpoints. |
In this section, you learned how to use the debugger's Watch and Locals windows to evaluate arithmetic and boolean expressions. You also learned how to modify the value of a variable during your program's execution.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography