1. Writing a simple program
Now you will write a simple program which will help you to understand the key elements of a program. Let’s start by creating a new project by clicking on File in the toolbar and selecting New – Start a New Project. You can also open a New Project by pressing a key combination Ctrl+Shift+N on your keyboard. When you open a New Project the window should appear and in this window select Win32 Console Application. Under a name in New Project window type Program0001 or give a name that you want. After naming your program click OK. The New Project window is shown in next figure.
Figure 1.1 – New Project window
After you click OK the New Project window will close and Win32 Application Wizard will appear. In a Win 32 Application Wizard click Next.
Figure 1.2 – Win32 Application Wizard
After you click Next in Win32 Application Window you will see Application Settings
Figure 1.3 – Application settings
The default setting is a Console application that will include a fi le containing a default version of main(), but you’ll start from the most basic project structure, so choose Empty project from the set of additional options and click the Finish button. Now you have a project created, but it contains no files at all. Because you are starting with an empty project, you need to check that the project properties are set appropriately. You can display the project properties dialog by selecting Properties from the Project menu or just pressing Alt+F7. Select Configuration in the left pane, and then General in the left pane if it is not already highlighted. In the right pane, you’ll see a list of properties, so select Character Set in the Project Defaults set. To the right of the property name, you’ll see a drop - down list, from which you should select Not Set. The default value for this property for a Win32 console implies you are going to use the Unicode character set in the application. This will cause the compiler to look for wmain , which won ’ t be present. If you fail to reset this project property, then you will get an error message during the build operation because wmain cannot be found. Having set the Character Set property, you are ready to create the program code. You’ll start by adding a new source fi le to the project, so right - click Source Files in the Solution Explorer pane and select the Add ➪ New Item . . . menu option. The Add New Item dialog, similar to that shown in Figure 1.4, displays.
Figure 1.4 – Add a New Project
OK so now type this program code, into a Console Application Window.
// Program0001.cpp : Defines the entry point for the console application.
using namespace std;
int _tmain(int argc, _TCHAR* argv)
int a, b, c;
cout << "Type the value of the first variable a:"<< endl;
cin >> a;
cout << "Type the value of the second variable b:" << endl;
cin >> b;
c = a + b;
cout << "The result is: " << c << endl;
cout << " I have created my first program in less than 5 minutes." << endl;
The preceding example is intended to illustrate some of the ways in which you can write C++ statements and is not a model of good programming style. The editor will check the code as you type. Anything it thinks is not correct will be underlined with a red squiggle, so look out for these. If you see one, it usually means that you have mistyped something somewhere. Since the file is identified by its extension as a file containing C++ code, the keywords in the code that
the editor recognizes will be colored to identify them. You will be able to see if you have entered Int where you should have entered int, because Int will not have the color used to highlight keywords in your source code. If you look at the Solution Explorer pane (press Ctrl+Alt+L to display it) for your new project, you ’ ll see the newly created source fi le. Solution Explorer will always show all the fi les in a project. You can display the Class View pane by selecting from the View menu or by pressing Ctrl+Shift+C. This consists of two panes, the upper pane showing global functions and macros within the project (and classes when you get to create a project involving classes), and the lower pane presently empty.
After you debug it press Ctrl+F5 and you run your program. The result of a program is shown in this few next figures.
Figure 1.5 – type the value of the first variable a
Figure 1.6 – Type the value of the second variable b
Figure 1.7 – The result is
So you see it’s not that hard to create a program. Of course this was easy one but the more you learn the more sophisticated program will get.