Please help with my first program!!

I'm a beginner of beginners to the max to C++
and I have started to go through the tutorial here on this site, and although I got my Hello World program to work, one of the programs under the second topic "Variables. Data Types." is not working for me.

I'm currently using Microsoft Visual C++ 2010 Express as my compiler.
The following is the program:

//operating with variables
#include <iostream>
using namespace std;
int main()
//declaring variables:
int a, b;
int result;
a = 5;
b = 2;
a = a + 1;
result = a - b;
//print out the result:
cout << result;
//terminate the program:
return 0;

I literally copy and pasted the program from the variables and data types section and just added the system("PAUSE"); so that I don't have to try to confirm my results in a 10th of a millisecond.

My first time, I added the program as a new file in the same project as my HelloWorld program and when I run it, for some reason, I get the output from my Hello World program but nothing from the above program.

Afterwards I tried to make it into a separate project and I came up with the following errors when I tried to build and run it:

1> operators.cpp
1>c:\users\documents\visual studio 2010\projects\operators\operators\operators.cpp(3): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\documents\visual studio 2010\projects\operators\operators\operators.cpp(24): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I'm sure this has something to do with my incompetence over the language or the compiler so far, but please, someone help me out!! Thank you!!

This error 1> Add directive to 'StdAfx.h' or rebuild precompiled header means you should add #include "stdafx.h" , to the top of your program, above #include <iostream>
After adding it, the program should compile and run, as expected.
Last edited on
Also you need no statement system("PAUSE");. You can use key combination Ctrl+F5 to make a pause before exiting the program.
Thank you it worked!
But I'm wondering why I need that line:
#include "stdafx.h"
for my program when my Hello World program didn't need it?
Also it doesn't work with the same project folder with my Hello World.
For every new program do I have to make a new project?
You need stdafx.h if you're using MS Visual Studio. Codeblock doesn't need it.
Okay, thank you everyone for the help!!
I really appreciate it!!
You need stdafx.h if you're using MS Visual Studio. Codeblock doesn't need it.

You only need to include stdafx.h if you are using precompiled headers. If you create an emty project you don't have to use them.
Topic archived. No new replies allowed.