Solved

Repeat until keypressed in C++

Posted on 2001-09-06
8
4,209 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
 
LVL 30

Expert Comment

by:Zoppo
ID: 6460140
or
while (!kbhit())
{
  // do your staff
 ...
}
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 30

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now