views:

77

answers:

3

I want to write simple C++ code for adding two integers (in a command line window). How do I do this in Visual Studio 2010?

(I know the code for adding the numbers.. I don't know how to prepare the files)

@Armen Tsirunyan

I did just that, then I added the following code to the c++ file:-

#include <iostream.h>

main()
{
    int integer1, integer2, sum;
    cout << "Please enter first number";
    cin >> integer1;

    cout << "Please enter second number";
    cin >> integer2;

    sum = integer1 + integer2;
    cout << "The sum of the two numbers is: " << sum << endl;

    return 0;
}

but I got a message telling me that the project was out of date and I would like to build it, I put "yes"... then I got an error!

A: 

Open Visual Studio

Menu => File -> New -> Project

Select in the Dialog which pops up 'Visual C++' -> 'Win32' There select 'Win32 Console Application'

Enter name and location for the project you want to create and then press OK, in the next dialog press 'Finish' and you can edit the file.

Edit:

#include <iostream>

int main()
{
    using std::cin; 
    using std::cout;
    using std::endl;
    int integer1 = 0, integer2 = 0, sum = 0;
    cout << "Please enter first number";
    cin >> integer1;

    cout << "\nPlease enter second number";
    cin >> integer2;

    sum = integer1 + integer2;
    cout << "\nThe sum of the two numbers is: " << sum << endl;

    return 0;
}
Vinzenz
You also need a `using std::endl;` in there too ;)
ChrisF
you are right, damn it codepad.org -.- they must have there some 'using namespace std' somewhere :P
Vinzenz
+1  A: 

Open Visual Studio

File -> New Project Select Visual C++ -> Win32 -> Win32 Console Application Enter name and location press OK

Select Application Settings. Remove the check from Precompiled headers. Add the check for empty project. Click Finish

Click Project -> Add New Item -> C++ file

Code and enjoy :)

Armen Tsirunyan
A: 

C++ source files are simply plain text files; no special preparation is required.

In VC++ 2010 you can create a project in a number of ways, but you need to start with File->New->Project (or just select "New Project" on the start page). If you don't want Visual Studio to add all sorts of MS specific code to you project, select the "Win32 Console", and step through the project wizard, and select the "Empty Project" chack box. Then right-click teh project root in the "Project Explorer" and select Add->New Item. Select a appropriate file type, and enter a name for the file (or just type a file name and extension to override the selected file type). Enter your code in the editor.

Clifford