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?
 
bkdcCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.