Console Applications in Visual C++ 2010

These are the directions to create a console (or text or DOS) mode application in Visual C++ 2010. The screenshots are from Visual C++ 2010 Express. However, basically the same instructions apply for Visual Studio 2010, 2012 or 2013. These directions will work for your single file or multi-file applications in CSIT 802, 832, 836, 839 and 840. These directions will not work for CSIT 861 because in that class you will be creating Windows applications.

Glossary

These steps use some terminology you may not have heard before:

  • Application - A synonym for a program. Word, Excel, Internet Explorer and Doom all are applications. 

  • Extension - The part of the file name after the period. For example, if you name a file teacherhavemercy.doc, the file extension is doc. The file extension usually indicates the application you use to open the file. For example, a doc file usually is opened in Word, an xls file in Excel, etc.

  • Source file - This is a file in which you write C++ code. It has a .cpp extension, which stands for C++.

  • Header file - This is another file that contains C++ code. It has a .h extension, which stands for header. Some header files already are written for you. Others you may write in advanced classes such as CSIT 832, 836 and 840; you likely won't in CSIT 802.

  • Project - A container for the source and header files you write.

  • Solution - A container for one or more projects. In our classes the solution almost always will contain only one project. When so, there is no practical difference difference between a solution and a project.

OK, enough terminology! Let's get started. Here's the process:

  1. Create the project
  2. Create a source file
  3. Write code
  4. Compile
  5. Run your code

Creating the Project

  1. Choose the File > New > Project menu command. This displays the New Project dialog shown in Figure 1. (Ignore the values in the Name, Location and Solution fields; they will be set in steps 4 and 5.)

Figure 1 - New Project dialog

  1. If you have Visual Studio 2010, the left pane will list languages in addition to Visual C++, so you should choose Visual C++. If you have Visual C++ 2010 Express, then Visual C++ will be your only choice. 

  2. Choose Win32 Console Application from the right pane. WARNING: Do not choose the similarly named Win32 Project or CLR Console Application. If you do, you'll have to start all over.

  3. Location will default to your My Documents\Visual Studio 2010\Projects subfolder. Usually you should use that location. If you do need to change the location, you can do so using the Browse button.

  4. In Name, type the name you choose for your project. This name also will be the Solution Name, and the name of the subfolder (in the location specified in Location) created to store your project files. Hint: Choose a logical name, such as 839A1 for the first assignment in CSIT 839.

  5. Click the OK button. This will display the Win32 Application Wizard shown in Figure 2.

Figure 2 - Win32 Application Wizard

  1. Don't click Finish. If you do you may have to start over. Instead, click Application Settings on the left. The appearance of the Win32 Application Wizard then changes to that shown in Figure 3.

Figure 3 - Win32 Application Wizard after choosing Application Settings

  1. Make sure under Application Type you choose Console Application (that choice may already be the default). Additionally, under Additional options, uncheck Precompiled Header and check Empty Project. WARNING: These are not the defaults under Additional options. Skipping this step is a common mistake and may require you to start over.

  2. Click the Finish button. Visual C++ will now create your project. This may take a few moments.

  3. Choose the View > Solution Explorer menu command to display Solution Explorer, which is shown in Figure 4.

Figure 4 - Solution Explorer

Create a Source File

You've now created a project. The next step is to write code. We'll write that code in a source file. This file has a cpp extension, which stands for C++.  In the following steps, you will create a source file, then add code to it.

  1. So far the project is empty. To add a source (*.cpp) file, choose the Project > Add New Item menu command. This will display the Add New Item dialog shown in Figure 5. You also can display the Add New Item dialog by right-clicking Source Files in Solution Explorer to display the shortcut menu shown in Figure 6, and then choosing Add > Add New Item from the shortcut menu.

Figure 5- Add New Item dialog (Default View)

Figure 6 - Shortcut Menu for Source Files

  1. Choose Code from the left. pane Choose C++ File (.cpp) from the right pane.  Generally you will not change the Location, which is the subfolder in which the project files are stored. Type the name of the new .cpp file in the Name box. I chose genghis.cpp. Then click the Add button. Figure 7 shows the new genghis.cpp file in Solution Explorer.

Figure 7 - Solution Explorer showing new .cpp file

Note: In advanced classes, you may need to add additional .cpp files, or h files. You add additional .cpp files following the above steps. You would add .h files the same way except that in Step 2 you would choose Header File (.h) instead of C++ File (.cpp).

Write Code

Now that you have added the source file, the next step is to write code in it. Double-click genghis.cpp in Solution Explorer. That will display the file in code view, where you can type code.

Figure 8 - Code View

The next step is to type code directly into the source file. The following code is derived from the Getting Started handout for my CSIT 802 class, where students who want to get on my good side write code to output, in Spanish, "Our teacher is the best!"

#include <iostream>
using namespace std;

int main ()
{            
   cout << "Nuestro maestro, el es la bestia!";
   return 0;
}

Here's the code in code view:

Figure 9 - Code

Compile

Choose File > Save All to save your work. Then compile your program from the Build menu with the Build > Compile menu command.

Build > Compile menu command

Figure 10 - Compile menu command

The Output window will display. If compilation was successful, the message displayed will be: "Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped."

Output window - successful compile

Figure 11 - Output window showing successful compilation

If you do have compile errors, then you will need to fix them. We will discuss that subject in class.

Run your Code

We're now ready to run your code. To do so, choose the Debug > Start Without Debugging menu command. (Note: Choose without, not with, debugging). If you don't see that menu command, you can add it to the menu with the Tools > Settings > Expert Settings menu command.

When you choose the choose the Debug > Start Without Debugging menu command, you may see a message that your project is out of date and asking if you want to build it. If so, just press the Yes button.

Figure 12 - Project out of date message

A console window will now display with the output: Nuestro maestro, el es la bestia!

console window

Figure 13 - Console window displaying code output

"Press any key to continue ..." is generated by Visual C++, not your code. Its purpose is to have you press a key to close the console window.

That's it! You've now created your first Visual C++ console application!