How to hide the "Press any key to continue?"

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 4
Reputation: Leopold is an unknown quantity at this point 
Solved Threads: 0
Leopold Leopold is offline Offline
Newbie Poster

How to hide the "Press any key to continue?"

 
0
  #1
Jan 17th, 2009
I'm VERY inexperienced in programming, I'm just learning the very basics. Anyways, I just figured out how to add a "pause" line to keep the program from closing as soon as I run it, but now there's a nasty "Press any key to continue" after my text. Is there a way to hide this? Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
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: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: How to hide the "Press any key to continue?"

 
1
  #2
Jan 17th, 2009
I assume you're using system ( "PAUSE" ) . My first inclination is to say that this solution is a bad idea anyway, and if it doesn't do exactly what you want, that's all the better for convincing you not to use it.

The usual recommendation for pausing a program from an IDE that terminates the hosting console when the program ends is to ask for input. For example, the following code won't terminate until you press the Enter key:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std::cout<<"Hello, world!\n";
  6. std::cin.get();
  7. }
You'll eventually have problems with existing data in the stream causing this not to work, but we have a sticky on this forum that teaches you how to deal with that.
Last edited by Narue; Jan 17th, 2009 at 9:11 pm.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: Leopold is an unknown quantity at this point 
Solved Threads: 0
Leopold Leopold is offline Offline
Newbie Poster

Re: How to hide the "Press any key to continue?"

 
0
  #3
Jan 17th, 2009
Ahh, thanks. Solved my problem completely.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: How to hide the "Press any key to continue?"

 
0
  #4
Jan 17th, 2009
You can also do:

  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6. std::cout << "HELLO WORLD!!!!!!!111";
  7. getch();
  8. }
Last edited by TheBeast32; Jan 17th, 2009 at 9:41 pm.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
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: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: How to hide the "Press any key to continue?"

 
1
  #5
Jan 17th, 2009
>You can also do:
You could, if you wanted to be wrong most of the time. conio.h and getch are non-portable, so the majority of compilers won't support them.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: How to hide the "Press any key to continue?"

 
0
  #6
Jan 17th, 2009
Ok
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,491
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: How to hide the "Press any key to continue?"

 
0
  #7
Jan 18th, 2009
For a windows solution, you could try this which will allow the user to press any key to continue (not just enter):
  1. void Pause(char *message = "Press any key to continue . . . ") {
  2. std::cout << message;
  3. HANDLE hStdin;
  4. DWORD cNumRead;
  5. INPUT_RECORD irInBuf[1];
  6.  
  7. if ( HANDLE(hStdin = GetStdHandle( STD_INPUT_HANDLE )) == INVALID_HANDLE_VALUE )
  8. return;
  9.  
  10. while ( true ) {
  11. if (! ReadConsoleInput( hStdin, irInBuf, 1, &cNumRead) )
  12. return;
  13.  
  14. for (DWORD i = 0; i < cNumRead; ++i)
  15. if ( irInBuf[i].EventType == KEY_EVENT && irInBuf[i].Event.KeyEvent.bKeyDown ) {
  16. std::cout << '\n';
  17. return;
  18. }
  19. }
  20. }
To remove the message, simply call it like this.
  1. Pause("");
Last edited by William Hemsworth; Jan 18th, 2009 at 3:28 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1207 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC