March 21st, 2004 09:38 PM
I am just starting to learn about C++ and have bought a book (SAMS Teach yourself C++) it had a cd that came with it which has the Dev-C++ IDE compiler on it.
As the book teaches you how to use this compiler in conjunction with the programs it teaches you I am trying to learn how to use it.
I have a problem though, after I have compiled, linked and debugged the programs in the book whenever i try to run them the command prompt screen flashes up for a split second then disappears instead of staying up so I can see the results of what it have done.
I am running WinXP Home and have updated Dev-C to the latest version.
Could anyone give me any ideas as to what I am doing wrong or how I can rectify the problem, I have changed all of the compilers settings to what it says in the book but to no avail!
As i said I am quite new to programing and havent got a clue about whats going wrong!
Thanks in advance for any help!
March 21st, 2004 10:38 PM
Try running your programs from the command line rather than through the IDE. I've never used Dev-C++, but this usually happens when you try to run a DOS program as a Windows program. Alternatively, add code like this at the end of your main() function
Hope this helps.
printf("Press Any Key To Continue: ");
March 21st, 2004 10:40 PM
open up the command line interface first - then type in the path and .exe. the command prompt will stay open.
If you d/l the XP power tools there's a modification that when you open the right click menu in windows explorer will allow you to 'open command window here' - this will open the command line at which ever folder you're in using windows explorer. handy if you navigate to the window you've stored your c++ exe in
Quis Custodiet Ipsos Custodes
March 22nd, 2004 05:03 AM
I had this same problem. I use dev-c++ 5 (beta).
In dev-c++ when you create a new cpp file i think it still adds this in there.
system("PAUSE"); //add this before return 0;
That should solve your problem. You could still open it in comand but i think the above way is better.
March 22nd, 2004 08:32 AM
I have a problem though, after I have compiled, linked and debugged the programs in the book whenever i try to run them the command prompt screen flashes up for a split second then disappears instead of staying up so I can see the results of what it have done
it's a matter of split seconds ,you have to be pretty sharp learning C++
It happens with all the compilers i used be it TurboC++ , Microsoft Quick C or VC++.....
This generally happens with programs that do not need user intervensions like the program below dosen't need the user to do anything , the programs runs does the adding and displays output. If it would have been dos it would have transferred the control back to the prompt and you would have been able to see the output just above the current prompt.
But under Windows the Command Prompt shows up does the calculations shows the result and disapeers . I think VC++ counters this problem by adding a small code into the programs and at the end of every programand the command Prompt windows says "Press any key to Continue" . It does this by using the Getch() ; it waits for the user to press any key. ............I have made it a habit of writing getch() at the end of every C,C++ ,program i write.
Try using getch(); at the end of the code
this one Blinks for less than a second and poof.. Dissapeers
just add getch(); at the end
this one waits for me to press any key after the program has finished.
March 22nd, 2004 08:24 PM
Thanks all for the help! it certainly helped me understand what was happening a bit more. I think the quickest way i found was to add the " system("PAUSE"); " before the return 0;
so thanks for that mikem0327.
I tried the getch(); way SwordFish_13 but it kept coming up as an error when i compiled it, i even cut and pasted the code you wrote here and it still came up as an error, could this be my compiler??
Thanks again all
March 23rd, 2004 05:39 AM
What's the error message is it "Function getch(); should have a Prototype" . Did you include the #include<conio.h> conio header file because getch() is defind in the conio header file
I think this will give you a gist of what gothic_type is trying to say
Originally posted here by gothic_type
hope you get the gist of what I'm trying to say
i've seen several people use system("PAUSE") when they want to delay their programs. i'm not sure who's teaching this method, but it's a bad habit.
by calling system(), you are invoking the default shell. the shell then executes the command line given to it ("PAUSE", in this case). that is, it runs the 'pause.exe' program. so, now your simple C/C++ program is relying on two external programs for a stupid thing like pressing a single key. what if someone deleted or renamed 'pause.exe'? what if someone tried to compile your program on unix, or a mac?(okay getch() too is not portable but it dosent invoke other programs) it wouldn't work. you'd get an annoying shell message and no pause. besides, why have the overhead of launching 2 programs, I mean its like bringing a Cannon out to kill a fly the fly might get killed but the cannon might harm you too.
I just Downloded DEV C++ it's VER 126.96.36.199 on my slooooooooow Dial-up at about 4 Kbits /per second yep it's that slow *yawn*
To tell you the truth i can't even compile the "Hello World" program in it After working for almost one year in C++ . damm Turbo C++ has spoiled me .....
after half an hour of labourious struggel we are finally getting somewehere, ureka i have compiled the "Hello world" program.
try this one i have just compiled it in DEV C++
Or you can also use this, it dosen't use getch()
using namespace std;
int main ()
cout << "Hello World!" << endl;
these two line in bold at the end of the program will also work.
using namespace std;
int main ()
cout << "Hello World!" << endl;
cout << "Press ENTER to continue..." << endl;
And now i am going to uninstall DEV C++ . i just love Turbo C++ Ver 3 even more now Plain and simple.
If Portability is a issue with your programs then i guess Getch(); is a bad idea, you wouldn't find it in Linux or MAC . There are hell lot of ways to do it you just have to make it wait for a key press. if you are not going to port your programs to another system (*nix etc) then i guess conio.h would be quite helpful to you it contains many imp functions (getch(), getche(),clrscr(),putch() etc most of which are uncompitable winth *nix etc.
March 23rd, 2004 08:59 AM
Nokia, you should use getch() like SwordFish_13 advised rather than system("PAUSE") because when you call system(...) you are getting the computer to run a command using it's cli (command language interpreter e.g. dos, the bash shell...) It's not a good idea to do that if at all possible in case you need to port your programs, and also, calls to system(...) don't usually run very fast.
I don't believe getch() is portable, so maybe that isn't a concern, but the speed issue is. If you decide to clear your screen using system("cls"), it can slow your program right down.
Sorry that post is a bit garbled...hope you get the gist of what I'm trying to say.
March 23rd, 2004 02:38 PM
Everybody, getch() is not portable. I think it's as bad as system("PAUSE"). Use getchar() instead, it's simple, portable and doesn't need any external shell.
Also, follow this thread for a discussion of Standard C:
Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds
March 26th, 2004 11:49 AM
No i didnt include the #include<conio.h> header SwordFish_13, obviously that was where I was going wrong!
I have just tried a few programs out the book and added it along with getch(); and it works everytime, so thank you very much for that it has certainly solved a big problem for me.
Just out of intrest is it possible or would you recommend against adding it to most C programs or are the occasions when you shouldnt add it? In other word would I be OK to add it to all the programs I am learning?
Thanks for the help!