C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 28th, 2004, 12:44 AM
Ne0 Ne0 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 Ne0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Press Any Key To Continue Type Thing

In *nix, using C, how can i make it so it waits for the user to 'press any key' or at least press enter type thing untill it continues? Thanks.

Reply With Quote
  #2  
Old July 28th, 2004, 03:49 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 15
You can use getch/getchar (been a while since i used it so you'd have to look up specifics) to read a character in when they press enter but I'm pretty sure there is a function that will not wait for enter to be pressed.

Failing that it largely depends on what you are using to process your keyboard input. Libraries like SDL have functionality for monitoring keyboard state which would do nicely for this.

Hope this helps,

-KM-

Reply With Quote
  #3  
Old October 19th, 2005, 07:53 PM
tdb2q tdb2q is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 1 tdb2q User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 16 sec
Reputation Power: 0
Arrow windows solution

Quote:
Originally Posted by Ne0
In *nix, using C, how can i make it so it waits for the user to 'press any key' or at least press enter type thing untill it continues? Thanks.


if you are using windows you can include stdlib.h and use the following command

system("pause");


Reply With Quote
  #4  
Old October 19th, 2005, 08:07 PM
jaf1211 jaf1211 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 21 jaf1211 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by tdb2q
if you are using windows you can include stdlib.h and use the following command

system("pause");



pause needs to be in all caps, correct?

Reply With Quote
  #5  
Old October 19th, 2005, 11:53 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 13
Send a message via Yahoo to BloodlustShaman
Yes but that I use C++ and if it is the same yes also remember to include stdlib.h

Reply With Quote
  #6  
Old October 20th, 2005, 12:53 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 13
Send a message via AIM to Geo.Garnett
or if you want to be tricky and want to do a time delay until it exits, you could try this.
Code:
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
   
   float del_sec = 5;//number of delay seconds
   clock_t delay = del_sec * CLOCKS_PER_SEC; //converts del_sec to clock tics
   cout<<"Starting Count Down\n";
   clock_t start = clock();
   while (clock() - start < delay)//waits for 5 seconds
      ;// note the semicolon
   cout<<"done\n";
   system("PAUSE");
   return 0;
}

p0p that in your compiler and smoke it!

But, I use a C++ compiler so like the other guys say you might have to include other libraries depending on how smart your compiler is =).. But note the use of the system pause in there which is the easiest solution to your question.
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Last edited by Geo.Garnett : October 20th, 2005 at 12:58 AM.

Reply With Quote
  #7  
Old October 20th, 2005, 11:28 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 13
Send a message via Yahoo to BloodlustShaman
Quote:
Originally Posted by Geo.Garnett
p0p that in your compiler and smoke it!


I know I did and I love the feature and not that you were trying to help me but thanks for that cool feature.

I love it!!!!

Where did you learn that?, Of course do not tell me if you do not want to just say "I do not want to tell you that"

Reply With Quote
  #8  
Old October 21st, 2005, 12:07 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 13
Send a message via AIM to Geo.Garnett
Reading through one of my C/C++ books I came across that and just by chance made a note of it.

Reply With Quote
  #9  
Old October 21st, 2005, 12:13 AM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 13
Send a message via Yahoo to BloodlustShaman
Kool well thanks for that code it is not usefull for me but it will be in the future

Reply With Quote
  #10  
Old October 21st, 2005, 06:34 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 120 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 13
Strange discussion here ... the question was for *nix in C :-s

The answer is:
getch() <conio.h>

It just waits for a key. This does not have to be <enter>.

getchar() does almost the same, only the user has to press <enter> before processing starts. Meaning: when the user types "hello<enter>" the next couple of getchar() calls will immediatly return with the next character, up to the <enter>. So this function is not the solution. Use getch(), it should be there in conio.h

Reply With Quote
  #11  
Old October 21st, 2005, 10:56 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 13
Send a message via AIM to Geo.Garnett
Well, being that the thread is three months old we kinda got off subject for sure. We weren't really trying to solve NeO's problem any more, were all just having a little discussion on this old thread.

Reply With Quote
  #12  
Old October 24th, 2005, 06:33 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 120 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 13
Oops ... didn't look at his post date :-$ sorry
I sure hope he has enabled the 'send me an e-mail on reply' option :-D

To contribute to the discussion.
I always use the GetTickCount() function ... but this might be a Windows only function. Don't know for sure. It returns a DWORD with the number of milliseconds after computer boot.

Reply With Quote
  #13  
Old March 10th, 2007, 06:46 AM
anruari anruari is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 1 anruari User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m
Reputation Power: 0
a robust solution

I know this is an old thread, and similar ones are on a lot of other forums, but I've been looking for a portable solution to this (Press any key...) all morning.

I couldnít get getchar to do what I wanted. I had been using

char trash;
cin >> trash;
return 0;

trouble is I didn't like the press a key then enter result. It just dosn't seem elegant.


I am learning C++, and intend to write portable applications which I can move between both a Linux, BSD and Windows machines.
Initially Iím working through the console programs, which if opened from within a graphical environment close before you have a chance to read the output.

I want my solution to this to be portable, so no system(PAUSE) calls, and no <conio> as that is not standard and not portable, though as I learn more I'll see if there are suitable ports of this library for most compilers.
(ps I know this works in DOS, but does it work in Linux??? (Iím away from that computer right now so cant try it...

I seem to have found a slightly more elegant solution, based on

"Press Enter / Return to continue..."


#include <iostream>
using namespace std; // or just <iostream.h> on older compilers

void pressEnter()
{
cin.clear() // just to ensure there's no junk in the stream which
cout << ""Press Enter / Return to continue...\n";
cin.ignore(1,0);
}

Last edited by anruari : March 10th, 2007 at 06:55 AM. Reason: fix typo's, add cin.clear()

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Press Any Key To Continue Type Thing


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap