Visual C++ Update

So as I said in a previous post, I am working on a new programming book to C++. I have never worked in this language nor do I intend to start. Why do it? Well learning new languages helps me understand the languages I already know better. Since I have been so busy, I have not progressed as far as I would want in the book but that happens. Making money with my existing skills always trumps learning new skills. As I ran down this path, one big thing I discovered is I could not get the examples from the book to work in the Visual C++ world.

Crap, Visual C++ won’t run C++ code? No, that is not true but I could not get the window it was running in to stay open but I did not know that. DOH!

How did I discover this?

I figured this out by building a rough application. Next, I ran the debug and traced my way through. Finally, I knew my output was present but it was not showing up in the output console. Of course that required me to get my head out my bum and create a console, rather than a windows application. That’s was a surprise when I hit the wrong thing.

How to fix this?

You must add the following before return 0; line. It allows output window to stay open long enough for you to see it. Why this was as hard of an answer as it was is beyond me but here is the code.

system(“pause”);

If you are trying to push through C++ in Visual C++, your programs will run and close before your eyes can focus. Use this to prevent it.

An exciting post eh?

If yes then do what I did and stick this in the .cpp file.

#include “stdafx.h”
#include <iostream>
using namespace std;
int main()
{
   cout << “Hello reader. \n”
   << “Welcome to C++. \n”;
   system(“pause”);
   return 0;
}

So now you know and knowing is good.

Leave a Reply

Your email address will not be published. Required fields are marked *