Jump to content
  • Advertisement
Sign in to follow this  
Red Jasper

system("pause")

This topic is 4809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI Guys and Gals I am leaning c++ and I am having a small problem with getting the window to stay open on a console program. I have looked on the FAQ on Dev c++ and it said I should add system ("pause"); it works but its asks the user to press any key to continue. How can I have it so that the window just stays open? Basic Variables #include <stdlib.h>// needed to pause system #include <iostream.h> int main() { // declaring variables int a,b; int result; //process a = 5; b = 2; a = a + 1; result = a - b; //print result cout << result; system("pause"); //end program return 0; } thank you

Share this post


Link to post
Share on other sites
Advertisement
Ask and you shall recieve. I don't know if it's ideal or not, but I got it out of one of my books.
cin.ignore(cin.rdbuf()->in_avail() + 1);


Note that you have to hit enter, but it will not display a prompt to hit a key.

Share this post


Link to post
Share on other sites
well in that case, you'd want to run the program through a command prompt. first, find out where your program is being created, eg. the project dir. then go Start->run. in the window that pops up, type "cmd" without the quotes. press ok. then browse to your project directory through the command prompt. presuming you don't know how to use the command prompt, here is a few helpful commands

cd <directory name>
- will go to that directory

dir
- will list all files and directories in the current directory

cd / (may be the backwards slash)
- will go to the root directory of the drive. eg. say your in c:\windows, this will take you to c:

cd ..
- will go up one directory

<driveletter>:
- will take you to that drive. eg c:

anyway, once your in the correct directory, type your executable/program name and press enter. when you wish to run the program again, you'll just have to press the up arrow key, then press enter.

Share this post


Link to post
Share on other sites
If you've got a command prompt opened, you should be able to just drag and drop your program into the window and the path will be passed into it. Then just hit enter to begin execution.

Share this post


Link to post
Share on other sites
Use something like getch() or cin.get() instead of system("pause")
ex:
#include <stdlib.h>// needed to pause system
#include <iostream.h>
int main()
{

// declaring variables
int a,b;
int result;

//process
a = 5;
b = 2;
a = a + 1;
result = a - b;

//print result
cout << result;


getch();

//end program
return 0; }

Share this post


Link to post
Share on other sites
As DigiDude said, you can use std::getch(), but if you're using C, you'll have to include conio.h, 'cause it contains getch(), and from what I've read on the forums, there are some compilers that don't have it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!