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

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

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




Holding The Execution Window Open

5 Pages V < 1 2 3 4 > »   

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

bad_karma00

RE: Holding The Execution Window Open

27 Mar, 2008 - 05:55 PM
Post #21

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 33



Thanked: 1 times
My Contributions
I'm just a beginner but we've been taught to use the following:


CODE

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
    int stop;
    
    
    
    
    
    cin >> stop;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Cerolobo

RE: Holding The Execution Window Open

9 Apr, 2008 - 03:09 AM
Post #22

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 41 times
My Contributions
The below code will "pause" the console. Plus, it's ANSI C, so it'll work in C/C++
CODE

#include <stdio.h>

int main(void)
{
  printf("Hit \"Enter\" to continue\n");
  fflush(stdin); /* Remove anything that is currently in the standard input */
  getchar();     /* Wait for the user to hit the enter key */
  fflush(stdin); /* Remove any additional charcters the user may have inputed */

  return 0;
}


Of course, this is a lot more useful as a macro

CODE

#include <stdio.h>

#define PAUSE {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);}

int main(void)
{
  PAUSE

  return 0;
}


PAUSE contains it's own scope, so it will not produce adverse effects. IE, this will work
CODE

if(foo)
  PAUSE;


Now, I personally don't like to have to key a extra key to quit, but I love the macro for debugging, as long as you remove the printf(). For example, useing a non recursive string reversal.
CODE

#include <stdio.h>  /* fflush(), getchar(), printf(), stdin */
#include <string.h> /* strlen() */

#define PAUSE {fflush(stdin);getchar();fflush(stdin);}

void ReverseString(char *str);

int main(int argc, char *argv[])
{
  if(argc != 2)
  {
    printf("Usage: %s \"TEXT\"\n", argv[0]);
    return 0;
  }

  ReverseString(argv[1]);

  printf("%s\n", argv[1]);

  return 0;
}

void ReverseString(char *str)
{
  int len = strlen(str) - 1;
  int i;
  char c;

  for(i = 0; i < len; ++i, --len)
  {
     c        = str[i];
     str[i]   = str[len];
     str[len] = c;

/* Note: These are offset to the left to make it apparent they are debug data */
{
int j;
printf("%s\n", str);
for(j = 0; j < i; ++j)
  putchar(' ');
putchar('^');
for(; j < len - 1; ++j)
  putchar(' ');
putchar('^');
PAUSE;
putchar('\n');
}
  }
}



Edit: Forgot a fflush(stdin);

This post has been edited by Cerolobo: 9 Apr, 2008 - 03:47 AM
User is offlineProfile CardPM
+Quote Post

mikeblas

RE: Holding The Execution Window Open

19 Apr, 2008 - 07:02 PM
Post #23

D.I.C Regular
Group Icon

Joined: 8 Feb, 2008
Posts: 390



Thanked: 27 times
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. It is not part of standard C++.

Are developers who don't know simple things, like how to stop the debugger from closing the console window after execution has stopped, really interested in porting code to multiple platforms?

Better than any of these recommendations, in my opinion, is to learn how to use the debugger. If someone wants to view the output before the application closes, then they should place a breakpoint in the code before the application closes. Debugging is an important skill. You can't start too early!
User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

23 Apr, 2008 - 02:23 AM
Post #24

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
QUOTE(xtreampb @ 4 Oct, 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

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.


Quoting a really old post here, but just thought I'd have a little input smile.gif

The file doesn't have to be one word, you could do something like this:
CODE
"game over.exe"
pause

User is offlineProfile CardPM
+Quote Post

Amadeus

RE: Holding The Execution Window Open

24 Apr, 2008 - 06:58 AM
Post #25

g+ + -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 13,284



Thanked: 149 times
Dream Kudos: 25
My Contributions
QUOTE(mikeblas @ 19 Apr, 2008 - 11:02 PM) *

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. It is not part of standard C++.

Are developers who don't know simple things, like how to stop the debugger from closing the console window after execution has stopped, really interested in porting code to multiple platforms?

Better than any of these recommendations, in my opinion, is to learn how to use the debugger. If someone wants to view the output before the application closes, then they should place a breakpoint in the code before the application closes. Debugging is an important skill. You can't start too early!

An excellent point...but I fully expect that well over 95% of the users of this board not to use a debugger. Further, i usually try to endorse proper debugging through the use of old school debugging techniques - the manual insertion of specific breakpoints in the code.

If you review many of the topics on the board that ask about holding the execution window open, most are not trying to track bugs...they are only trying to see the end result, but have only ever learned how to program using an IDE and do not understand how that IDE works. Despite the fact that I have grave reservations about programmers who ONLY know how to use an IDE, I try to at least provide them with some basic understanding of that IDE's functionality.

Frankly, if I had the time to explain how to use the debugger (or debug mode) of whatever IDE/compiler they are using, I'd point them towards the documentation for that specific IDE/complier and use the time to have a beer. smile.gif

User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

10 Jul, 2008 - 11:12 AM
Post #26

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
So many people use system("pause"); in Windows console apps...

DON'T!

here's why...
User is offlineProfile CardPM
+Quote Post

CalumJR

RE: Holding The Execution Window Open

6 Aug, 2008 - 09:52 AM
Post #27

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 47


My Contributions
I have a question please.

Our tutors at university have always said to use
CODE
while(!_kbhit());
in order to hold the screen for input. This is in the conio.h library & it works perfectly in Visual Studio 2005.

What I'd like to know is why hasn't this really been mentioned much & why doesn't it seem to be used commonly?

Also, what would be the best code to use, which does the same function as kbhit, which is cross-platform & cross-compiler?

Thanks in advance.
cJr.

This post has been edited by cJr.: 6 Aug, 2008 - 10:25 AM
User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

6 Aug, 2008 - 10:02 AM
Post #28

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
I'll be honest with you, I've never actually seen that before.

However, I would still advise against the use of it, since conio.h does not conform to all standards, and it isn't going to be around forever. I always see a lot of people using conio.h in their programs~ granted, it has some very useful functions, but it's best to stick to the standards.

Personally, I tend to use cin.get (); (in C++ anyway)
Granted, it waits for the user to press enter, but we could just prompt them:
cpp
#include <iostream>

void pause ()
{
std::cout << "\nPress the enter key to continue . . .";
std::cin.get ();
}

int main ()
{
pause();
return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

CalumJR

RE: Holding The Execution Window Open

6 Aug, 2008 - 10:39 AM
Post #29

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 47


My Contributions
Thank you gabehabe. I knew the way I'm being taught wasn't the best one to use after seeing this thread, but I just didn't know why.

You've cleared it up for me now, thank you.

I will try & use:
CODE
cin.get();
in future & see how I get along smile.gif
User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

6 Aug, 2008 - 10:43 AM
Post #30

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
Confront your university teachers about it, ask them why they use it.

I personally don't think it's too good an idea, I'm just intrigued as to why a higher-level education would suggest using something like that. smile.gif
User is offlineProfile CardPM
+Quote Post

CalumJR

RE: Holding The Execution Window Open

6 Aug, 2008 - 11:33 AM
Post #31

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 47


My Contributions
Thank you for the idea. I will ask them, especially when now I've just changed all the code in this big program I am making to "cin.get();" instead of "while(!_kbhit());" and it works perfectly! smile.gif lol.
User is offlineProfile CardPM
+Quote Post

KYA

RE: Holding The Execution Window Open

7 Aug, 2008 - 06:52 AM
Post #32

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Weird, I have never seen kbhit either...
User is online!Profile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

18 Aug, 2008 - 01:32 PM
Post #33

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
@cJr:
Did you ask your teachers why they suggested that?
User is offlineProfile CardPM
+Quote Post

KYA

RE: Holding The Execution Window Open

27 Aug, 2008 - 05:29 PM
Post #34

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
laugh.gif
User is online!Profile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

28 Aug, 2008 - 02:33 AM
Post #35

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
That was his first post.

Was that supposed to offend us?
User is offlineProfile CardPM
+Quote Post

KYA

RE: Holding The Execution Window Open

28 Aug, 2008 - 06:55 AM
Post #36

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
I guess...?
User is online!Profile CardPM
+Quote Post

gabehabe

RE: Holding The Execution Window Open

28 Aug, 2008 - 07:13 AM
Post #37

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,864



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

My Contributions
Hey, android 16, was you aiming to insult us?
User is offlineProfile CardPM
+Quote Post

KYA

RE: Holding The Execution Window Open

28 Aug, 2008 - 07:19 AM
Post #38

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
We don't take kindly to fellows....


I'm looking for the guy who shot my Pa.
User is online!Profile CardPM
+Quote Post

CalumJR

RE: Holding The Execution Window Open

28 Aug, 2008 - 11:36 AM
Post #39

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 47


My Contributions
QUOTE(gabehabe @ 18 Aug, 2008 - 10:32 PM) *

@cJr:
Did you ask your teachers why they suggested that?



No, I haven't been able to yet as I haven't been back to uni. I start again in late September. But I will try & email one of them if f I can.
User is offlineProfile CardPM
+Quote Post

RedSonja

RE: Holding The Execution Window Open

4 Sep, 2008 - 12:55 AM
Post #40

D.I.C Head
Group Icon

Joined: 4 Sep, 2008
Posts: 170



Thanked: 4 times
Dream Kudos: 25
My Contributions
Cheap and nasty way I use every day to start exe files and see what they are doing (I do a lot of old code).


Make a little text file, call it run.bat. Put this in it:

MyStuff.exe
pause



double click it, a DOS window opens, your exe writes its stuff in there and won't go away till you tell it to.




User is offlineProfile CardPM
+Quote Post

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

Time is now: 11/21/09 03:35PM

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