View Full Version : quick C question..
liquid
08-11-2002, 07:56 AM
When I run a program I have made from inside the compiler the dos window stays open.. but if I execute the program directly the window just flashes bye..
How can I make it stay on the screen..?
I am using ms visual c++ 6.0
thanks...
Danger Fan
08-11-2002, 01:06 PM
the way I always did it was to have the program wait for some input. at the end of your code, put this:
int a;
cin >> a;
When you prog hits that, it will wait until you enter a number and hit enter. Then it will end.
Strike
08-11-2002, 04:24 PM
Or open a command window first and then run it.
Kamikaze!
08-11-2002, 05:04 PM
#include <conio.h>
getch();This makes the program wait until you press a key, works better than cin.
Bradmont
08-12-2002, 03:08 AM
but getch() only exists on windows, so try to avoid it.
sans-hubris
08-12-2002, 03:39 PM
Easy, standardized substitute:
#include <stdio.h>
...
void get_enter(void)
{
char garbage;
garbage = fgetc(stdin);
while(garbage!='\n')
garbage = fgetc(stdin);
}
This function will wait until the enter key is pressed, and skip all the other characters. Be warned, however, that you need to watch out for leftover EOLs in your code (especially in C++ when you use the insertion operators.)
file13
08-12-2002, 06:46 PM
you could also use:
system("pause");
sans-hubris
08-13-2002, 12:45 AM
Originally posted by file13
you could also use:
system("pause");
"pause" only exists on DOS (i.e. Windows), which is what Bradmont and I were trying to avoid.
There is, of course, nothing to stop all of you from using those commands (getch and pause), and, who knows, perhaps you may not ever touch a non-Microsoft system, but if you find yourself working with one, you'll realize the need for sticking to portable standards.
Bradmont
08-13-2002, 03:37 AM
and, say, some unix user spots your app and, it'd be useful, and offers to port it. Using cross-platform standards would make that work a lot easier. :)
Originally posted by Strike
Or open a command window first and then run it.
I agree with this.. If you feel for some reason you absolutely must click on an icon to get the program running, you could just make the icon a shortcut for command.com (or cmd.exe, whichever your version of doze uses) which executes your program on start-up. I'm fairly sure you can set that in the shortcut properties.
file13
08-13-2002, 11:19 AM
Originally posted by sans-hubris
"pause" only exists on DOS (i.e. Windows), which is what Bradmont and I were trying to avoid.
oh i completly agree. but the question did specifically mention DOS and since he's using Visual C++ this is how it's done in there anyways--when you run it within the IDE. you could always wrap the non-portable code in preprocessor code if you plan to port it. but in my experience most M$ coders aren't that concerened about portability.
but you're preaching to the chior. that's why i code in portable languages like Ada or Ocaml.... ;)
Kamikaze!
08-13-2002, 05:05 PM
Originally posted by Bradmont
but getch() only exists on windows, so try to avoid it. What about kbhit()?
Danger Fan
08-13-2002, 05:21 PM
Kamikaze!: Is that and invader zim avatar?
ps. Sorry for the OT post
Kamikaze!
08-14-2002, 07:49 AM
Yeah. I can't make it bigger because the max size is 50x50 (else it looks weird) :(
Strike
08-14-2002, 08:35 AM
Originally posted by Kamikaze!
What about kbhit()?
Windows-only too, as far as I know.
I still don't get what's so hard about opening a command window first and running it ...
If you are creating a console program, then it is meant to be run from the console. Otherwise create a win32 program. They're not hard to do with visual studio.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.