Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NET
Getting Started Tutorial
In the Location Text Box on the right, type the name of the directory (homedir) where you
wish to create this project's directory to store related files. Alternately, you may browse to the
directory from a dialog box by selecting the "..." button to the right of the text box. Enter a
name for the project in the Text Box "Name". As you type, notice that the project's name is
added to the directory in a newly created message “Project will be created at homedir\name”.
The .NET will automatically create a new directory with the same name as your project in the
base directory. Select OK to set up the project
After this window you get a new window with name “Win32 Application Wizard”. In that
click the “Application Settings” option and you will get a new window. Choose console
application and empty project as shown below.
.
//
// Program: temperature.c
// Description: Read a temperature in degrees Fahrenheit and convert to Celsius
//
#include <stdio.h>
int main()
{
double f,c;
printf(“Enter degrees F:”);
scanf(“%lf”, &f);
c = (5.0/9.0)*(f-32.0);
printf(“Degrees F (%8.2lf) = Degrees C (%8.2lf) \n”, f, c);
return(0);
}
Introduction
In this tutorial we will walk through the process of using the .NET Debugger to search for and
correct bugs in a C++ program. We will learn how to do the following:
Run the program under the debugger
Watch the values of selected expressions in the watch window.
Set breakpoints, and run up to the breakpoints.
Single step through the code.
The Microsoft .NET IDE provides a powerful debugging environment that allows the
programmer to quickly and effectively detect, locate, and correct the common bugs in
programs. Like any powerful tool, it requires a certain amount of work to become comfortable
with the tool.
The Microsoft Debugger is integrated into the IDE, and provides source code level debugging.
This means that you can watch the execution of your program line by line, and check the
values of variables by their symbolic name (the name you put in the source code) rather than
looking at raw memory locations.
return 0 ;
} // End of Main
double GetAverage(int nIn[], int nMax){
In the Microsoft IDE setting a breakpoint is easy. Place the cursor on the margin of source
code you want to stop on and click the left mouse button once. You can also do the same by
selecting Debug + New Break Point (or Ctrl+B) and giving the corresponding values. You
may also set a breakpoint by right-clicking the mouse and selecting "Insert Breakpoint" from
the pop-up menu. A red dot will appear in the gray bar to the left of the line of code to indicate
the breakpoint has been set. If you press the dot of break point a second time, the breakpoint
will be removed and the dot will disappear.
If we knew where the problems were, we could skip over some lines, but since we don't, put a
breakpoint on the first line of the code - the printf statement. Now we need to RUN the code
within the IDE. Select " Debug|Start" from the menu or press on the menu bar. You can
also press F5 button. It will start to execute, then stop at the breakpoint. Notice that the output
window comes up, but there is nothing in it. The IDE windows have changed, now showing the
source code window across the top, an immediate watch window on the bottom left, and
another window on bottom right. A yellow arrow has appeared on the red dot to the breakpoint,
indicating the current line of code being executed. Next, we will look at some of the variables.
The Watch window is located on the bottom left and has three tabs: Autos, Local and Watch1.
In the Auto .NET displays variables that are of immediate interest in the program. Which
variables are displayed will change as the program executes, always showing the most recently
defined variables, values returned from functions, and changed or referenced variables.
(nCount will be some random integer which reflects the contents of that memory location at the
beginning of the program. The nValues variable looks different. It has a small plus in a box to
the left of the variable name, and its value looks like this: 0x0012ff6c. Remember, the 0x at the
beginning of values indicates that it is a hexadecimal value. Since nValues is an array of
integers, the value being displayed is the memory location where the array begins. Click on the
plus sign to the left of the variable name. The array expands below the name, showing the
indices of the array and the values for each of the integers in the array. The plus sign has also
become a minus sign. Click on the minus sign to hide the array.
There are other tabs with Locals and Watch1. Locals will show the values of local variables of
the function. In Watch1 window, you may specify which variables you want to watch. These
variables will be displayed at all times and sometimes may show an error message when the
variable is out of scope. You have greater control in this window. You can display the array
nValues the same way as in the immediate watch window, or you may display just one of the
integers by specifying it by its index. You can also display computations in this watch window.
For example, to watch how the sum of the integers in nValues changes as the program
executes, you can enter "nValues[0] + nValues[1] + nValues[2] + nValues[3] + nValues[4]"
under the name column and .NET will automatically keep track of the sum of the values.
You may type the name of the expression you want to watch directly into the watch window,
or you may highlight a variable or expression in the source code and drag it to the Watch
window. Add nValues and nCount to the watch window. Place a breakpoint on the return
statement, and then execute the program. It will run up to the break point, and stop - this time it
will display the state of the two watch variables. Next we will step through the code.
First, let's see what happens when we start the program. Run the debug program, with a
breakpoint at the first printf statement, and nCount and nValues in the watch window. The
program starts up, the output window appears and the execution stops at the breakpoint.
The output window is blank, because we haven't sent anything out yet. The Source code
window has the printf line marked because that is where we are halted. Look at the watch
window; it shows that the nCount variable is filled with a random number. Let's step into the
loop and see what happens.
On the Tool bar are four icons showing braces. One shows a blue arrow jumping over the
braces (Step Over F10) . Another shows the arrow pointing into the braces (Step Into F11)
. These are the buttons for single stepping through a program. The other two buttons are
the Step Out and Run to Cursor. These allow you to skip large blocks of code in debugging.
We'll leave these two functions for you to explore on your own. Let's use the Step Over Button
first.
Now the printf statement inside the for loop is the current line. Also notice that now the
nCount Variable has a value - it is zero, because that's where our for loop begins. Press the
Step Over button again. The prompt for the next number is displayed, since the printf statement
has executed. Step through another line of code.
The code is now stopped on the scanf statement. You will need to switch to the user window
and enter a number and press enter.
Pressing the Step Over button will take us to the beginning of the loop; pressing it again will
increment the value of nCount in the watch windows. Let's single step through another pass
through the loop. Press the Step Over buttons twice more. See what happens to the variables.
This shows you one of the errors - the data is all going into nValues [1]. Go to the source
window, and correct the line. Now recompile, and rerun the program - make sure that it is
getting the input correctly.
When you are satisfied that the input is working, you can press the run button, and the program
will run until the next breakpoint, or the end of the program.
There appears to be a couple more errors in the code. Let's address the problem with the
maximum value. To do this we'll need to use the Step Into function.
Place a breakpoint on the line that the GetMax function is called. Remove the breakpoint from
the beginning of the source code and start the program with the "Go" button. It will run
normally until it hits the breakpoint.
Press the Step Into button. Execution of the program now passes to the first line of the function
GetMax(). We can now use the Step Over button to step through the function and identify the
errors.
A word of caution: hitting the Step Into button at the wrong time may cause the IDE to load
and display either assembly language or unfamiliar code. Don't worry; all you've done is
stepped into the code for a standard function or operator such as the insertion operator. To exit
this code, press the Step Out Icon . This should return you to your code.
There are still a few errors; try tracking through the function and see what you can fix!
Final notes
Please note that the files generated by .NET are fairly large, so you'll want to delete non-source
files from your network drive periodically.
You may want to back up your programs onto floppy disk. This also will allow you to take
your program to a non-CIS lab or the instructor's office. All you need to save are the source
files.
In any case, if you already have a source file on a floppy diskette, and you want to create a
project to work on it, you can do the following steps: