Obtaining Visual Studio Community Edition

Visual Studio is already installed on the computers in Briggs 419. If you have a windows computer and would like to install the software on your own computer, you can download the installer from this link. (There are several different versions of Visual Studio available. Make sure that you select Visual Studio 2017 Community Edition.)

On the main installation screen in the installer, click the option for Desktop development with C++. This is the only option you will need.

Creating a Project

Visual Studio organizes the source code files for your programs into projects. For each program you create you will create a project and project folder.

  1. Start up Visual Studio and select New/Project... from the File menu.
  2. In the new project dialog click the Win32 template category on the left and click on Win32 Console Application in the center pane. At the bottom of the window type a name for your project and click the Browse button to select a location for the project folder. Place the project folder in a location that you can find again later.
  3. Click OK. Click Next on the first screen of the Application Wizard. In the second screen of the Application Wizard click the Empty Project option and click Finish to dismiss the dialog.
  4. You should now have a new, empty project. Right-click on the Source Files folder in the Solution Explorer pane and select the option Add/New Item.
  5. In the dialog that appears, click the Code template category on the left and C++ file in the center panel. At the bottom in the name field, type the name 'main.cpp'.
  6. You should now have a new, empty source code file open in the Visual Studio editor. Copy and paste the code from the Source Code section below into that empty source code file.
  7. Since the example program needs to work with a couple of text files you will next want to add those to the project. To add a text file to the project, select the project itself in the solution explorer:
  8. Right-click on the project in the solution explorer and select the option Add/New Item... In the dialog that appears, select the Utility category and then select the option to make a new, empty text file.
  9. Type a list of numbers into numbers.txt. Then, create a second empty text file named "copy.txt" in your project.
  10. To run your program, click on Start Without Debugging in the Debug menu. That will compile and run your program and pop up a console window to show you the program's output.

Source Code

Below is the source code for our first example program. Copy and paste this code into the source code editor pane in Visual Studio.

#include <iostream>
#include <fstream>

int main(int argc, const char * argv[]) {
    
    std::ifstream in;
    in.open("numbers.txt");
    
    std::ofstream out;
    out.open("copy.txt");
    
    int x;
    while(in >> x) {
        out << x << std::endl;
    }
    
    in.close();
    out.close();
    
    std::cout << "Done!\n";
    
    return 0;
}