Lab # 2: Debugging 1
- Open
up Visual C++
- Start/Programs/Microsoft
Visual Studio 6.0/ Microsoft Visual C++ 6.0
- Wait
while it loads
- Click
on the close button
- Create
a blank C++ source file
- File/New
- You
should be in the Projects Tab
- Change
to the Files Tab
- Select
C++ Source File
- Under
File name, enter the text prog2
- Click
the OK button
- Cut
and paste the following code from the browser into Visual C++
- To
do this, click and drag over the text you want to copy
- Press
Ctrl-C
- Switch
to Visual C++
- 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;
}
- Build/Compile
prog2.cpp
- Wait
- The
bottom window in Visual C++ should have the following text visible:
Error executing cl.exe.
prog2.obj - 1 error(s), 1
warning(s)
- 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.
- Scroll
up in the bottom window until you see the error:
c:\winnt\profiles\jwaxman\desktop\prog2.cpp(6)
: error C2065: 'e' : undeclared identifier
- The
number in parentheses is the line number in your program which is causing
the problem – in this case, line 6.
- Double
- Click on the text of the error message. ‘e’: undeclared identifier
should appear in blue and white in the status bar.
- 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.
- 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.
- 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;
}
- Build/Compile
prog2.cpp
- 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)
- 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 '='?
- 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.
- 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.
- Fix
the program to eliminate that warning.
- Fix
the program to eliminate all of the warnings.
- If you
so choose, work on your swap program.