Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,165 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,501 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! 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
post 18 Jul, 2007 - 06:02 PM
Post #1


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,879



Thanked 23 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 online!Profile CardPM

Go to the top of the page


no2pencil
post 18 Jul, 2007 - 07:09 PM
Post #2


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 5,388



Thanked 35 times

Dream Kudos: 2325

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 offlineProfile CardPM

Go to the top of the page

Topher84
post 19 Jul, 2007 - 08:06 AM
Post #3


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 226



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 - 08:09 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Jul, 2007 - 09:50 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,879



Thanked 23 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 online!Profile CardPM

Go to the top of the page

no2pencil
post 19 Jul, 2007 - 09:52 AM
Post #5


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 5,388



Thanked 35 times

Dream Kudos: 2325

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 offlineProfile CardPM

Go to the top of the page

Topher84
post 19 Jul, 2007 - 10:28 AM
Post #6


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 226



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

Go to the top of the page

raedbenz
post 22 Jul, 2007 - 01: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

Go to the top of the page

sh0elace
post 6 Aug, 2007 - 10: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

Go to the top of the page

Xing
post 6 Aug, 2007 - 10: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 - 11:15 PM
User is offlineProfile CardPM

Go to the top of the page

Xing
post 6 Aug, 2007 - 11: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 - 11:25 PM
User is offlineProfile CardPM

Go to the top of the page

sh0elace
post 7 Aug, 2007 - 09: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

Go to the top of the page

ajaymatrix
post 14 Aug, 2007 - 12:10 PM
Post #12


D.I.C Regular

Group Icon
Joined: 15 May, 2007
Posts: 382



Dream Kudos: 100
My Contributions


that cleared a lot of my doubts..
good article...
User is offlineProfile CardPM

Go to the top of the page

4 Pages V  1 2 3 > » 
Reply to this topicStart new topic
Time is now: 10/6/08 11:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month