473,569 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Press any key

I'm trying to write a simple press any key routine, but am having some
problems. Here is the routine:

void PressAnyKey ()
{
cout << "Press Any Key";
do{}while (!kbhit());
getch();
cout << endl;
}

When it runs, the next part of the code is to read using a similar kbhit(),
getch() routine, but it reads in whatever was pressed during the press a
key. If I add an extra getch(), then it just sits there until you press it
twice.

Any ideas?

John
Jul 19 '05 #1
3 15830

"John Howard" <jd********@yah oo.com> wrote in message
news:IP******** *************@b gtnsc05-news.ops.worldn et.att.net...
I'm trying to write a simple press any key routine, but am having some
problems. Here is the routine:

void PressAnyKey ()
{
cout << "Press Any Key";
do{}while (!kbhit());
getch();
cout << endl;
}

When it runs, the next part of the code is to read using a similar kbhit(), getch() routine, but it reads in whatever was pressed during the press a
key. If I add an extra getch(), then it just sits there until you press it twice.

Any ideas?

John


I'm not sure if I'm getting your question right, but it seems like the
program is running so fast that the next "press any key" loop is called
while the user is still holding down the button? You could just add in a
delay, something like

for (unsigned long x = 100000; x!=0; x--);

I doubt that's long enough, but just keep increasing the number. Of course,
this is a bad way to do it, as CPU speeds change, and thus that loop will
not be the same length of delay on every computer. I, unfortunately, cannot
see another standard way of doing this.

Most compilers, however, have an extention, the system() function, which is
nonstandard, but can do exactly what you want. It literally tells the OS to
do something, so by its very nature it is very OS dependent. Do this only if
you are going to use this on a single OS. For example, in Win32 (and perhaps
even Win16, but nobody cares about that anymore) you could do:

system("pause") ;

In Win32, system() is the equivalent of going start->run and typing in
whatever's inside those quotes. Pause is a recognized DOS statement which
displays "Press any key to continue..." and waits for a keypress. That
should do better if you're willing to do something nonstandard.

Another non-standard option but commonly found extention is also available
for your compiler. Check for something like wait() or something like that.
This function will usually allow you to pause for an integral number of
seconds. This is, obviously, a better way of doing it than the delay loop,
as it will remain the same speed on every computer.

Hope that helped
--
MiniDisc_2k2
Jul 19 '05 #2
John Howard writes:
I'm trying to write a simple press any key routine, but am having some
problems. Here is the routine:

void PressAnyKey ()
{
cout << "Press Any Key";
do{}while (!kbhit());
getch();
cout << endl;
}

When it runs, the next part of the code is to read using a similar kbhit(), getch() routine, but it reads in whatever was pressed during the press a
key. If I add an extra getch(), then it just sits there until you press it twice.


I guess I don't know what "similar" means. I would expect this code,
executed twice, to work. There are problems mixing getch() with the
ordinary I/O libraries. Are you sure you are not tangled up in one of those
problems? I think you should repost your question, without using the word
similar, in a group where discussion of getch() is not forbidden.
Jul 19 '05 #3
"John Howard" <jd********@yah oo.com> wrote in message news:<IP******* **************@ bgtnsc05-news.ops.worldn et.att.net>...
I'm trying to write a simple press any key routine, but am having some
problems. Here is the routine:

void PressAnyKey ()
{
cout << "Press Any Key";
do{}while (!kbhit());
getch();
cout << endl;
}

When it runs, the next part of the code is to read using a similar kbhit(),
getch() routine, but it reads in whatever was pressed during the press a
key. If I add an extra getch(), then it just sits there until you press it
twice.

Any ideas?

John


Why don't you get rid of the do-while loop and just have the getch there?
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6207
by: asdasd | last post by:
Hello there, I'm hoping someone might be able to help me out. Its been awhile since I've programmed in visual basic (and roughly at that)... I want to create a program that will allow me to choose a keyboard key (perferably an F-KEY) from a combobox and be able to set a delay (in secs) from a textbox. Once that has been chosen I need to...
1
2950
by: Brian Basquille | last post by:
Can i carry out a command during execution with the press of a key? Am working on a Countdown Timer and everytime i press p on the keyboard, i want to add a minute to the timer and every time i press m, i want to deduct a minute from it. Any suggestion would be much appreciated. Thanks in advance, Brian
6
2440
by: Johan Svedberg | last post by:
Hi! I read somewhere that it is impossible to write a program in ANSI-C which only reads one char from the keyboard without having to press Enter (i.e. -questions). Is this true? -- Johan Svedberg, johan@svedberg.pp.se, http://johan.svedberg.pp.se/
5
20712
by: junk mail | last post by:
My friend is trying to code a small c program where he wants to force the user to press enter and only enter to continue. Currently he is using getchar() with a loop but you can type any number of characters, which are echoed to screen before you have to press enter. For example:- do { /* Begin loop */
0
1956
by: neeraj | last post by:
Hi Everybody I have one problem in my asp.net web application. The problem is that I check the date validation in code behind; if user not gives the valid data and press save button then I fire my own exception by JavaScript alert message Through register startup script Like RegisterStartupScript("Startup", "<script language=javascript>
9
64342
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use 'getchar', or 'gets', or some get function? or 'cin', or what's an easy way? What I want is the program to pause before exiting so the user can...
1
6983
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the page has multiple button such login page with a search function somewhere around, then it's not respond properly. I have attached a brief example of...
1
2343
by: =?Utf-8?B?QXNhZg==?= | last post by:
Hello, I have a Default.aspx page with several controls in a Master Page. When pressing on a Button control Page_Load event is fired on the Default.aspx page and after that the Page_Load event on the Master Page is fired but it does not go to the Button event in the first button press but only on the second press on the button. I can't...
3
3468
by: win | last post by:
How to get the keycode press in a web form Vincent
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.