Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-04-2003, 12:42 PM   #1 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
system(PAUSE);

i've seen several people use system("PAUSE") when they want to delay their programs. i'm not sure who's teaching this method, but it's a bad habit.
by calling system(), you are invoking the default shell. the shell then executes the commandline given to it ("PAUSE", in this case). that is, it runs the 'pause.exe' program. so, now your simple c program is relying on two external programs for a stupid thing like pressing a single key. what if someone deleted or renamed 'pause.exe'? what if someone tried to compile your program on unix, or a mac? it wouldn't work. you'd get an annoying shell message and no pause. besides, why have the overhead of launching 2 programs, when you can accomplish the same thing in c?

so, for those of you who are starting out, here is an implementation in c for use in your programs that does essentially the same thing.

Code:
#ifndef WIN32
 #include <unistd.h>
#endif

#ifdef __cplusplus
 #include <iostream>
  #ifndef WIN32
   #include <stdio.h>
  #endif
 using namespace std;
#else
 #include <stdio.h>
#endif

#define WAIT_MSG "press enter to continue..."

/*
 note that the function "pause" already exists in <unistd.h>
 so i chose user_wait() for it.
*/
void user_wait()
{
  int c;

#ifdef __cplusplus
  cout << WAIT_MSG << endl;
#else
  printf("%s\n", WAIT_MSG);
#endif
  /* eat up characters until a newline or eof */
  do
  {
    c = getchar();
    if(c == EOF) break;
  } while(c != '\n');
}
here is a simple program that uses it

Code:
int main(int argc, char *argv[])
{
  printf("hello\n");
  user_wait();
  printf("goodbye\n");
  return 0;
}
the main problem with it is that console io is usually line buffered, so it requires a newline, instead of 'any' key. there are system-specific ways to override this (which is what 'pause.exe' does), but this code should be portable to most systems.
joe_bruin is offline   Reply With Quote
Old 09-26-2004, 08:39 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
ADDENDUM: standard C++

The code below occasionally works...
Code:
#include <iostream>
int main(int argc, char *argv[])
{
  std::cin.get();
  return 0;
}
...but does not suffice.
In that case some do advice to add
Code:
std::cin.sync();
before std::cin.get(); to clear the stream buffer. That might easely not suffice as well. You'll need to add a little more code to make it work after input (e.g. cin>>MyVar). I'll present you the code below wich will work quite effortlessly:
Code:
#include <iostream>
#include <string>

//Namespace std to keep this small code readable.
//However, one shouldn't use the standard namespace globally the
//way I do here.
using namespace std;

void reset_istream(istream & is)
{
   char ch;
   // Reset the state.
   is.clear();
   // Remove all characters until we find a newline or EOF.
   ch = is.get();
   while ((ch != '\n')&&(ch != EOF))
      ch = is.get();
   is.clear();
}

void wait_for_enter()
{
   cout << "press <enter> to continue...\n";
   // Reset failstate, just in case.
   cin.clear();
   string line;
   getline( cin, line);
}

int main()
{
   //Your code here;   

   reset_istream(cout);
   wait_for_enter();

   return 0;
}
This should work in most cases. Notice that sometimes you don't need "reset_istream()" at all. You'll find out soon enough so no worries.

- Val -
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
system(PAUSE); joe_bruin Standard C, C++ 3 11-04-2003 01:18 PM


All times are GMT -8. The time now is 07:35 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting