943,902 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6069
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 14th, 2008
0

Re: Replacing system("pause"); with a function

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?
Reputation Points: 6
Solved Threads: 0
Light Poster
#include<DAN.h> is offline Offline
41 posts
since Dec 2007
Jan 14th, 2008
0

Re: Replacing system("pause"); with a function

>Excuse me for sounding like a total noob here for a
>second, but what is a "console namespace".
You can create your own namespaces:
C++ Syntax (Toggle Plain Text)
  1. namespace console {
  2. void pause()
  3. {
  4. //...
  5. }
  6. }
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 14th, 2008
0

Re: Replacing system("pause"); with a function

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):
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. namespace console {
  5. void pause() {
  6. std::cout << "Press ENTER to continue...";
  7. std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  8. }
  9. }

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)
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. namespace console {
  5. void pause() {
  6. DWORD mode;
  7. HANDLE hin;
  8. INPUT_RECORD inrec;
  9. DWORD count;
  10.  
  11. // Instruct the user
  12. std::cout << "Press the 'any' key...";
  13.  
  14. // Set the console mode to no-echo, raw input,
  15. // and no window or mouse events.
  16. hin = GetStdHandle( STD_INPUT_HANDLE );
  17. GetConsoleMode( hin, &mode );
  18. SetConsoleMode( hin, 0 );
  19.  
  20. // Wait for and get a single key press
  21. do ReadConsoleInput( hin, &inrec, 1, &count );
  22. while (inrec.EventType != KEY_EVENT);
  23.  
  24. // Restore the original console mode
  25. SetConsoleMode( hin, mode );
  26. }
  27. }

You can use it the usual way:
C++ Syntax (Toggle Plain Text)
  1. int main() {
  2.  
  3. std::cout << "Hello world!\n" << std::endl;
  4.  
  5. console::pause();
  6.  
  7. std::cout << "\n\nThank you!" << std::endl;
  8.  
  9. return EXIT_SUCCESS;
  10. }

Enjoy!
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jan 14th, 2008
0

Re: Replacing system("pause"); with a function

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.
Reputation Points: 34
Solved Threads: 4
Junior Poster
zoner7 is offline Offline
114 posts
since Dec 2007
Jan 15th, 2008
0

Re: Replacing system("pause"); with a function

Quote ...
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 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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Programming student find distances btwn cities using array of structs
Next Thread in C++ Forum Timeline: clear screen





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC