Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4331
  • Last Modified:

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.
0
Knight_G
Asked:
Knight_G
  • 3
  • 2
  • 2
  • +1
1 Solution
 
kotanCommented:
use kbhit().

for (...) {

     if (kbhit()) {
          break;
     }
}
0
 
kotanCommented:
remember to include conio.h
0
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now