Holding The Execution Window Open

Title 2: How do I pause for input?

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

91 Replies - 61230 Views - Last Post: 31 December 2010 - 08:36 AM Rate Topic: -----

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Holding The Execution Window Open

Posted 18 July 2007 - 06:02 PM

*
POPULAR

First and foremost, allow me to assure you that this post is not meant to discuss the sleep() command, mutexes or semaphores, or using threads. It is meant to address one of the most commonly asked questions by newcomers to C++, especially those who use an IDE for programming.

When my program runs, the execution window closes immediately. What is wrong? How do I hold it open/pause it?

Nothing is wrong - that is exactly what your program has been designed to do - execute, then exit.

When newcomers ask the question above, they are almost always told to use one of the following commands:
getch();
//or
system("pause");



Both of the commands above are platform specific solutions, and do not conform to ANSI standards. They use components that are found on certain architectures, but not others. It is preferable to use a solution that does conform to standards, and uses accepted C++ methods.

One such method that is also often recommended is using:
cin.get();


This solution will indeed pause for user input - as long as no characters are currently in the stream. As most applications will have asked for input several times, it is likely that there will be at least one character in the stream (often a newline) that will be captured by the cin.get() method, and the application will continue on past.

There are many ways to do it and remain standards compliant. I will not reinvent the wheel, but will simply post one example already submitted by a member of this site - Xing (hope you don't mind me posting this).

This simple snippet is as follows:
#include <iostream>
#include <limits>
 
int main() {
   
  // Rest of the code	 
  
  //Clean the stream and ask for input
  std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  std::cin.get();
  return 0;
}


The original submission can be found here:

http://www.dreaminco.../snippet582.htm

This method is standards compliant, and will hold the command window open without invoking any executables that may or may not be present.

Of course, you can always run the program from the command line. :)
Was This Post Helpful? 8

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5305
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Holding The Execution Window Open

Posted 18 July 2007 - 07:09 PM

Quote

Of course, you can always run the program from the command line.

Yeah, if someone refuses to use getc (cin) or sleep, then just run it from the command line... otherwise code a windows interface.

What would be an example of a program that can not use getc (cin) or sleep?

How about if you did something like the following:

#include <stdio.h>

int main(void) {
  int i=0,k=0,j=999999;
  printf("Our Program is running...\n");
  for(;i<j;i++) {
	if(i==j)return 0;
	else {
		printf(" ");
		printf("\b");
	}
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Holding The Execution Window Open

Posted 19 July 2007 - 08:06 AM

may want to include that getch() is part of the #include <conio.h> for c++ otherwise you get a compile error

#include <iostream>
#include <string>
#include <conio.h> //for getch()

using namespace std;

void main()
{
   string strInput;

   cout << "Enter Some Stuff Here: "; //Prompt for input
   cin   >> strInput;						  //Get Input

   cout << strInput;						  //Display Input

   getch();									   //Pause
}//end main


This post has been edited by Topher84: 19 July 2007 - 08:09 AM

Was This Post Helpful? 0

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Holding The Execution Window Open

Posted 19 July 2007 - 09:50 AM

I think my point may have been missed. I advise against the use of getch(). It is a platform dependent function that does not comply to ANSI standards. It is not part of standard C++.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5305
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Holding The Execution Window Open

Posted 19 July 2007 - 09:52 AM

View PostAmadeus, on 19 Jul, 2007 - 09:50 AM, said:

I think my point may have been missed. I advise against the use of getch(). It is a platform dependent function that does not comply to ANSI standards.

I got your point. There is no conio.h on *nix systems, so the code is not portable.
Was This Post Helpful? 0
  • +
  • -

#11 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Holding The Execution Window Open

Posted 19 July 2007 - 10:28 AM

View Postno2pencil, on 19 Jul, 2007 - 09:52 AM, said:

View PostAmadeus, on 19 Jul, 2007 - 09:50 AM, said:

I think my point may have been missed. I advise against the use of getch(). It is a platform dependent function that does not comply to ANSI standards.

I got your point. There is no conio.h on *nix systems, so the code is not portable.


my bad... i've just always used getch() just because of the easy use and the most we programmed in college were win32 console (as i'd say most people looking for help here are doing also) apps hence no need to include *nix systems. Nice write-up though!
Was This Post Helpful? 0
  • +
  • -

#12 raedbenz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-December 06

Re: Holding The Execution Window Open

Posted 22 July 2007 - 01:04 AM

hi..if u use VC++ express edition , simply use Start without DEBUGGING ( Ctrl + F5)..
Was This Post Helpful? 0
  • +
  • -

#13 sh0elace  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 225
  • Joined: 05-August 07

Re: Holding The Execution Window Open

Posted 06 August 2007 - 10:29 PM

How would one do so while working in C, not C++?
Was This Post Helpful? 0
  • +
  • -

#14 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Holding The Execution Window Open

Posted 06 August 2007 - 10:59 PM

Snippet shown by me would not work in case of numeric input failures since ignore does not clear the fail state of input stream.. More complete solution could be something like:

#include <iostream>
#include <limits>

void waitForInput()
{
	std::cout <<"Press Enter to exit"<<std::endl;
	if (!std::cin)
	{
		std::cin.clear();// clear the fail flag since ignore does not do that.
	}
	//Clean the stream and ask for input
	std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
	std::cin.get();
}
int main()
{

	// Rest of the code

	waitForInput();

}

This post has been edited by Xing: 06 August 2007 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

#15 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Holding The Execution Window Open

Posted 06 August 2007 - 11:24 PM

View Postsh0elace, on 7 Aug, 2007 - 10:59 AM, said:

How would one do so while working in C, not C++?

You can wait for user to hit enter using this
printf("Hit 'ENTER' to exit"\n");
fflush(stdout);
(void)getchar();

This post has been edited by Xing: 06 August 2007 - 11:25 PM

Was This Post Helpful? 0
  • +
  • -

#16 sh0elace  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 225
  • Joined: 05-August 07

Re: Holding The Execution Window Open

Posted 07 August 2007 - 09:11 AM

View PostXing, on 7 Aug, 2007 - 01:24 AM, said:

View Postsh0elace, on 7 Aug, 2007 - 10:59 AM, said:

How would one do so while working in C, not C++?

You can wait for user to hit enter using this
printf("Hit 'ENTER' to exit"\n");
fflush(stdout);
(void)getchar();


Ahh, thank you thank you! You just helped me out a bunch.
Was This Post Helpful? 0
  • +
  • -

#17 ajaymatrix  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: Holding The Execution Window Open

Posted 14 August 2007 - 12:10 PM

that cleared a lot of my doubts..
good article...
Was This Post Helpful? 0
  • +
  • -

#18 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: Holding The Execution Window Open

Posted 04 October 2007 - 08:08 AM

I learned to create a batch file using notepad (when using windows OS)

if the name of my program was "Game_Over" i would type

Game_Over.exe
pause



and i would just execute the batch file. but the only problem is that the name of the exe. file has to be one word.
Was This Post Helpful? 0
  • +
  • -

#19 Smarf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: Holding The Execution Window Open

Posted 12 October 2007 - 04:54 AM

View Postraedbenz, on 22 Jul, 2007 - 01:04 AM, said:

hi..if u use VC++ express edition , simply use Start without DEBUGGING ( Ctrl + F5)..


I tried to bring this point up with someone in my class but not even my teacher understood what I was trying to say.

If you make the program for someone other than yourself, they aren't going to open up your code in an IDE (like Visual XX Express Edition) to run it. They're going to use the little exe file that comes with it like they do for all their other programs. So Ctrl + F5 doesn't work.
Was This Post Helpful? 0
  • +
  • -

#20 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Holding The Execution Window Open

Posted 14 October 2007 - 07:57 AM

...yes but the truth is, as a user, it is really IRRITATING to have to press a key when a program finishes. One of the really nice things about a command line program is they you can put them into a batch file. Then you can use piping to take the output of one program and insert in as the input to another. This becomes problematic if user interaction is needed.

The idea of holding the execution window open is great for assignments, and ok in some programs, but not great for utility programs that a real user may use.
Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »