6 Replies - 592 Views - Last Post: 03 September 2013 - 06:03 PM Rate Topic: -----

#1 Nutrient  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-September 13

Making program pause before exiting

Posted 03 September 2013 - 04:48 AM

Hey all,

Fairly new to coding although i am enjoying it so far.

Once simple question is how can i make my code pause at the end and wait for a key to be pressed to exit? is that was End Of File is?

For instance this code executes in the terminal but on windows a terminal pops up for a fraction of a second so i can't actually read the output.

 

#include <iostream>

int main ()

    {
         int sum = 0, val = 50;
         // keep executing the while as long as val is less than or equal to  10
         while (val <= 100) {
               sum += val; //assigns sum + val to sum
               ++val;      // add 1 val
               }
         std::cout << "Sum of 1 to 10 inclusive is "
                   << sum << std:: endl;
         return 0;
}




Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Making program pause before exiting

#2 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Making program pause before exiting

Posted 03 September 2013 - 05:02 AM

system("pause");



Directly before return 0;
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,168
  • Joined: 05-May 12

Re: Making program pause before exiting

Posted 03 September 2013 - 05:56 AM

Although that solution would work, that is a really expensive way to make a program pause. Think about all the work that goes on with that 1 line of code: The C-runtime will have to load the the Shell32.dll into memory so that it can call ShellExecuteA(). Next it will have to supply the extra parameters needed by ShellExecuteA() and then call it. ShellExecuteA() will have to convert the ANSI string "pause" and the other strings into the UNICODE string "pause", and then call ShellExecuteW(). ShellExecuteW() will have have take the all strings passed to it and populate a SHELLEXECUTEINFO structure, and call ShellExecuteEx(). Next if you are on a 64-bit system, and your program was compiled as 32-bits, then Windows will have marshal the 32-bit SHELLEXECUTEINFO into a 64-bit SHELLEXECUTEINFO and call the 64-bit ShellExecuteExW(). The 64-bit ShellExecuteEx() will have to detect whether the caller was 64-bit or 32-bit and launch the appropriate 64 or 32 bit version of CMD.EXE, and pass the string parameters from to the command line. CMD.EXE, after launching, will have to detect if it is currently running within a console context or not and determine if it needs to create another window. And then it gets around to finding the string "pause" and then iterating over every directory listed in the PATH environment variable trying to see if there is a "pause.com", "pause.exe", "pause.cmd", and "pause.bat". Only if all the directories come up empty, will CMD look through its list of internal commands and finally bring up the prompt to press any key.

Ouch!

Why not just a simple?
char ch;
cin << ch;


Was This Post Helpful? 2
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4103
  • View blog
  • Posts: 12,707
  • Joined: 25-December 09

Re: Making program pause before exiting

Posted 03 September 2013 - 06:56 AM

Not to mention the fact that that method only works on a Windows machine. The pause command in not even available on any other operating system.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5361
  • View blog
  • Posts: 27,318
  • Joined: 10-May 07

Re: Making program pause before exiting

Posted 03 September 2013 - 08:47 AM

Was posted in the C/C++ FAQ.
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Making program pause before exiting

Posted 03 September 2013 - 11:12 AM

The system() system call also has some overhead to it, at least on Linux. On Linux, this is the description of what system() does:

Quote

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

I second what Skydiver said as it's the most portable and will do exactly what you want which is to keep the console window open. Just remember that it closes so quickly because you're running it in an IDE. If you ran the program from a console window, the program just exits it doesn't close the window too.
Was This Post Helpful? 1
  • +
  • -

#7 Nutrient  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-September 13

Re: Making program pause before exiting

Posted 03 September 2013 - 06:03 PM

Thanks, appreciate your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1