Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about breaking loops in my program...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm writing a program that generates pythagorean triples for a summer project in math. I have a problem however... the main loop repeats indefinitly so that the user can continue to generate pythagorean triples, but I want the user to have the ability to break the loop and exit the program at any time by pressing a certain key, such as the "~" for example. How can I do this so that the main loop can still repeat but the user can exit at any time? What can I do so that the rest of the commands can still be carried out, but the program will be checking for the user to hit the ~ key the entire time?? Is this even possible? Thanks.
 
I'm programming using visual C++ as my compiler, and its in a DOS enviroment
 
DOS - not Windows? Sorry to keep on but it makes a difference - which version of Visual C++ as I was under the impression it ceased to support pure DOS in version 1.5!

Cheers
 
If your version of the vc++ compiler supports it you could look at using the _kbhit() function within your processing loop the check to see if a key has been pressed, if it returns 0 then no key has been pressed so you continue generating your triples, if it returns a non-zero value then you use _getch() to retrieve the key value from the keyboard buffer and test it to see if it matches the key you want, if it does not then you can continue processing, if it does match then you can do your clean up and exit code.

Hope this helps - Cheers - Gavin

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top