Writing your first C++ Program using Visual Studio 2012

Writing your first C++ Program using Visual Studio 2012

  • Comments 15

In previous blogs I have shown you how to set up Visual Studio 2012 so that it will function on Windows 8, 7, and so forth using the Azure VM, although we haven’t created a program to upload to the store.  That will be covered in another parallel blog series over at http://blogs.msdn.com/silverlightgames.

I won’t use a video this time, although I am tempted to. Smile

Open Visual Studio 2012 using the Windows 8 Client or the Azure VM (if you are using a Windows 7 or Apple computer), we are going to start with the usual boring Console Window.  Sorry.  Visual Studio 2012 differs from VS 2010 as it will create a default name.

image

Since you won’t be living with this app just keep the default name.  Not a good practice, but hey, let’s keep it really easy:

image

It would appear the next dialog box is a useless, but if you click application settings you will also see the following:

image

 

Select Empty Project and then Finish.  If your IDE looks like the following, that would be pretty boring, so add the solution explorer by clicking on View-Solution Explorer or use ctrl+alt+L (good idea to memorize the keystrokes while you are a student)

image

After the solution explorer shows up, you have an empty project, which means it doesn’t have anything in it.  You will need to add a simple couple of lines of code.

image

Select the C++ File (.cpp), give it a name or not.  This is just boring console app that you will forget about in a few minutes after reading this blog.

image

 

Add code that looks like the following to your new Source File, the file with the extension of *.cpp:

#include <iostream>

int main()
{
    std::cout<< "Hello world and all that" << std::endl;
    return 0;
}

If you run your program using F5 then the console will pop up and then close.  To see your beautiful code press CTRL+F5, when you do this then the console app will wait for you to press the enter key to continue, which means it will close.

 

Pretty simple.  And useless, except that it is your first program in C++.  Not something you can show Mom, or maybe you can show it to your Mom, she will smile and think you are pretty smart.  But not anyone else will be overly impressed.  Except for you.  You should be.  Now you can say you have written some C++ Code.  Good work.

 

image

What you console app might look like:

image

So what is <iostream>?  Why the two colons ::, return 0 and so forth.

Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
  • It's nice small tweak for the "Start without Debugging" behaviour... Many a time I have to add equivalent of Console.ReadKey() to the end of console application to see it freeze and let me have the chance to check the output.

    I'm interested that whether the line "Press any key to continue" is added by complier option, or just by running "the application" and "PAUSE" command sequentially...

  • Hey Cheong00,

    Good to hear from you. I am back and the C++ story is going to be awesome.

    Also check out touchdevelop.com :)

  • Using VS2012 ExpDesktop, I like not having to include "system("pause");" before my "return 0;" to see the output. Starts to compensate for the SCREAMING!!! menu headings.

    Looking forward to more from you, Surf4Fun.

  • I am using Visual Studio 2012 and I have followed all your instructions here but why am I getting this error:

    1>------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Please help me! Thanks in advance. c:

  • Hmm, had a question...

    Is there a difference between visual studio express and regular? Im kinda confused about #include "stdafx.h"

    It is similar to this other one I found earlier draalin.com/writing-your-first-c-program

    Is that include actually required??

  • Hey SheWilliam,

    Sorry about the delay.  You would get the fail if your code isn't correct for some reason, there should be an error message above it.

    Hello James B.,

    There are a lot of differences between Express and Regular or professional, the main one is in testing and using addons, as well a bunch of other stuff like the debugging capabilities.  If you are not a student and strapped for cash, then Express is likely the best thing for you.  If you have the cash to buy Regular or Professional then do so.  If you are a student then get Visual Studio Ultimate from your school's MSDNAA or Dreamspark administrator.

  • Hello Surf4fun..

    I have installed VS 2012 in Windows 7.

    Is it problems if I Installed in Windows 7.

    And I have follow your example "Hello World" c++ in VS 2012 but I still have problem.

    Windows said : " 1>LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== "

    can you help me about my problem?

    please send your email to "abednego.tarigan@gmail.com"

    Thanks.

    Regards,

    Abednego Sibero

  • GREAT

  • Thanks great help

  • thanks:)

  • i  love  this  artical

    it  really  helped me :)

  • I am getting the message of, The system cannot find the path specified.

  • Depak,

    You may be using the wrong command line, make sure you are using the Visual Studio command line.  

  • It's nice small tweak for the "Start without Debugging" behavior... Many a time I have to add equivalent of Console. ReadKey() to the end of console application to see it freeze and let me have the chance to check the output.

  • Thanks for this help!!!

    Finally got rid of the non sense of codeblocks or devc IDE in which the headache of downloading the compiler and still it never gets compatible to the windows 8.1.

    Enjoying the ease of coding in Visual studio :)

Page 1 of 1 (15 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them