Author Topic: system("pause") command  (Read 73311 times)

Offline azrael2000

  • Single posting newcomer
  • *
  • Posts: 2
system("pause") command
« on: November 18, 2010, 06:24:57 pm »
Hi All.

Just a little question.

While developing, I normally use the command

Code
system("Pause");

in C++ to stop the console window so I can see what is in it.

I have tried this command in Code::Blocks and it fails.

Do I have to load a .h file?  In other programs I have used this has never been an issue.

I am running windows 7 OS

Regards

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: system("pause") command
« Reply #1 on: November 18, 2010, 06:33:08 pm »
The first thing:
C::B is an IDE not a compiler, it can work with many compilers.

If you use a project (recommended), you should go to "Project -> Properties -> Build targets", make sure your target is of type "Console project" and you have checked "Pause when execution ends".

Offline xunxun

  • Almost regular
  • **
  • Posts: 187
Re: system("pause") command
« Reply #2 on: November 18, 2010, 06:40:19 pm »
The standard doesn't contain "system", and it's not portable.
You can try to use "cin.get()" to pause the execution.
Regards,
xunxun

Offline azrael2000

  • Single posting newcomer
  • *
  • Posts: 2
Re: system("pause") command
« Reply #3 on: November 18, 2010, 06:42:31 pm »
Hi there.

Thank you for the response.

I checked where you suggested, and I do have the check box checked.

However, the console window still just quickly flashes when the compiled program (debug or release) runs and then is no more.

I've been using the gcc compiler with the program.

Regards

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: system("pause") command
« Reply #4 on: November 18, 2010, 08:31:19 pm »
Hi there.

Thank you for the response.

I checked where you suggested, and I do have the check box checked.

However, the console window still just quickly flashes when the compiled program (debug or release) runs and then is no more.

I've been using the gcc compiler with the program.

Regards

It works only, if you start your program from inside C::B with the green triangle or via "Build -> Run" (Ctrl + F10) or "Build -> Build and run" (F9), not if you use the debugger and not if you run it from outside C::B.

jvnetsl

  • Guest
Re: system("pause") command
« Reply #5 on: November 25, 2011, 11:31:15 am »
You Must include Windows header file to do that.. <windows.h>
so include this,
#include <windows.h>

Hope this helps..... :) :) :) :) :) :) :) :) :) :) :)

JV net works..

zabzonk

  • Guest
Re: system("pause") command
« Reply #6 on: November 25, 2011, 12:42:01 pm »
> The standard doesn't contain "system", and it's not portable.

system() is part of both the C and C++ standards, which declare it in stdlib.h (or cstdlib).
« Last Edit: November 25, 2011, 05:29:12 pm by Neil Butterworth »

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: system("pause") command
« Reply #7 on: November 25, 2011, 04:00:14 pm »
You can try to use "cin.get()" to pause the execution.
To use cin.get() to pause execution, you might have to call cin.sync() first.

> The standard doesn't contain "system", and it's not portable.

system() is part of both the C and C++ standards, which must declare it in stdlib.h (or cstdlib).
I think it is more that the "Pause" that system() is calling is an executable that must exist in the current path; it may or may not exist/function in the same manor - depending on where the program is run - (or could possible be a virus hoping that you will run it by mistake!).  (It is also misuse of system(), as forcing the OS to search through the path and load a new program just to pause execution, is a huge amount of overhead to achieve a very tiny function.)