Solved

Repeat until keypressed in C++

Posted on 2001-09-06
8
4,229 Views
Last Modified: 2008-01-16
how to translate the following TP7 code into C++ :

repeat
...
until keypressed;

this portion of code will remain looping until a key striked.


Thanks.
0
Comment
Question by:Knight_G
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6460091
use kbhit().

for (...) {

     if (kbhit()) {
          break;
     }
}
0
 
LVL 6

Expert Comment

by:kotan
ID: 6460094
remember to include conio.h
0
 
LVL 2

Accepted Solution

by:
bkdc earned 50 total points
ID: 6460118
kotan is right.
However for(...) might confuse you (a little).
I think a more "natural" approach is:

do
{
  //do your stuff
  ....

}
while (!kbhit());
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 31

Expert Comment

by:Zoppo
ID: 6460140
or
while (!kbhit())
{
  // do your staff
 ...
}
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6460167
Yep Zoppo, you're right but there's a small detail you missed. do....repeat ensures that the code between do<> repeat/while gets to be executed at least once.
while(...) { } might exit without actually doing anything
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6460435
well, I think this depends on what one wants ... maybe I don't want the code to be executed if I pressed the key before this code is reached?
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6460453
yes, that's true but in Pascal there's a big difference between repeat...until  and while ... do ... . I tried to respect that diff. because somebody had a reason for writing
>>repeat
>>...
>>until keypressed
0
 

Expert Comment

by:rupnow1033
ID: 6462817
you also could try to use the getch() command which will pause untill any key on the keyboard is pressed.
it requires <conio.h>

...
{
  cout << "please press any key";
  getch();
}
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question