Solved

Repeat until keypressed in C++

Posted on 2001-09-06
8
4,244 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
Technology Partners: 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!

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

713 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