| | |
Replacing system("pause"); with a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>Excuse me for sounding like a total noob here for a
>second, but what is a "console namespace".
You can create your own namespaces:
Now you can say
>second, but what is a "console namespace".
You can create your own namespaces:
C++ Syntax (Toggle Plain Text)
namespace console { void pause() { //... } }
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. I'm here to prove you wrong.
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):
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:
You can use it the usual way:
Enjoy!
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
int main() { std::cout << "Hello world!\n" << std::endl; console::pause(); std::cout << "\n\nThank you!" << std::endl; return EXIT_SUCCESS; }
Enjoy!
•
•
Join Date: Dec 2007
Posts: 113
Reputation:
Solved Threads: 4
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.
Last edited by zoner7; Jan 14th, 2008 at 9:07 pm.
•
•
•
•
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.
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 foo; 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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Programming student find distances btwn cities using array of structs
- Next Thread: clear screen
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






