Replacing system("pause"); with a function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 41
Reputation: #include<DAN.h> is an unknown quantity at this point 
Solved Threads: 0
#include<DAN.h> #include<DAN.h> is offline Offline
Light Poster

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

 
0
  #11
Jan 14th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #12
Jan 14th, 2008
>Excuse me for sounding like a total noob here for a
>second, but what is a "console namespace".
You can create your own namespaces:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #13
Jan 14th, 2008
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):
  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:
  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:
  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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: zoner7 is an unknown quantity at this point 
Solved Threads: 4
zoner7 zoner7 is offline Offline
Junior Poster

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

 
0
  #14
Jan 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #15
Jan 15th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC