Repeat until keypressed in C++

how to translate the following TP7 code into C++ :

repeat
...
until keypressed;

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


Thanks.
Knight_GAsked:
Who is Participating?
 
bkdcConnect With a Mentor Commented:
kotan is right.
However for(...) might confuse you (a little).
I think a more "natural" approach is:

do
{
  //do your stuff
  ....

}
while (!kbhit());
0
 
kotanCommented:
use kbhit().

for (...) {

     if (kbhit()) {
          break;
     }
}
0
 
kotanCommented:
remember to include conio.h
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ZoppoCommented:
or
while (!kbhit())
{
  // do your staff
 ...
}
0
 
bkdcCommented:
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
 
ZoppoCommented:
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
 
bkdcCommented:
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
 
rupnow1033Commented:
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
All Courses

From novice to tech pro — start learning today.