Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 4th, 2006, 9:50 AM   #11
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by Jessehk View Post
Sane, you really should stop ending all your C code in system( "pause" ).
It destroys any portability, as other OSs use other commands.

I think using getchar() would work just as well.
c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main( void ) {
  4. puts( "Press any key to exit..." );
  5. getchar();
  6.  
  7. return 0;
  8. }


Jessehk, there are two problems with that code:
If there's something in the buffer (IE. After some instances of scanf), then it will skip right past the prompt.

You need to press enter after typing in a character.
Both of these issues leave it to not behave the same way as system ("pause"), which is the desirable requirement in most instances.

The system ("pause") is just meant to be there as an informal way to add a break to the end of the program. If the code was going to be serious enough to require portability, it would arguably never even need a break at the end in the first place. But if you wanted to replicate its behaviour, you would need to combine an event flush with something like GetAsyncKeyState, in order to remove the need to hit return. Which, I believe... is windows inclusive?

If it's not windows inclusive, is that the best way to handle keystrokes without waiting for return, it seems a little extraneous? Never bothered to look up any alternatives to system ("pause"). I see no need really.

Yeah, it's still Windows inclusive. Ain't it?
Gimme a cheap way out of it!

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <time.h>
  4.  
  5. void waitForRelease (char lower, char upper) {
  6. int key, hasPressed;
  7. do {
  8. hasPressed = 0;
  9. for (key=lower; key<=upper; key++) {
  10. while (GetAsyncKeyState (key)) {
  11. hasPressed = 1;
  12. }
  13. }
  14. } while (hasPressed);
  15. }
  16.  
  17. int getSeconds () {
  18. return clock () / CLOCKS_PER_SEC;
  19. }
  20.  
  21. int waitForKeyStroke (char lower, char upper, int maxWait) {
  22. int key, start = getSeconds ();
  23. while (1) {
  24. for (key=lower; key<=upper; key++) {
  25. if (GetAsyncKeyState (key)) {
  26. return key;
  27. }
  28. }
  29. if (maxWait && (getSeconds () - start) >= maxWait) {
  30. return -1;
  31. }
  32. }
  33. }
  34.  
  35. int systemPause (int maxWait) {
  36. waitForRelease ('\n', 'z');
  37. printf ("Press any key to continue . . .");
  38. return waitForKeyStroke ('\n', 'z', maxWait);
  39. }
  40.  
  41. int main () {
  42. printf ("Hello World!\n\n");
  43. systemPause (15);
  44. return 0;
  45. }

Last edited by Sane; Nov 4th, 2006 at 10:24 AM.
Sane is offline   Reply With Quote
Old Nov 4th, 2006, 6:17 PM   #12
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Jessehk's point was not to use system("pause") in code you post on forums. Posting it liberally in forums like this one implicitly encourages newbies to use it, and it is never a good idea to encourage non-portable techniques unless they are absolutely needed. Furthermore, in most of the examples where you use it, it is completely irrelevant to the problem at hand. Which means you are posting unnecessary information that implicitly encourages newbies to adopt bad practices.

As to a cheap solution under windows, that's easy. When you are creating an executable, all compilers create them in some directory (usually the path of your project, or some other directory that you configure, so you know where it is). I then do the following (assuming I have windows installed, with the windows directory being C:\WINNT and the path of executable being created being C:\whatever)

1) Create a desktop shortcut to C:\WINNT\System32\CMD.exe

2) Change properties of the shortcut so the directory it starts in is C:\whatever.

To use that shortcut;

1) Doubleclick it.

2) Type in the name of the executable and hit Enter. The output from the executable will be displayed in the command window, even after the program exits.

3) Repeat step 2 whenever you want to run the executable.

4) Close the command window when no longer required.

It takes a bit more effort to do this than using system("pause") in your program, but it only needs to be setup (at most) once for every project.
grumpy is offline   Reply With Quote
Old Nov 5th, 2006, 3:12 AM   #13
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
I agree with all the portability issues grumpy said above. Don't most IDE's do this automatically, I mean run the program in such a way that is "pauses" after execution.
Game_Ender is offline   Reply With Quote
Old Nov 5th, 2006, 4:41 AM   #14
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by grumpy
Jessehk's point was not to use system("pause") in code you post on forums. Posting it liberally in forums like this one implicitly encourages newbies to use it, and it is never a good idea to encourage non-portable techniques unless they are absolutely needed. Furthermore, in most of the examples where you use it, it is completely irrelevant to the problem at hand. Which means you are posting unnecessary information that implicitly encourages newbies to adopt bad practices.
Agreed. If I want something like this in my code, I generally write a waitKey() function that uses a platform-specific method to wait for a key. It's generally fairly trivial to implement something like this, and even if it's non-portable, a programmer who sees the function call can usually figure out what it does, and why it's there.
Quote:
Originally Posted by grumpy
As to a cheap solution under windows, that's easy. When you are creating an executable, all compilers create them in some directory (usually the path of your project, or some other directory that you configure, so you know where it is). I then do the following (assuming I have windows installed, with the windows directory being C:\WINNT and the path of executable being created being C:\whatever)

1) Create a desktop shortcut to C:\WINNT\System32\CMD.exe

2) Change properties of the shortcut so the directory it starts in is C:\whatever.

To use that shortcut;

1) Doubleclick it.

2) Type in the name of the executable and hit Enter. The output from the executable will be displayed in the command window, even after the program exits.
You could do all that, or you could be lazy like me, and get the 'open command prompt here' PowerToy from Microsoft's site. It's a shell extension that adds an option to the right-click menu in Explorer, so you can open a command prompt in a folder of your choice. This, along with TweakUI, are a couple on my list of must-haves.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 6th, 2006, 4:26 AM   #15
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by lectricpharaoh View Post
You could do all that, or you could be lazy like me, and get the 'open command prompt here' PowerToy from Microsoft's site. It's a shell extension that adds an option to the right-click menu in Explorer, so you can open a command prompt in a folder of your choice. This, along with TweakUI, are a couple on my list of must-haves.
Each to their own. I use Microsoft's powertoys, but installing a program to achieve what I can do with two or three mouse clicks (the "open command prompt here" powertoy still needs to be supplied with path information) is not exactly lazy. I could be a member of the olympic laziness team, and beat you in this instance.

And, Game_Ender, the reason this comes up as an issue is that most IDE's do not run a console application in a way that pauses after they finish execution. Hence the common workaround is to get the program to wait for input; unfortunately the method that people tend to wish for is equivalent to the microsoft "pause" command, and there is no portable way (but plenty of non-portable ways) to achieve that in C/C++.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
relation between array and pointer n00b C++ 6 Oct 12th, 2006 4:38 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 6:41 PM
create struct from value of a variable mfo6463 C++ 18 Feb 9th, 2006 10:45 AM
Assigning first character of variable into an array IceNeo Visual Basic .NET 1 Nov 10th, 2005 1:13 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC