Sunday, July 26, 2009

C++ problem?

I have a problem whenever i write C++ code out on a complier it flashes input onto the screen really quick then its gone, im currently trying to use Dev-C++. If anyone knows how to help me that would be awsome.








FYI im a NOOB at C++ so put it in stupid form plz.

C++ problem?
Use Dev C++ just to compile (Ctrl+f9 is the shortcut) and run a program by calling it (typing it's name in the right directory) from Command prompt. That will work for sure .





Maybe a


system PAUSE;


line would make Dos screen from DevC++ stay longer when you Compile%26amp;Run, but I'm not sure about that.





Anyway, there is no problem with the C++ code that you typed.
Reply:wat is a c++ problem???????????????
Reply:If you want, declare a variable, (such as an int or char).





Next, ask before the end of "int main ()" for input on that variable.


E.G.





int main (void)


{


/*program loop stuff*/


char End;


std::cin %26gt;%26gt; End;


return 0;


};
Reply:If you're using C++, stay away from things like 'getch', etc. Instead use:





cin.get();





Before your 'return 0;' statement
Reply:Its likely you are writing a simple c application (not a windows app). This means that when you run an application, all output will be displayed in a 'DOS' window, but when the execution of your app is finished (when the 'main' function returns) then that window will be closed as it is no longer needed.





To get around this problem, you can put a simple statement to request a character from the keyboard, and the program will not continue until it recieves that character. This can be done by adding the following...





getchar(); // Returns an int holding the key code of which ever keyboard key was pressed. Needs 'enter' from keyboard as well.





You will need to #include %26lt;Stdio.h%26gt; to use this if you arn't already.





As an alternative, you could call a system command (which takes any command that you can enter under DOS, as lyinx2001 suggests, by adding...





system("PAUSE");





You will need to #include %26lt;stdlib.h%26gt; if you are not already doing so.





A thrid way, and is mainly if you are developing in windows, is to pause your program for x seconds. You can do this by calling...





Sleep(1000); // Take note of the capital S and the number is the time you want the program to sleep in milliseconds. 1000 == 1 Second.





You will need to #include %26lt;winbase.h%26gt; if using this.





My preference has always being the first approach, and im sure there is many other ways of keeping the window open (there normally are many ways of completing a single task when programming is concerned - its all a matter of preference).





If you were using a Microsoft development environment, then normally the window will not be closed and a 'Press any key' will be displayed by default.
Reply:just reinstall in or goto http://www.c.com
Reply:It's been a while since I did anything with Dev-C++, but if I remember right, you can run your compiled program from the DOS Shell ... (go to the start menu, select run and type in command.com if you have win 98/ME or older, or cmd.com if win2000 or newer) and then use the cd command to change to the directory of your program (I'm going to assume that you're somewhat familiar with DOS and how it works, if not I can help you with that too) but in any case, run it from the DOS Shell, and it should keep the display up. See windows is designed to once a DOS program quits running, to shut the window down automatically. there's also a setting under properties of the individual program that you can uncheck to keep the window up after program termination... this option is different for different versions of Windows. Look around, I'm sure you can find it.


-Duo
Reply:If you want to pause the output, just include the statement system("PAUSE"); before the return statement. That will freeze the program until a key is pressed.


No comments:

Post a Comment