compiler trouble


#1

I am getting back into programming and trying to learn c++ again and for some reaosn I am having a hard time making a “hello world” program. My exact problem isnt the code or anything, i got up to arrays in c++ and i know php enough to work as a web programmer; the problem is that whenever I compile any program, even a hello world program, it shuts off immediatly after executing the exe file. I am using microsoft .net 2003 btw (also had ms visual studios 6.0 layign around and tried compiling witht hat and i cant even build a program period in 6).


#2

The problem is you’re using windows. :slight_smile:

I’m guessing what’s happening is you’re double-clicking the .exe to run the program. It executes and then as soon as it finishes windows closes the shell.

Launch your program by typing its name in a command shell and you should see the expected result.

I seem to remember also that there was something you could do with MFC (euuugh. Even the memory makes me shudder) (was it called an MFC command prompt project or something?) that would run a shell when you double-clicked your program, then when it was finished would wait for a keypress before closing the shell.

You could implement something like this yourself using the getchar() (or was it getch() ) command. Put a getchar() before your return statement in your main() function and your program will wait for you to press a key before returning. The getchar() function’s in some arcane msdos header iirc.


#3

lol, no one likes windows, poor Bill. Yea, i was only able to run my programs through command prompt, I dont feel like dealing with mfc right now, ill stick to command line. I dont know the header its in so cant use the getchar() function (although i remeber using it before, unless it was in php and not my previous c++ lessons months ago).


#4

getchar() – can be found in stdio.h, right?

See msdn:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/crt_getc.2c.getwc.2c_.getchar.2c_.getwchar.asp

-mk-


#5

getchar() is in stdio.h. Thanks :mk: and playmesumch00ns


#6

If you are using XP or, probably also windows2000, if you right click on the exe and go to its properties, you can then turn off the switch that closes the window automatically. Changing that will lead to a .pif file being created with the same name of the exe.


#7

I couldn’t find that option but I did find that using system(“pause”); before main() ends does the trick even better the getchar().


#8

or even,

Console::WriteLine(“Press Enter to end program.”);
Console::ReadLine();

:slight_smile:

-mk-


#9

I find the easiest solution is:

system(“pause”);

This prints out “Press space to continue” and then waits until you press space before continuing…


#10

Yes, but that’s Managed C++ :wink: and you really should prefix the string literal with ‘S’.


#11

Ctrl + F5 to build also ends console programs with a “Press any key to continue.” as well.


#12

If you want to just output a string for debugging purposes, irrespective of whether your program has a WinMain and a message loop or a old-school main (ie. a console app) you might want to consider calling:

::OutputDebugString(LPCTSTR value);

This is particularly useful if you’re debugging code that’s running in a different security contex or Window Station. You can use the “DebugView” tool from www.sysinternals.com to see your messages when not running under Visual Studio’s debugger. Incidentally, if you don’t know about this site be sure to check out their Process Explorer tool, and I find their command line “PS toolkit” a must have too.


#13

lol, after system(“pause”); the things you guys recommended are above my level right now. I just needed to keep my tutorial executables from closing once complete so a simple solution, like system(“pasue”) or getcahr(), would have been suffice. After all, isnt the point of making good programs also making it simple? Im sure ill run across those other thigns you guys mentioned when I get to that part in the book. Only xp for me has this problem of closing once fully executed (with command line type programs), my old computer ran ME(later upgraded to xp) and never needed any extra code to get the “press any button to close”


#14

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.