Visual C++

If you want to use Visual C++, you can use either Visual Studio Community or Visual C++ 2022 or 2019 or 2017.

Microsoft Visual C++ 2022 is installed on the SEASnet lab machines and the SEASnet Remote Desktop (which you can remotely access from any Windows or Mac computer with a high-speed Internet connection), so you don't have to install Visual Studio if you want to use it.

Note that you will be able to log into that SEASnet Remote Desktop only from

Obtaining Visual C++ 2022

If you would like to use Visual C++ 2022 on your own computer running Windows, you can download Visual Studio Community for free from Microsoft and install it. (As a student, you have free access to other Microsoft developer tools through their Azure Dev Tools for Teaching program.) Early on, you'll be presented with a window showing "Workloads". Be sure to check Desktop development with C++. Click Install to continue.

Note that although you can install trial versions of Visual Studio Professional or Visual Studio Enterprise if you want to, using them for CS 31 or CS 32 would be like using a flamethrower to light a candle when a match would do.

Creating a Visual C++ Project

Visual C++ is designed to support developers of large systems, so to use it as we will initially to write simple one-file programs may seem like overkill. However, over time you will come to appreciate some features (the debugger, especially).

Here is a way to set up a Visual C++ project to compile and run a simple one-file program. A number of these steps may have alternate ways to achieve the same end. These instructions are for Visual C++ 2022; the steps for Visual C++ 2019 or 2017 are similar.

  1. Start up the Visual C++ IDE (Integrated Development Environment). If you don't have a shortcut on the Desktop, then from the start menu select Visual Studio 2022 (with the purple squared-off infinity sign icon, not the folder icon. The first time you fire it up after installation, it may take half a minute to get you to the start page. If it asks you to sign in, you may just click "Skip and add accounts later" if you wish. If it says "Personalize your Visual Studio experience", for the Development Settings, select Visual C++ from the drop-down list. (If you're not offered Visual C++ as a choice but see other languages, you forgot to specify Desktop development with C++ as a Workload during installation when you installed Visual Studio Community.) Click the Start Visual Studio button. Then on the Get started page, click the Create a new project box on the right.

  2. On the Create a new project page, scroll down to Empty Project (Start from scratch with C++ for Windows. Provides no starting files.) and select it. Click Next.

  3. On the Configure your new project page, enter for Project name, a project name such as "hello". So that you can more easily find your projects later, you might want to change the Location to something like "C:\CS31" on your own machine, or "Z:\CS31" on a SEASnet machine. On a SEASnet machine, even if you don't use a simple location like "Z:\CS31", at least change the initial part of the location from "\\smb02.seas.ucla.edu\yourusername\" to "Z:\". In general, to make reading error messages easier in step 8, a short location like "C:\CS31" or "Z:\CS31" is better than a long one like "\\smb02.seas.ucla.edu\yourusername\my documents\visual studio 2022\projects", and a short project name like "hello" or "P1" is better than a long one like "example CS31 project". Click Create. (If you're on a SEASnet machine you may then see a dialog saying "Found a suitable location...". It's harmless to click OK to dismiss it.)

    Caution: If you are on a SEASnet machine, be sure to use a project location path starting with "Z:". If you don't, when you try to run your program, a paranoid SEASnet security application will put up a dialog box saying "Unable to start program ... This program is blocked by group policy."

  4. (This step is unnecessary for Visual Studio 2022 or 2019.) Select Project / Properties. In the Property Pages dialog, in the left panel, select Configuration Properties / Linker / System. In the right panel, select SubSystem, and in the drop down list to its right, Console (/SUBSYSTEM:CONSOLE). Click OK.

  5. From the top menu bar, select Project / Add New Item... In the Add New Item dialog, select C++ File (.cpp) in the middle panel if there is a middle panel. Enter a source file name, such as "hello.cpp", in the text box below. Click Add.

  6. Edit the hello.cpp file in the window that appears. Type in a program like

    	#include <iostream>
    	using namespace std;
    
    	int main()
    	{
    	    cout << "Hey, this really works!" << endl;
    	}
    
  7. From the Debug menu, select Start Without Debugging. This will save your source file, compile it, and run the resulting executable if there were no build errors. (If you launch your program by finding and double-clicking on the .exe file, or if you're running Visual Studio 2017 and you selected Start Debugging or you forgot to do step 4, your console window screen will disappear as soon as your program finishes executing, which you don't want.)

    If you see a dialog box that says "This project is out of date: ... Would you like to build it?", check the "Do not show this dialog again" checkbox before clicking Yes.

  8. If your program has any compile-time or link errors, a dialog box will appear telling you there were build errors. Check the "Do not show this dialog again" checkbox before clicking No, you don't want to continue. The Output window below your source file will list the errors. Double-click the first error message to see what it's referring to in your code. Fix the error. You can click on and fix other errors as well, but be aware that sometimes one mistake produces multiple error messages, so your first fix may resolve those problems as well. Go back to step 7.

  9. If your program runs, its output will appear in a new console window that pops up. When your program finishes, that console window will then display text like

    	Z:\hello\hello\Debug\hello.exe (process 1234) exited with code 0.
    	Press any key to close this window . . .
    

    Those two lines are produced by the Visual Studio environment, not by you; we will never consider them to be part of the output of your program. Press any key to dismiss the console window.