C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

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

Join 307,102 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,017 people online right now. Registration is fast and FREE... Join Now!




Holding The Execution Window Open

5 Pages V < 1 2 3 4 5 >  

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

gabehabe

RE: Holding The Execution Window Open

4 Sep, 2008 - 01:48 AM
Post #41

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



Thanked: 177 times
Dream Kudos: 3275
Expert In: Lots of things.

My Contributions
Seems a little pointless, and it will only run on Windows.

All that you're achieving by putting "pause" at the end of that batch file is the equivalent to this:
system ("pause");
User is offlineProfile CardPM
+Quote Post

RedSonja

RE: Holding The Execution Window Open

4 Sep, 2008 - 02:25 AM
Post #42

D.I.C Head
Group Icon

Joined: 4 Sep, 2008
Posts: 170



Thanked: 4 times
Dream Kudos: 25
My Contributions
QUOTE(gabehabe @ 4 Sep, 2008 - 02:48 AM) *

Seems a little pointless, and it will only run on Windows.

All that you're achieving by putting "pause" at the end of that batch file is the equivalent to this:
system ("pause");



Yes, you are right. A great deal of what I do seems pointless. Unfortunately I am obliged to work under Windows at the moment. (Some day the customer will want Vista, oh my.) Me, I dream in Unix. I do so miss vi.

Anyway, the point is, my batch file method works without changing the code. Sometimes I only have the exe and no source code.
User is offlineProfile CardPM
+Quote Post

krzysz00

RE: Holding The Execution Window Open

2 Mar, 2009 - 03:13 PM
Post #43

D.I.C Head
Group Icon

Joined: 25 Feb, 2009
Posts: 78



Thanked: 2 times
Dream Kudos: 100
My Contributions
getch(); is in the UNIX header ncurses.h . Link with -lncurses
User is offlineProfile CardPM
+Quote Post

webmin

RE: Holding The Execution Window Open

17 Mar, 2009 - 03:00 PM
Post #44

D.I.C Head
**

Joined: 21 May, 2008
Posts: 230



Thanked: 2 times
My Contributions
I think I've created a platform independent method of using system commands like "cls", "clear", or "pause", but I really need someone with a linux machine to test this on. Of course credit goes to some of the solutions I've seen on here, and one of my classmates who helped me specifically with automating the "cls" or "clear" process. Here's the code seeing as I can't upload header files:

cpp

#include <stdlib.h> //For system commands

//Detect and define which command to use
#ifndef WIN32
#define COMMAND "clear" //Clears a linux console screen
#else
#define COMMAND "cls" //Clears a windows console screen
#endif

#define wipe() system( COMMAND )
//To clear the console screen use wipe();

#define wait() {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);}
//To pause the program or hold the console screen open use wait();


The commands should be pretty well commented, but just in case use:

wipe(); //This will clear the console screen
wait(); //This will pause the program waiting for input i.e a enter key pressed

I will even make this as easy as I can for you. Here's a program you can test this with:

cpp

#include <stdio.h>
#include <stdlib.h> //For system commands

//Detect and define which command to use
#ifndef WIN32
#define COMMAND "clear" //Clears a linux console screen
#else
#define COMMAND "cls" //Clears a windows console screen
#endif

#define wipe() system( COMMAND )
//To clear the console screen use wipe();

#define wait() {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);}
//To pause the program or hold the console screen open use wait();

int main (void)
{
printf("This is the console screen before we clear it.\n");
wait();
wipe();
printf("A new line in the console screen. Looks like we cleared the screen successfully");
wait();
}


Check that on your system for me and let me know how it works. Thanks.

This post has been edited by webmin: 17 Mar, 2009 - 08:25 PM
User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

18 Mar, 2009 - 04:48 AM
Post #45

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



Thanked: 177 times
Dream Kudos: 3275
Expert In: Lots of things.

My Contributions
But you're using system() sad.gif

QUOTE(gabehabe @ 10 Jul, 2008 - 06:12 PM) *
So many people use system("pause"); in Windows console apps...

DON'T!

here's why...


Also, as an aside: you've covered win and linux but what's the Mac command to do those things?

But still... system() sad.gif

This post has been edited by gabehabe: 18 Mar, 2009 - 04:49 AM
User is offlineProfile CardPM
+Quote Post

webmin

RE: Holding The Execution Window Open

18 Mar, 2009 - 09:09 AM
Post #46

D.I.C Head
**

Joined: 21 May, 2008
Posts: 230



Thanked: 2 times
My Contributions
About the mac commands, I didn't really even think of them, nor do I know them (It's been about six years since I actually used a mac). If anyone would be willing to contribute those commands, I may be able to incorporate them.

As for the system commands, they are only used on on the clear, and cls commands, which was the simplest way I found to dealing with clearing the console screen. I do see what you mean by the system commands being a little taxing on the system trying to find the command, waiting for input, and then exiting the command. That's what makes system ("pause") so bad. However cls, and clear, while they work on the same principle they don't have to wait for user input to clear the console. Also the command works near instantaneous. They way I see it, if your computer has a problem taking on these commands, then you need a new machine to program on. And I can say that I at least tried to account for it being used on another system, of which the only one I didn't account for is a MAC.

If someone has a mac and would like to test this, go ahead, and then post the results here, or if you know the specific commands to clear your console screen, or pause the program, then by all means please share. This is going into a personal project of mine to create a header file that can perform these tasks with simplicity and ease. Please let me know if you have any tips or suggestions. Thanks
User is offlineProfile CardPM
+Quote Post

Fussion289

RE: Holding The Execution Window Open

20 Mar, 2009 - 12:11 PM
Post #47

New D.I.C Head
*

Joined: 20 Mar, 2009
Posts: 5



Thanked: 1 times
My Contributions
One way I've always paused the CMD prmt was simple. Inswert a dummy string or int and request it right before the return 0; or the last }, whichever method of C++ you use. Like so:

CODE

#include <string>
int main()
{
      //some code your doing here
      string QUIT; //now you could just as easily use char or int
                         //string is just more fail safe.
      std::cin >> QUIT;
     return 0;
}


not the best but would be platform good no matter where just don't forget to always #include <string>.

This post has been edited by Fussion289: 20 Mar, 2009 - 12:13 PM
User is offlineProfile CardPM
+Quote Post

computerfox

RE: Holding The Execution Window Open

21 Mar, 2009 - 07:35 AM
Post #48

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
system("pause") isn't really sufficient because you have to go into the system and i hate it when professors require you to use it.

instead when i would use dev (since i use xcode now) i would use cin.get() because you just press enter and it seems a lot more sufficient.
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Holding The Execution Window Open

20 Apr, 2009 - 08:01 PM
Post #49

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 712



Thanked: 9 times
Dream Kudos: 125
My Contributions
QUOTE(xtreampb @ 4 Oct, 2007 - 07: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

CODE

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.


in the case a batch file, a better solution would be

CODE

start "" "Game Over.exe"
pause
cls
exit


User is online!Profile CardPM
+Quote Post

saxasm

RE: Holding The Execution Window Open

23 Apr, 2009 - 10:38 AM
Post #50

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 32



Thanked: 1 times
My Contributions
The methods posted here don't seem to work if you have multiple exit points like,
CODE

#include <iostream>
#include <string>

#include "DummyClass.h"

using namespace std;

int main ( int argc, char* argv[] )
{
     Dummy A_Dummy;

     if (!A_Dummy.Do_Whatever_The_Dummy_should_Do())
          return 1;

     A_Dummy.PrintResult();
     // Whatever you use to keep the console open here
     return 0;
}


Your code will only catch the default exit and not the much more interesting error messages.

So i found this to be useful, might be some fault in it but it is standard and seems to be working:
CODE

#include <iostream>
#include <string>

#include "DummyClass.h"

using namespace std;
void KeepConsoleWindowOpen()
{
//Whatever code you use for that here
}
int main ( int argc, char* argv[] )
{
     atexit(KeepConsoleWindowOpen);
     Dummy A_Dummy;

     if (!A_Dummy.Do_Whatever_The_Dummy_should_Do())
          return 1;

     A_Dummy.PrintResult();
     // Whatever you use to keep the console open here
     return 0;
}


It will catch all exits, i think.
User is offlineProfile CardPM
+Quote Post

computerfox

RE: Holding The Execution Window Open

23 Apr, 2009 - 02:28 PM
Post #51

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
cin.get(); after everything else...
User is offlineProfile CardPM
+Quote Post

enter.net

RE: Holding The Execution Window Open

30 Apr, 2009 - 12:41 PM
Post #52

New D.I.C Head
*

Joined: 28 Apr, 2009
Posts: 7


My Contributions
it is good to find a C basic ebook

This post has been edited by enter.net: 30 Apr, 2009 - 12:41 PM
User is offlineProfile CardPM
+Quote Post

thegamewriter

RE: Holding The Execution Window Open

13 May, 2009 - 08:14 AM
Post #53

New D.I.C Head
*

Joined: 1 Jan, 2009
Posts: 17


My Contributions
here's how I usually pause my programs...

First of all, I allways use my header "cbpgm.h" in my work.

One of the first things I put into it was this:

CODE

void pause(void){
     while(1 == 1){/*if one does not equal one, please contact your
                           *nearest physicist*/}
}


so I just call it by having
CODE
pause();


I also just put a simple scanf routine in
CODE

int main(){
     char input;
     printf("Press ENTER to continue\n");
     scanf("%s\n", input);
     return 0;
}


This post has been edited by thegamewriter: 13 May, 2009 - 08:14 AM
User is offlineProfile CardPM
+Quote Post

ImaSexy

RE: Holding The Execution Window Open

9 Jun, 2009 - 04:14 PM
Post #54

D.I.C Addict
Group Icon

Joined: 9 Jun, 2009
Posts: 911



Thanked: 63 times
Dream Kudos: 125
My Contributions
Why not create a function like this
CODE

#include <limits>
void pause()
{
cout<<" 'push enter to quit' "<<endl;
cin.sync();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}

User is offlineProfile CardPM
+Quote Post

codefreak.

RE: Holding The Execution Window Open

13 Jul, 2009 - 11:47 AM
Post #55

D.I.C Head
Group Icon

Joined: 10 Jul, 2009
Posts: 79



Thanked: 3 times
Dream Kudos: 25
My Contributions
This solution will pause for user input even with characters currently in the stream.

CODE

#include <iostream>
using namespace std;

int main()
{
    int stop;





    // Let me see the result.
    cin >> stop;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

erimir

RE: Holding The Execution Window Open

17 Jul, 2009 - 03:25 AM
Post #56

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 22



Thanked: 1 times
My Contributions
good thread, very helpful for those self teaching! keep it up
User is offlineProfile CardPM
+Quote Post

codefreak.

RE: Holding The Execution Window Open

17 Jul, 2009 - 08:01 AM
Post #57

D.I.C Head
Group Icon

Joined: 10 Jul, 2009
Posts: 79



Thanked: 3 times
Dream Kudos: 25
My Contributions
This way is best; it will work with characters in the stream.

CODE

#include <iostream>

using namespace std;


int main()
{
    int stop;





    cin >> stop;
    return 0;
}


This post has been edited by codefreak.: 17 Jul, 2009 - 08:03 AM
User is offlineProfile CardPM
+Quote Post

ImaSexy

RE: Holding The Execution Window Open

17 Jul, 2009 - 10:16 AM
Post #58

D.I.C Addict
Group Icon

Joined: 9 Jun, 2009
Posts: 911



Thanked: 63 times
Dream Kudos: 125
My Contributions
i dont know about the "best way"
User is offlineProfile CardPM
+Quote Post

codefreak.

RE: Holding The Execution Window Open

17 Jul, 2009 - 11:34 AM
Post #59

D.I.C Head
Group Icon

Joined: 10 Jul, 2009
Posts: 79



Thanked: 3 times
Dream Kudos: 25
My Contributions
This solution is standards compliant; it will wait for user input - even if there are characters currently in the stream.

CODE

#include <iostream>

using namespace std;


int main()
{
    int stop;
    /*


        Code...


    */
    // Let me see the result.
    cin >> stop;
    return 0;
}


This post has been edited by codefreak.: 18 Jul, 2009 - 03:32 AM
User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

18 Jul, 2009 - 02:44 PM
Post #60

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



Thanked: 177 times
Dream Kudos: 3275
Expert In: Lots of things.

My Contributions
What's wrong with cin.get();?!
User is offlineProfile CardPM
+Quote Post

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

Time is now: 11/21/09 12:22PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month