![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Jun 2001
Location: Sydney
Posts: 4,210
|
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? |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
|
Have you tried:
Code:
while ( cin.peek() == EOF ) {
//do stuff
}
__________________
"That's the way good software gets designed. So if you pull out a piece it won't run" - Steve Ballmer |
|
|
|
|
|
#3 |
|
Member
Join Date: Jul 2001
Location: Melbourne, VIC
Posts: 1,395
|
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. |
|
|
|
|
|
#4 |
|
Member
Join Date: Jun 2001
Location: Sydney
Posts: 4,210
|
thanks guys
__________________
Last edited by xsive; 28th October 2002 at 3:41 AM. |
|
|
|
|
|
#5 | |
|
Member
Join Date: Feb 2002
Location: Sydney
Posts: 1,775
|
Quote:
|
|
|
|
|
|
|
#6 | |
|
Member
Join Date: Jul 2001
Location: Melbourne, VIC
Posts: 1,395
|
Quote:
Code:
while (!stop)
{
// do stuff
}
Code:
cin >> stuff; stop = true;
__________________
The unattainable is unknown at Zombo.com! Over 1200 counts of layin' the smack down since July 2001.
|
|
|
|
|
|
|
#7 | |
|
Member
Join Date: Jul 2001
Location: Sydney
Posts: 1,597
|
Quote:
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. |
|
|
|
|
|
|
#8 |
|
Member
Join Date: Aug 2001
Location: Chapel Hill, BNE
Posts: 32
|
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();
|
|
|
|
|
|
#9 | |
|
Member
Join Date: Feb 2002
Location: Sydney
Posts: 1,775
|
Quote:
|
|
|
|
|
|
|
#10 |
|
Member
Join Date: Oct 2002
Posts: 1
|
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. |
|
|
|
|
|
#11 | ||
|
Member
Join Date: Jul 2001
Location: Melbourne, VIC
Posts: 1,395
|
Quote:
EDIT: Welcome to the forums mertz. ![]() Quote:
__________________
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. |
||
|
|
|
|
|
#12 |
|
Member
Join Date: Feb 2002
Location: Sydney
Posts: 1,775
|
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.
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|