microsoft visual c++ 6 - people | computer science ...people.cis.ksu.edu/~babu/cis209/lab1.doc ·...

16
Microsoft Visual Studio .NET Getting Started Tutorial Creating a Simple Console Application This tutorial is a very brief introduction to the Microsoft Visual Studio .NET Integrated Development Environment (IDE). It takes a simple step-by-step walk-through of the steps required to develop a console application with .NET and the Visual Studio IDE. Step 1. Starting Microsoft Visual Studio .NET Open the All Programs pop-up menu from the Start menu and you will find Microsoft Visual Studio .NET in the Microsoft group. An empty window will be displayed with pull down menus across the top.

Upload: phamlien

Post on 18-Apr-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

Microsoft Visual Studio .NET

Getting Started Tutorial

Creating a Simple Console Application

This tutorial is a very brief introduction to the Microsoft Visual Studio .NET Integrated Development Environment (IDE). It takes a simple step-by-step walk-through of the steps required to develop a console application with .NET and the Visual Studio IDE.

Step 1. Starting Microsoft Visual Studio .NETOpen the All Programs pop-up menu from the Start menu and you will find Microsoft Visual Studio .NET in the Microsoft group. An empty window will be displayed with pull down menus across the top. Select File + New to display the “New” Dialog Box.

Step 2. Creating a Project All programming in .NET requires the creation of a project to store associated files and project settings. From the “New” Dialog Box, and select the “Project” window. Expand the "Visual

Page 2: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

C++ Projects" tree in the Project Types window, select Win32 for the Project Type.(I tried Managed C++ Empty Project also and it worked- this is an extension of VC++ to support .NET features.)

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.

Page 3: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will
Page 4: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

.

Step 3 - Creating your source file Now the .NET willl return to the startup screen, but two new tabs will now be shown in the rightmost window -- "Solution Explorer" and "Class View". If you don’t see them you can go to View menu and click on the required name. You will need to create new source files or add some existing source files to your project. Select "Project + Add New Item" from the main menu (or simply press Ctrl+Shift+A). This will bring up the "Add New Item" Dialog Box again, and in the Categories Select “C++” and select the “C++ File”, then enter a name for the new file. In the example (shown below), we entered the name “temperature.c”.

Press open to create the blank file.

Step 4 - Enter the source codeNow you can enter the code for our simple temperature conversion program from class:

// // Program: temperature.c // Description: Read a temperature in degrees Fahrenheit and convert to Celsius // #include <stdio.h>

Page 5: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

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); }

Step 5 - Compile, link, and executeTo compile and link the program, select “Build + Build Solution” or “Rebuild Solution” from the menu. Then, run the program by selecting “Debug + Start Without Debugging” or press Ctrl + F5 button. Now enter the values required and execute the program. If you press start pr Ctrl + F5, then the output window will disappear before you can see the output.

Microsoft Visual Studio .NET Debugger

Debugging a simple program with the Microsoft Visual C++ DebuggerThis will be a step-by-step walk through of the process of debugging a simple program with Microsoft Visual Studio .NET.

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.

Page 6: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

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.

Let's get started.

Setting up the ProjectThe project should be set up in the same manner as the Introduction project. Make certain that you have selected a Managed C++ Empty Project. Name the Project Debug.

The Source Code Enter the following for the source code. debug.cpp There are a few errors in the code, but let's use the debugger to find them. ////////////////////////////////////////////////////////////////////////// This program reads five numbers from the keyboard, and prints out the// average and the maximum value// Programmed for CS201// Date : Sep 19, 1997// Programmer: James P. Gunderson///////////////////////////////////////////////////////////////////////#include <stdio.h>#include <stdlib.h>// Global Constants#define MAX 5// Function Prototypesdouble GetAverage( int nIn[], int nMax) ;int GetMax( int nIn[], int nMax ) ;int main() { int nValues[MAX] ; int nCount ; // Display a prompt: printf("Enter five numbers: "); // First we read in the numbers. for ( nCount = 0; nCount < MAX ; nCount++ ){ printf("Enter the next number : " ); scanf("%d",&nValues[1]) ; }

// Now we echo the input back to the user for ( nCount = 1; nCount < MAX ; nCount++){ printf("Value [ %d] is : %d\n",nCount,nValues[nCount]); }

// Now lets call a function to get the average: printf("The average is %lf\n", GetAverage(nValues, MAX )); printf("The maximum number is %d\n", GetMax(nValues, MAX ));

return 0 ;

Page 7: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

} // End of Maindouble GetAverage(int nIn[], int nMax){ double temp = 0.0 ; int i; for ( i = 0 ; i < nMax; i++) temp += nIn[i] ; temp /= nMax ; return temp ;} // End of GetAverage

int GetMax( int nIn[], int nMax){ int nBiggest = nIn[0] ; int i; for ( i = 1; i < nMax; i++) if (nBiggest > nIn[i]) nBiggest = nIn[i] ; return nBiggest ;} // End of GetMax()///////////////////////////////////////////////////////////////////////////// END OF LISTING//////////////////////////////////////////////////////////////////////////

Running the DebuggerAfter you enter the code (remember: if you spot the errors do not correct them), compile and link the code. There are no compiler or linker errors, so if you get any you will need to find and fix them. We should now be ready to run. First just run the program, and enter the number 2, 4, 6, 8, and 10. The first thing that you should see is that these are not the numbers that the program echoes back to you! What happened? What's wrong? Let's use the debugger to find out. The window should look like this.

Page 8: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

Setting up Breakpoints.A breakpoint is any point in the executable code where the execution stops, allowing the programmer to see what is going on inside the program as it runs. Breakpoints allow you to run through portions of the code where there are no problems, so that you can spend your time focusing on the areas that need to be fixed.

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.

HINT: Use a breakpoint to stop your program before it exits!

Using the Watch Windows.One of the most powerful features of the debugger is the ability to look at the state of the variables as the program executes. This way the programmer can see if the variables are changing the way that they are intended to change, and to see if the program is doing the things that were intended. Microsoft provides different watch windows to display the state of 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.

Page 9: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

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.

Stepping through the Code.The .NET IDE allows you to step through the code in two different ways. You can execute one line at a time, stepping into each function call, or you can run the functions without the scanning their execution. We will look at examples of both.

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

Page 10: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

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.

Single Stepping through your codePress the Step Over button. The arrow showing the current line of execution will move to the next line of executable code. It stops on the beginning of the for loop. You will see the "Enter five numbers" text appear in the output window. We were stopped BEFORE the line executed, so by single stepping, we caused the computer to execute that one line. Note in the immediate watch window, two new variables have appeared with a bent arrow in a box to the left of the names. The bent arrow indicates a return value, in this case from the execution of the insertion of the character string and end into the standard output stream (printf). Since these functions are provided with the IDE, we should be able to safely ignore these messages. Nothing else has changed so let's press it again.

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.

You should see these changes:

The number you typed shows up in the user Window. The Source code window shows that control has gone to the closing brace at the bottom of the loop. In the Immediate Watch window, nValues [1] appears with a new value. Note how the changed value is red. If you expand the array in the Watch window, you can see the value of nValues [1] has changed.

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.

Page 11: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

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.

Explore on your own

There are still a few errors; try tracking through the function and see what you can fix!

Ending the session

When you exit .NET, it will ask you if you want to save any changed files. Usually, you want to say yes. Exit the compiler now using File + Exit.

Returning to your program

To work on this program later, you start up the compiler again. Go to the File menu and choose Open project. (Note that this is different than plain Open, which will only open a file). Double-click on the project directory and select the solution file. This will cause the desktop that existed when you exited the project to reappear.

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.

Page 12: Microsoft Visual C++ 6 - People | Computer Science ...people.cis.ksu.edu/~babu/cis209/Lab1.doc · Web viewDebugging a simple program with the Microsoft Visual C++ Debugger This will

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:

1. Create a project as in steps 1-3 above. 2. Copy the source file to the directory you created. 3. Add the source file to your project: go to the Project menu, choose Add to Project,

choose Files, find and select the copy of the source file (not the original), and click OK.