Sam's Teach yourself g++ problem, Please Help!

Hi, I'm 17 and I just started using Sams Teach Yourself C++ 5th edition. Hilariously, I somehow managed to get stuck on the first lesson. I installed the MinGW, which came with the G++ compiler. I wrote a program In Notepad++, I compiled it correctly, I can see the exe on my desktop, but when I go to open it, it instantly closes out. Any Ideas?

Run it through command line.
Run cmd first, then navigate to your file and run it.
Or execute it from IDE of your choice. If you are not using one, start using it. It is really handy.
I have visual studio installed, is that a good IDE to use?
Yes, it is pretty good. Which version is it? You should have at least 2013, or, better, 2015.
Visual studio is good, but I use Code::Blocks and i love it more than VS. You can download a version with all that good stuff already there, just open the IDE and start writing. If your interested in checking it out here is the link:

http://www.codeblocks.org/downloads/26

Download the one that says:

codeblocks-13.12mingw-setup.exe


The BerliOs link does not work so use the sourceforge one.
Last edited on
I have Microsoft Visual Studio 2013. So by using this I won't have to worry about compiling through G++ correct? However when I use Visual Studio I am still confused as to which form to use. Which one is most common, Windows Forms or console application? I am familiar with Visual Basic, is there a visual form editor for C++ as well?
Last edited on
Which one is most common, Windows Forms or console application?
Most guides and books will assume you are using console project as it is the most pure part of C++. After you get the grasp of the language, you can move on to 3rd party libraries e.g. graphical ones.

, is there a visual form editor for C++ as well?
Both Visual Studio and Borland C++ Builder provides one.
Ok I copied the code and it did the same thing in Visual Studio, the console just kept closing as soon as it opened. This is my code:

1
2
3
4
5
6
7
#include <iostream> 

int main()
{
	std::cout << "Solidum petit in profunis!\n";
	return 0;
}


is there anything wrong with it?
Last edited on
Ctrl + F5 instead of just F5 to run application.

F5 will make it run in debug mode which is useful if you have some problems and know how to use it.
There is absolutely nothing wrong with your program.

The problem is this:

1. you click the exe
2. a console window is created and attached to the running exe
3. the exe prints your message
4. the exe terminates
5. 5the console window closes
6. this all happened so fast you saw just a flash, maybe


To run the program, you need to have an open console window.
1. Change to the folder your exe file is in (or, if it is on the desktop, minimize everything so that the desktop has focus)
2. Hold down Shift and press the Menu key (on the right side of the keyboard between the Windows key and the Ctrl key)
3. Then press 'W' and then Enter
4. Type the name of your exe file

Hope this helps.
Thank you Duoas that did work!

Now my question is, do I have to do that every time I am creating a console application? I'm fine with that, just wondering.

And how come the same thing was happening in Visual Studio? Is there any way I can debug without that happening?
Last edited on
And how come the same thing was happening in Visual Studio?
Run without debugging. You wil see same behavior with debuggind in Code::Blocks too, but in C::B run with debug is not the default mode.

Is there any way I can debug without that happening?
When you debug, you have other ways to prevent this from happening.
Console applications are designed to run from the console. Double-clicking it from explorer is not running it from the console.

When you double-click a exe designed to run from the console, Windows is smart enough to create and attach a console window to the process. But when your program is done, Windows figures you no longer need the console created for you, so it removes it.

To keep the console window open a little longer, your program should not terminate until the user is ready. Here's an old article (that I have yet to update) that discusses this:
http://www.cplusplus.com/articles/iw6AC542/

Your other option is to create a link to your application that opens a console and then runs your application.

(Or you could just require/assume your users will run the program from the console they way it is designed to work.)

Hope this helps.
It has, I just wished they would have mentioned this in the book I purchased. Thank you very much!
If you have Visual Studio 2013 there is a tutorial on how to compile standard C++ programs here :

http://www.cplusplus.com/doc/tutorial/introduction/visualstudio/


And of course the free version of Visual Studio is called Visual Studio Community now.
Last edited on
Topic archived. No new replies allowed.