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

Join 136,948 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,876 people online right now. Registration is fast and FREE... Join Now!




Holding The Execution Window Open

4 Pages V  1 2 3 > »   
Reply to this topicStart new topic

Holding The Execution Window Open, Title 2: How do I pause for input?

Amadeus
18 Jul, 2007 - 05:02 PM
Post #1

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,228



Thanked: 40 times
Dream Kudos: 25
My Contributions
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:
CODE

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:
CODE

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:
CODE

#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.dreamincode.net/code/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. smile.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Holding The Execution Window Open
18 Jul, 2007 - 06:09 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
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:

CODE

#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");
    }
  }
}


User is online!Profile CardPM
+Quote Post

Topher84
RE: Holding The Execution Window Open
19 Jul, 2007 - 07:06 AM
Post #3

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


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

CODE

#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 Jul, 2007 - 07:09 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Holding The Execution Window Open
19 Jul, 2007 - 08:50 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,228



Thanked: 40 times
Dream Kudos: 25
My Contributions
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++.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Holding The Execution Window Open
19 Jul, 2007 - 08:52 AM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Amadeus @ 19 Jul, 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.

I got your point. There is no conio.h on *nix systems, so the code is not portable.
User is online!Profile CardPM
+Quote Post

Topher84
RE: Holding The Execution Window Open
19 Jul, 2007 - 09:28 AM
Post #6

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
QUOTE(no2pencil @ 19 Jul, 2007 - 09:52 AM) *

QUOTE(Amadeus @ 19 Jul, 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.

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!

User is offlineProfile CardPM
+Quote Post

raedbenz
RE: Holding The Execution Window Open
22 Jul, 2007 - 12:04 AM
Post #7

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 19


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

User is offlineProfile CardPM
+Quote Post

sh0elace
RE: Holding The Execution Window Open
6 Aug, 2007 - 09:29 PM
Post #8

D.I.C Head
Group Icon

Joined: 5 Aug, 2007
Posts: 225


Dream Kudos: 25
My Contributions
How would one do so while working in C, not C++?
User is offlineProfile CardPM
+Quote Post

Xing
RE: Holding The Execution Window Open
6 Aug, 2007 - 09:59 PM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
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:

CODE
#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: 6 Aug, 2007 - 10:15 PM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Holding The Execution Window Open
6 Aug, 2007 - 10:24 PM
Post #10

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(sh0elace @ 7 Aug, 2007 - 10:59 AM) *

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

You can wait for user to hit enter using this
CODE

printf("Hit 'ENTER' to exit"\n");
fflush(stdout);
(void)getchar();


This post has been edited by Xing: 6 Aug, 2007 - 10:25 PM
User is offlineProfile CardPM
+Quote Post

sh0elace
RE: Holding The Execution Window Open
7 Aug, 2007 - 08:11 AM
Post #11

D.I.C Head
Group Icon

Joined: 5 Aug, 2007
Posts: 225


Dream Kudos: 25
My Contributions
QUOTE(Xing @ 7 Aug, 2007 - 01:24 AM) *

QUOTE(sh0elace @ 7 Aug, 2007 - 10:59 AM) *

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

You can wait for user to hit enter using this
CODE

printf("Hit 'ENTER' to exit"\n");
fflush(stdout);
(void)getchar();



Ahh, thank you thank you! You just helped me out a bunch.
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: Holding The Execution Window Open
14 Aug, 2007 - 11:10 AM
Post #12

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 389



Thanked: 1 times
Dream Kudos: 100
My Contributions
that cleared a lot of my doubts..
good article...
User is offlineProfile CardPM
+Quote Post

4 Pages V  1 2 3 > » 
Reply to this topicStart new topic
Time is now: 12/3/08 11:28PM

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