Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,835 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,505 people online right now. Registration is fast and FREE... Join Now!




system("pause");

 
Reply to this topicStart new topic

system("pause");

pietra
19 Feb, 2007 - 01:07 PM
Post #1

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
I see this line often in main function in other's code examples. It's not that I don't know anything about C++, but my teacher never mentioned it. So I'm wondering what does it mean. I know it's an option because we used to write programs without it in our class. I'm just curious smile.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: System("pause");
19 Feb, 2007 - 01:11 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
The command in question allows for the execution of the pause.exe, which will hold a command window open so that the output of a program may be viewed. It is a common shortcut used in programming these days, but has it;s downfalls. most notably, the pause.exe is NOT available on all platforms, and is not part of the ANSI standards. It is platform specific, and could therefor cause a program to fail when ported to other platforms. i always recommend against it's use, as there are methods to accomplish the same effect that do meet standards.
User is online!Profile CardPM
+Quote Post

horace
RE: System("pause");
19 Feb, 2007 - 01:16 PM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(pietra @ 19 Feb, 2007 - 09:07 PM) *

I see this line often in main function in other's code examples. It's not that I don't know anything about C++, but my teacher never mentioned it. So I'm wondering what does it mean. I know it's an option because we used to write programs without it in our class. I'm just curious smile.gif

It runs the pause command (as though from a command line or batch file) which waits until the user presses any key, i.e.
http://www.computerhope.com/pausehlp.htm

for details of the system() function have a look at
http://www.cplusplus.com/reference/clibrar...lib/system.html
User is offlineProfile CardPM
+Quote Post

GWatt
RE: System("pause");
19 Feb, 2007 - 02:01 PM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
Another option to use is getchar() It successfully pauses a program until <Enter> is pressed, and I have used it on Mac, Windows, and Linux.
User is offlineProfile CardPM
+Quote Post

pietra
RE: System("pause");
19 Feb, 2007 - 02:40 PM
Post #5

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
Ah! I see. Comes in handy.

Amadeus, what other methods were you thinking about? getchar() like GWatt suggested?

GWatt, would you be so kind and give me an example for ust of getchar() for this?

Thank you for the answers.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: System("pause");
19 Feb, 2007 - 03:26 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
You can use getchar() simply as a one line command:

CODE

int main(void)
{
   printf("Program will be held open");
   getchar();
   return 0;
}

or
CODE

int main(void)
{
   char c;
   printf("Program will be held open");
   c=getchar();
   return 0;
}

Other methods include using cin.get() for C++, among others. Of course, with many of the alternatives, you have to ensure the buffer is not already holding a character, like a newline.

There are other methods discussed here:

http://www.dreamincode.net/forums/showtopic14563.htm



User is online!Profile CardPM
+Quote Post

pietra
RE: System("pause");
19 Feb, 2007 - 04:49 PM
Post #7

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
I get it, ty smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:41AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month