You are here:

C++/Beginner programmer needs help

Advertisement


Question
I am just starting out with C++ programming and trying to teach myself through internet tutorials and books. I downloaded Dev C++ as a compiler and am using the editor that it provides. I tried to write the Hello World program but something went wrong. I wrote the program correctly, compiled it, and linked it. I tried to execute it, but the program window opens for a fraction of a second and then disappears, what am I doing wrong?

Answer
Nothing.

The program runs, outputs "Hello World" or whatever to the console window then completes and exits. At this point the _only_ command (your program) being run in the console has finished so the console closes.

To get around this you can:

- run the program from a command window (DOS box). As this is set up for you to enter many commands it will hang around long enough for you to see the results.

- Place a break point (a debugging term that I shall not go into much detail here) on the return (if you have one) of main or on its closing brace. This will pause the program (break its execution) at the point specified so you can do debugging tasks such as examine variable values and single step through the code to see what is going on. In this case we are only interested in stopping the program so you can see the results - when you are done you can resume normal execution. You will have to hunt around to find out if DevC++ allows you to do these things and if so how (I am not an expert in using DevC++).

- Add code to your program to wait for input before terminating. A quick and dirty way is:

       char c;
       std::cin >> c;

You will have to enter something other than whitespace (space tab, newline) followed by a newline (enter). For example q<enter> (for quit) would seem reasonable.

Another way is to use the system function to run up a command shell and execute a command - in this case the COMMAND.COM/CMS.EXE pause command (note that the pause command is MS Windows/DOS specific):

       system("pause");

You should ensure you include <cstddef> - place the #include directive with the one you have for <iostream>.
(note: I am assuming here that you are using a modern fairly standard implementation of C++).

Hope this helps  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.