Overclockers Australia Forums

OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 27th October 2002, 7:06 PM   #1
xsive Thread Starter
Member
 
xsive's Avatar
 
Join Date: Jun 2001
Location: Sydney
Posts: 4,210
Default [C++] Continuous loop until keypress?

I'm trying to loop a function continuously until a key is pressed (i'm implementing a manual/automatic run thing).
getch() is the closest thing I could find to help me achieve this but if i put getch() inside a loop the loop wont work unless theres some kind of input coming in.

All i end up with is a semi-automatic mode where the program loops while a key is held down. Surely there must be a better way?
__________________
Quote:
Originally Posted by Lucifers Mentor View Post
Oooh! Oooh! Can I be evil and German? PPPPLLlleeeaassseeeeeeeeee??!!!???
xsive is offline   Reply With Quote

Join OCAU to remove this ad!
Old 27th October 2002, 9:50 PM   #2
Geo
Member
 
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
Default

Have you tried:

Code:
while ( cin.peek() == EOF ) {
         //do stuff
}
Might work...
__________________
"That's the way good software gets designed. So if you pull out a piece it won't run" - Steve Ballmer
Geo is offline   Reply With Quote
Old 28th October 2002, 12:38 AM   #3
Jive Honky
Member
 
Jive Honky's Avatar
 
Join Date: Jul 2001
Location: Melbourne, VIC
Posts: 1,395
Default

Hmm, maybe have the loop running in a separate thread and use a semaphore? Nah, screw that, too much work, just follow Geo's suggestion.
__________________

The unattainable is unknown at Zombo.com!
Over 1200 counts of layin' the smack down since July 2001.

Last edited by Jive Honky; 28th October 2002 at 12:38 AM.
Jive Honky is offline   Reply With Quote
Old 28th October 2002, 3:41 AM   #4
xsive Thread Starter
Member
 
xsive's Avatar
 
Join Date: Jun 2001
Location: Sydney
Posts: 4,210
Default

thanks guys
__________________
Quote:
Originally Posted by Lucifers Mentor View Post
Oooh! Oooh! Can I be evil and German? PPPPLLlleeeaassseeeeeeeeee??!!!???

Last edited by xsive; 28th October 2002 at 3:41 AM.
xsive is offline   Reply With Quote
Old 28th October 2002, 9:52 AM   #5
Unregisturd
Member
 
Unregisturd's Avatar
 
Join Date: Feb 2002
Location: Sydney
Posts: 1,775
Default

Quote:
Originally posted by Jive Honky
Hmm, maybe have the loop running in a separate thread and use a semaphore? Nah, screw that, too much work, just follow Geo's suggestion.
How would that work though? You'd still need something to check the keyboard and do an up on the semaphore, so you're still going to have something in tight loop...
Unregisturd is offline   Reply With Quote
Old 28th October 2002, 12:46 PM   #6
Jive Honky
Member
 
Jive Honky's Avatar
 
Join Date: Jul 2001
Location: Melbourne, VIC
Posts: 1,395
Default

Quote:
Originally posted by Unregisturd


How would that work though? You'd still need something to check the keyboard and do an up on the semaphore, so you're still going to have something in tight loop...
Well, I've only done multithreading in C, which is fairly easy, so I'm not sure how to do it in C++. My idea was to have a boolean called 'stop' which is set to false. So in the main thread thread you'd be continually looping until 'stop' is true.
Code:
while (!stop)
{
// do stuff
}
In a separate thread, it will just be waiting on input and will set 'stop' to true when it gets it.
Code:
cin >> stuff;
stop = true;
Or something.
__________________

The unattainable is unknown at Zombo.com!
Over 1200 counts of layin' the smack down since July 2001.
Jive Honky is offline   Reply With Quote
Old 28th October 2002, 3:19 PM   #7
FlameHead
Member
 
FlameHead's Avatar
 
Join Date: Jul 2001
Location: Sydney
Posts: 1,597
Default

Quote:
Originally posted by Jive Honky


Well, I've only done multithreading in C, which is fairly easy, so I'm not sure how to do it in C++. My idea was to have a boolean called 'stop' which is set to false. So in the main thread thread you'd be continually looping until 'stop' is true.
Code:
while (!stop)
{
// do stuff
}
In a separate thread, it will just be waiting on input and will set 'stop' to true when it gets it.
Code:
cin >> stuff;
stop = true;
Or something.

That's how I would have done it. But if you wanted to break the loop where it was, id set the loop up in another thread, and when you press they key, an event or just straight input of somesort in the main, it would then teell the loop thread to stop.

But if that doesn't sound right, just ignore me since I only know Java well.
FlameHead is offline   Reply With Quote
Old 28th October 2002, 3:53 PM   #8
volatile
Member
 
volatile's Avatar
 
Join Date: Aug 2001
Location: Chapel Hill, BNE
Posts: 32
Default

I don't know if this works in all C++ compilers, but I've used the _kbhit() function in Visual Studio C++ .NET a fair bit lately.

Code:
while (!_kbhit()) {
    //do stuff
}
char dummy = getch();
volatile is offline   Reply With Quote
Old 28th October 2002, 6:58 PM   #9
Unregisturd
Member
 
Unregisturd's Avatar
 
Join Date: Feb 2002
Location: Sydney
Posts: 1,775
Default

Quote:
Originally posted by Jive Honky
Well, I've only done multithreading in C, which is fairly easy, so I'm not sure how to do it in C++. My idea was to have a boolean called 'stop' which is set to false. So in the main thread thread you'd be continually looping until 'stop' is true.
Ok, after re-reading the initial post it would seem like threading is a good idea if the program is doing something else. But to check for a keypress, the other thread shouldn't need to loop - it will block waiting for input until it arrives.
Unregisturd is offline   Reply With Quote
Old 28th October 2002, 7:11 PM   #10
mertz
Member
 
Join Date: Oct 2002
Posts: 1
Default

char a;

while (1) {

if(cin<<a) { break; }

}

just use the return value from the cin object... but my syntax i wrong with the input object im sure.
mertz is offline   Reply With Quote
Old 28th October 2002, 8:10 PM   #11
Jive Honky
Member
 
Jive Honky's Avatar
 
Join Date: Jul 2001
Location: Melbourne, VIC
Posts: 1,395
Default

Quote:
Originally posted by mertz
char a;

while (1) {

if(cin<<a) { break; }

}

just use the return value from the cin object... but my syntax i wrong with the input object im sure.
Unless I'm mistaken, the code will stop at the if(), waiting for input, instead of continuing, which is not what he wants (unless you have this in a separate thread, in which case why is it in a loop?).

EDIT: Welcome to the forums mertz.

Quote:
Originally posted by Unregisturd
Ok, after re-reading the initial post it would seem like threading is a good idea if the program is doing something else. But to check for a keypress, the other thread shouldn't need to loop - it will block waiting for input until it arrives.
I'm not quite sure what you mean. The other thread isn't looping; it's just got a simple if() statement, and is stuck waiting for input, once it gets it the thread is finished and the loop in the main thread ends (because the condition is no longer true).
__________________

The unattainable is unknown at Zombo.com!
Over 1200 counts of layin' the smack down since July 2001.

Last edited by Jive Honky; 28th October 2002 at 8:22 PM.
Jive Honky is offline   Reply With Quote
Old 28th October 2002, 9:24 PM   #12
Unregisturd
Member
 
Unregisturd's Avatar
 
Join Date: Feb 2002
Location: Sydney
Posts: 1,775
Default

I was just going off Geo's example... i'm sure there's a better way to do this, but I couldn't be bothered looking up my C/C++ and operating systems books.
Unregisturd is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 3:29 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!