Holding The Execution Window Open

Title 2: How do I pause for input?

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

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

#16 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: Holding The Execution Window Open

Posted 21 October 2007 - 02:33 PM

it closes when it goes to the third line
first line hitting return ok
second hitting return it closes

and no2pencil no cin
same as read only?

is there no other way that wont close
even hitting return until you die?
Was This Post Helpful? 0
  • +
  • -

#17 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Holding The Execution Window Open

Posted 17 December 2007 - 04:24 PM

if using c can you just us getchar()?
as long as the buffer is clear anyway?
Was This Post Helpful? 0
  • +
  • -

#18 draike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-February 08

Re: Holding The Execution Window Open

Posted 03 February 2008 - 12:14 AM

ok i hope people are still reading this and putting in there input cause i myself am a student and would like new , interesting and just plain standard ways of using C++

http://nehe.gamedev....n.asp?lesson=01
will be the source of the base of this code...

if you are not using <windows.h> just ignore this post
/* PSUEDO */
add a bool array of 256;
add a MSG;
send to registered WNDPROC
determine wether they key is acceptable or not for exiting or any
other command
and if so exit...

exa:CODE
/* global if you want */
bool keys[256];
/* WINMAIN - just for kicks */
bool exit;
MSG msg; // no further handling needed
/* where you want input without continuing */
while (!exit)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// do whatever from send to WNDPROC to asserting the keys here
}
// if you send to WNDPROC(HWND,MSG,HWORD,LWORD) make sure that
// you test when finished ^note there is not bool* so thats why global
// to make sure that exit can be true by
if ( keys[VALUE_FOR_EXIT] ) exit=true;
}

This post has been edited by draike: 03 February 2008 - 12:18 AM

Was This Post Helpful? 0
  • +
  • -

#19 P4L  Icon User is offline

  • Your worst nightmare
  • member icon

Reputation: 34
  • View blog
  • Posts: 2,788
  • Joined: 07-February 08

Re: Holding The Execution Window Open

Posted 14 February 2008 - 08:15 AM

This has definitely helped me out since my school teaches programming concepts in C
Was This Post Helpful? 0
  • +
  • -

#20 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Holding The Execution Window Open

Posted 22 March 2008 - 04:28 AM

I generally (when testing my program) just do
int main()
{
char garbage;
{
body
}
cin<<garbage;
return 0;
}


This post has been edited by Sepanto: 22 March 2008 - 04:29 AM

Was This Post Helpful? -1
  • +
  • -

#21 bad_karma00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 10-March 08

Re: Holding The Execution Window Open

Posted 27 March 2008 - 06:55 PM

I'm just a beginner but we've been taught to use the following:


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

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


Was This Post Helpful? 1
  • +
  • -

#22 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Holding The Execution Window Open

Posted 09 April 2008 - 04:09 AM

The below code will "pause" the console. Plus, it's ANSI C, so it'll work in C/C++
#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

#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
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.
#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: 09 April 2008 - 04:47 AM

Was This Post Helpful? -1
  • +
  • -

#23 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: Holding The Execution Window Open

Posted 19 April 2008 - 08:02 PM

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. 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!
Was This Post Helpful? 0
  • +
  • -

#24 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Holding The Execution Window Open

Posted 23 April 2008 - 03:23 AM

View Postxtreampb, on 4 Oct, 2007 - 08:08 AM, said:

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.


Quoting a really old post here, but just thought I'd have a little input :)

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

Was This Post Helpful? 0
  • +
  • -

#25 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 24 April 2008 - 07:58 AM

View Postmikeblas, on 19 Apr, 2008 - 11:02 PM, 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. 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. :)
Was This Post Helpful? 0
  • +
  • -

#26 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Holding The Execution Window Open

Posted 10 July 2008 - 12:12 PM

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

DON'T!

here's why...
Was This Post Helpful? 0
  • +
  • -

#27 CalumJR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 10-May 08

Re: Holding The Execution Window Open

Posted 06 August 2008 - 10:52 AM

I have a question please.

Our tutors at university have always said to use
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.: 06 August 2008 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#28 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Holding The Execution Window Open

Posted 06 August 2008 - 11:02 AM

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:
#include <iostream>

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

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

Was This Post Helpful? 1

#29 CalumJR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 10-May 08

Re: Holding The Execution Window Open

Posted 06 August 2008 - 11:39 AM

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:
cin.get();
in future & see how I get along :)
Was This Post Helpful? 0
  • +
  • -

#30 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Holding The Execution Window Open

Posted 06 August 2008 - 11:43 AM

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. :)
Was This Post Helpful? 0
  • +
  • -

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