Share via


Writing your first C++ Program using Visual Studio 2012

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 https://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.

Comments

  • Anonymous
    January 08, 2013
    The comment has been removed

  • Anonymous
    January 11, 2013
    Hey Cheong00, Good to hear from you. I am back and the C++ story is going to be awesome. Also check out touchdevelop.com :)

  • Anonymous
    January 11, 2013
    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.

  • Anonymous
    May 01, 2013
    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:

  • Anonymous
    May 14, 2013
    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??

  • Anonymous
    May 21, 2013
    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.

  • Anonymous
    July 14, 2013
    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

  • Anonymous
    August 20, 2013
    GREAT

  • Anonymous
    October 20, 2013
    Thanks great help

  • Anonymous
    October 25, 2013
    thanks:)

  • Anonymous
    November 19, 2013
    i  love  this  artical it  really  helped me :)

  • Anonymous
    July 08, 2014
    I am getting the message of, The system cannot find the path specified.

  • Anonymous
    July 09, 2014
    The comment has been removed

  • Anonymous
    August 03, 2014
    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.

  • Anonymous
    August 07, 2014
    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 :)

  • Anonymous
    October 22, 2014
    i am using opencv on visualstudio 2012...while performing build it shows an error 1>------ Build started: Project: test, Configuration: Debug Win32 ------ 1>LINK : fatal error LNK1104: cannot open file 'opencv_core248d.lib' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========= i hav included the core file...still its displaying d same error

  • Anonymous
    November 05, 2014
    this was quite good but i need to learn more with mfc also

  • Anonymous
    January 23, 2015
    i recommend tu use: using namespace std; in order to not use the std::cout...etc

  • Anonymous
    September 07, 2015
    It is nice so far. Thank you. So what is next? Where is the next lesson?

  • Anonymous
    September 08, 2015
    Hi all, will update this with XAML/C++ and Window 10, along with using the XAML/C++ on the Raspberry Pi!

  • Anonymous
    September 30, 2015
    i done the above steeps but they not show  me a right result ( data is out of project) what is tha solution of this problem plzzz tell me

  • Anonymous
    October 14, 2015
    itz really great to learn from thiz trick

  • Anonymous
    February 03, 2016
    Nice for starting c++ in visual stdio

  • Anonymous
    February 15, 2016
    you have no idea how useful this has been. I have taken and successfully done many tutorials. but they skip the most basic things needed to use ide, setting it up, as simple as it sounds, it isn't at all. you are hit with a ton of options and it is so helpful having the basic starting point clearly demonstrated. thank you