I hear alot of people saying not use:

system("pause");
system("cls");
system("title");

I think you get what I mean, so I'm trying to make functions that will replace all of these, and for my first installment I'm going to show you how to replace system("pause");. Here is what I have.

#include <iostream>
#include <conio.h>

void PrsAnyKey2Cont ();
char KeyStroke;

void PrsAnyKey2Cont ()
{
     cout << "Press any key to continue . . .\n";
     KeyStroke = getch ();
     KeyStroke = getch ();
     switch (KeyStroke)
     {
          default :
          break;
     }
}

int main ()
{
     cout << "Hello world!!" << endl;
     PrsAnyKey2Cont ();
     return 0;
}

There are probably a million things wrong with this right now and I plan on making a better function that will allow you to set the time of the pause and wether or not the user even needs to input a key to continue. Thanks for any constructive critisism!! ^_^

>Thanks for any constructive critisism!!
PrsAnyKey2Cont is a very awkward name.

Yeah, I know, but I couldn't think of anything short and descriptive. XD Do you have any seggestions for a better name?

>Do you have any seggestions for a better name?
At the risk of borrowing an existing name, "pause" is short and sweet. Throwing it in a "console" namespace is descriptive, and I can type and read console::pause so much faster than PrsAnyKey2Cont. :)

Excuse me for sounding like a total noob here for a second, but what is a "console namespace". To tell you the truth I've been writing:

using namespace std;

at the beginning of all of my programs for almost 2 years and I've never learned what it meant. Sorry. =[

I BELIEVE that a namespace is just something used to give context to where something should be used. Also, it helps to avoid conflicting names.

So, what does it mean to say std::pause?

Is there a pause in std? do you mean std::system("pause") ?
Well anyways, since 'pause' is/would be in namespace std, you must first 'access' the std namespace.

Think of it as a box where you put things to keep them either organized, or from getting jumbled with other things. You can't simply remove something from the box without first opening the box... (Said box has a lid...)

Yeah I just forgot to put using namespace std;, which would open the box, right?

More like dumping the box all over the floor... Because once the box is opened (using namespace std; ) you no longer have to open it each time you want something from it (std:wantedObject)

So once I open it how do I know whats in it? Do I have to declare std::functionname (); for that object to be in the box?

>Excuse me for sounding like a total noob here for a
>second, but what is a "console namespace".
You can create your own namespaces:

namespace console {
  void pause()
  {
    //...
  }
}

Now you can say console::pause(); , you can open the namespace with using namespace console; , or just the pause function with using console::pause; . Just like with the std namespace.

Well, you can do it the portable way (there is more than one way to do it the portable way; this is only my latest variation for y'all):

#include <iostream>
#include <limits>

namespace console {
  void pause() {
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
  }

Or, since all the other things you want to do (like clear the screen) are non-portable anyway, you can do it the Windows way:

#include <iostream>
#include <windows.h>

namespace console {
  void pause() {
    DWORD        mode;
    HANDLE       hin;
    INPUT_RECORD inrec;
    DWORD        count;

    // Instruct the user
    std::cout << "Press the 'any' key...";

    // Set the console mode to no-echo, raw input,
    // and no window or mouse events.
    hin = GetStdHandle( STD_INPUT_HANDLE );
    GetConsoleMode( hin, &mode );
    SetConsoleMode( hin, 0 );

    // Wait for and get a single key press
    do ReadConsoleInput( hin, &inrec, 1, &count );
    while (inrec.EventType != KEY_EVENT);

    // Restore the original console mode
    SetConsoleMode( hin, mode );
    }
  }

You can use it the usual way:

int main() {

  std::cout << "Hello world!\n" << std::endl;

  console::pause();

  std::cout << "\n\nThank you!" << std::endl;

  return EXIT_SUCCESS;
  }

Enjoy!

Using namespace std tells the compiler where the functions following it will be located - in this case, it is the standard library, hence std. Alternatively, you could put std:: (code) before every line to specify that the following code is from the standard library, but this method would be cumbersome and would waste time.

you could put std:: (code) before every line to specify that the following code is from the standard library, but this method would be cumbersome and would waste time.

That's a pretty powerful, self-assured opinion you've got there.

It is neither cumbersome nor does it waste time. What it does is prevent obnoxious namespace errors and mistakes. IMO, no library code should ever have a using namespace [I]foo[/I]; statement, unless you can guarantee that it stays in local blocks. Which namespaces are visible, and where, is for the person programming the application to decide, not the libraries he is using.

In other words, portable, well-written, library code doesn't dump namespaces together without your OK.

It's not just my opinion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.