Lab # 2: Debugging 1

 

  1. Open up Visual C++
    1. Start/Programs/Microsoft Visual Studio 6.0/ Microsoft Visual C++ 6.0
    2. Wait while it loads
    3. Click on the close button
  2. Create a blank C++ source file
    1. File/New
    2. You should be in the Projects Tab
    3. Change to the Files Tab
    4. Select C++ Source File
    5. Under File name, enter the text prog2
    6. Click the OK button
  3. Cut and paste the following code from the browser into Visual C++
    1. To do this, click and drag over the text you want to copy
    2. Press Ctrl-C
    3. Switch to Visual C++
    4. Press Ctrl-V

 

The text to copy is:

 

#include <iostream.h>

 

int main(void)

{

            int a, b, c, d;

            e = 9;

            c == 13;

            cout << c << endl;

            return 0;

}

 

  1. Build/Compile prog2.cpp
  2. Wait
  3. The bottom window in Visual C++ should have the following text visible:

Error executing cl.exe.

 

prog2.obj - 1 error(s), 1 warning(s)

  1. An error is a mistake which will prevent a program from being compiled – in other words, the compiler will be unable to change your program from a .cpp file into a .obj file.
  2. Scroll up in the bottom window until you see the error:

c:\winnt\profiles\jwaxman\desktop\prog2.cpp(6) : error C2065: 'e' : undeclared identifier

  1. The number in parentheses is the line number in your program which is causing the problem – in this case, line 6.
  2. Double - Click on the text of the error message. ‘e’: undeclared identifier should appear in blue and white in the status bar.
  3. The error message means that the compiler saw the token e, and thought it was the name of a function or variable. However, variables and functions in C++ cannot be used unless they are declared. In class, we saw

int x, y;

This means that we are declaring x and y to be variables of type integer. In line 5 of the program, we say int a, b, c, d; but we do not declare e.

  1. Change line 5 in the program so that line 6 will not report this error. Do not edit any other mistakes in the program – that is to be left for later in this lab.
  2. Scroll down until you see how the program should look now. Do not scroll down until you are convinced that you have corrected the problem yourself. I am including the solution only to make sure we are still talking about the same program.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream.h>

 

int main(void)

{

      int a, b, c, d, e;

      e = 9;

      c == 13;

      cout << c << endl;

      return 0;

}

 

  1. Build/Compile prog2.cpp
  2. Scroll up. The full text should be:

 

--------------------Configuration: prog2 - Win32 Debug--------------------

Compiling...

prog2.cpp

C:\WINNT\Profiles\jwaxman\Desktop\prog2.cpp(7) : warning C4553: '==' : operator has no effect; did you intend '='?

C:\WINNT\Profiles\jwaxman\Desktop\prog2.cpp(5) : warning C4101: 'a' : unreferenced local variable

C:\WINNT\Profiles\jwaxman\Desktop\prog2.cpp(5) : warning C4101: 'b' : unreferenced local variable

C:\WINNT\Profiles\jwaxman\Desktop\prog2.cpp(5) : warning C4101: 'd' : unreferenced local variable

C:\WINNT\Profiles\jwaxman\Desktop\prog2.cpp(8) : warning C4700: local variable 'c' used without having been initialized

 

prog2.obj - 0 error(s), 5 warning(s)

 

  1. The compiler reported 0 errors and 5 warnings. This means that, with no errors, the compiler was able to create prog2.obj from prog2.cpp, and it indeed did so. However, you have 5 warnings. A warning will not stop the compiler from compiling. However, it warns you that it thinks that you did a bunch of stupid things, and that when you finally run the program, the result that you will get will not necessarily be what you expect. We will focus on the first warning, which is in line 7.

C:\WINNT\Profiles\jwaxman\Desktop\prog2.cpp(7) : warning C4553: '==' : operator has no effect; did you intend '='?

 

  1. Double click on the warning. It highlighted the line c = = 13; It claims that the operator double equal (= = ) has no effect here and asks if you perhaps intended the single equal sign instead.
  2. This is indeed so. Single equals is assignment, and we intended to assign 13 the the variable c. Double equals does a comparison between the value stored in c (in this case, the values in c is random junk, so it may or may not be equal to 13). If c is equal to 13, the entire expression has a value of 1; otherwise, it hase the value of 0. So, for example, if I would say cout << c = = 13; then the program would output either 1 or 0. However, the double equal makes no change to the variable c, so when I cout c in the next line I will cout a random value.
  3. Fix the program to eliminate that warning.
  4. Fix the program to eliminate all of the warnings.
  5. If you so choose, work on your swap program.