Labs for CS 1037a

Refresher on Visual Studio + Intro to cs1037lib

This lab has four parts:
  1. Learn to create a Visual Studio project for CS1037.
  2. Learn to find stuff in Visual Studio Help.
  3. Learn to use the Visual Studio debugger.
  4. Learn about 'cs1037lib', a small collection of functions to help you draw things in a window.
Begin the lab by running Microsoft Visual Studio 2010. The first time you run Visual Studio 2010, it will ask you what kind of keyboard settings you'd like. Choose the C++ settings.
On the crappy lab machines, you may have to wait 4 or 5 minutes for Visual Studio to start the first time. It will start much faster the next time.

Part A – Creating a Visual Studio project

These are the exact steps you should use to create each lab/assignment in the course.
  1. Select File→New→Project.
  2. Configure a new "Win32 Console" project exactly as shown below in red. When ready click OK.
  3. Before clicking Finish, configure your application to be created "empty". Click Finish, and Visual Studio will open your new, empty project.
  4. In Solution Explorer, right-click on Source Files and select Add→New Item. Select C++ File and "main.cpp" as the name. Click Add.
  5. Type a "hello world" program into main.cpp.
  6. Hitting Ctrl+F5 runs your program without debugging. If asked to rebuild, always say Yes.


Part B – Visual Studio Help and Documentation

Paste the code below into main.cpp, replacing your hello world program. Don't worry about what the code does for now, just try to compile it (hit F7 or select Build→Build Solution).
#include <cstdio>

void try_password(char* guess);

void main()
{
    char* hack = "abcd";
    try_password(hack)
}

void try_passwerd(char* guess)
{
    char* username = getenv("USERNAME");

    for (int i = 0; i < 4; ++i) {
        char c = username[i]^2;
        if (guess[i] != c)
            printf("CHARACTER %d IS WRONG\n", j); // print index 0..3 of mismatch
        }
    }
}
The code doesn't compile because it was written by an engineer who totally forgot C++ over the summer. There are precisely five "C++ mistakes" in the code (missing ; , stuff like that). Get the program to compile by finding these tiny mistakes.

Hint 1. Follow the advice in the "Compilation Errors" section of Getting Help.

Hint 2. One of the mistakes is that the "required header" for getenv is not included (needs #include <something>). Position the text cursor on getenv and hit F1 to get Microsoft's getenv documentation. If asked to choose "Online or Local Help", choose Online.
+ Hit F1   →  


Part C – Basic Debugging

Run your corrected program from Part B without debugging (Ctrl+F5). It complains that some characters in the current password "abcd" are incorrect. Close the window and go back to Visual Studio.

Your job in Part C is to hack try_password to figure out what 4-letter password it expects. You must use the debugger, and should not modify the code except to change the default password from "abcd".

  1. Hit F10 (Step Over) until the CPU (yellow arrow) is ready to call try_password.
  2. Hit F11 (Step Into) once to follow the CPU into the try_password function, then hit F10 until the CPU is about to test if guess[i] is correct.
  3. While the CPU is frozen, inspect variables i,guess[i], and c. Either hover the mouse over the variable you want, or enter them into the Watch window, whichever you like best.
    Clearly, on my computer, the first letter of the correct password is 'g' and not 'a'.
  4. Step through the program with F10 until you have recovered the full password.
You've essentially finished Part C when your program runs on its own (Ctrl+F5) without complaining about wrong characters. However, try to repeat Part C one more time, but instead of using F10/F11 to step line-by-line, use a breakpoint to freeze the CPU at the specific line you want.
  1. Create a breakpoint on the line that tests guess[i] is correct. There are two ways: either click the gray margin with the mouse, or hit F9 to create one wherever the cursor is.
  2. Now hit F5 (no Ctrl) to run your program with debugging. The CPU will run until it hits the breakpoint.
  3. Inspect the variables just like in Step 3, then hit F5 to let the CPU continue running. The CPU will freeze at the same breakpoint the next time around the loop, so you can watch as i becomes 0,1,2, and finally 3, and you can inspect the value of c each time.

Useful shortcut keys for debugging in Visual Studio:
Ctrl+F5 → Run program; CPU runs until program quits
F5 → Run program with debugging; CPU will freeze at any breakpoint
Shift+F5 → Quit debugging; your program is forced to quit and debugging stops
F9 → Toggle breakpoint on current line
F10 → Step over; CPU runs all code on current line, including functions
F11 → Step into; follow the CPU to the next line of code it actually executes
Shift+F11 → Step out; let the CPU finish the current function and freeze after it returns
Shift+F9 → Quickwatch; pop-up version of Watch window
Don't forget you can also hover the mouse over variables to see their values.

Part D – Introduction to cs1037lib

In CS1037 we'll ask you to use some simple functions that we provide. These functions make it easier to get keyboard/mouse input and to draw things to the screen.
  1. Download the three files below, and save them beside your main.cpp.
  2. In the Solution Explorer, click the Show All Files button. The three cs1037lib files should be visible. Add all three files to your project via right-click→Include In Project. Hit F7 to make sure it builds.
  3. Paste the code below into your main.cpp, replacing all the code from Parts B and C.
    #include "cs1037lib-window.h"
    #include "cs1037lib-image.h"
    
    void main()
    {
    	SetWindowTitle("Lab 1");
    	SetWindowVisible(true);             // Show main window 
    
    	int map = LoadImage("london.png");  // Load image from file
    	DrawImage(map,0,0);                 // Draw image at xy position (0,0)
    
    	while (!WasWindowClosed()) {        // Loop until user closes window
    		// do nothing
    	}
    
    	DeleteImage(map);                   // Free memory used by image
    }
    
    If you run this program, it will complain that "london.png" is missing. Download london.png and save it beside main.cpp.
  4. The final step of the lab is to make the above program respond to mouse clicks. Download stamp.png and lab01demo.exe, saving them beside main.cpp. Run lab01demo.exe and try clicking.
  5. Modify your program so that it behaves like lab01demo.exe. As a first step, just try to load the stamp.png image and draw it somewhere on the screen. After that's working, find the GetMouseInput function in cs1037lib-window.h and look at the example given there. It should only take 6–9 extra lines of code to make your program act like lab01demo.exe.