Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2005, 3:54 AM   #1
Hotepric
Newbie
 
Join Date: Nov 2004
Posts: 2
Rep Power: 0 Hotepric is on a distinguished road
Simple Program, kbhit() and beep

I'm supposed to make a program that beeps everytime any key is pressed. I thought this would be easy with an if or maybe even a do while and kbhit() but I can't get any output from this program and I can only get this code to compile in borland C++ builder 6 I tried it in dev ++ and it didn't recognize the beep command. Any insight would be appreciated thanks in advance.



#include "windows.h"
#include <stdio.h>
#include <iostream.h>
#include <conio.h>

int main(){
if(kbhit())
{
Beep( 800, 600 );
}
}
Hotepric is offline   Reply With Quote
Old Apr 8th, 2005, 7:25 AM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
conio.h is a non-standard header. It is a borland specific thing and should be avoided at all costs. I would suppose kbhit and/or Beep is defined there. So Dev-C++ is not happy.
With that said, the reason it's not working is because if(kbhit()) only checks for a keyboard hit when the application starts running. It doesn't get one so the application dies. You could do something like:
bool keyboardHit = false;
while(!keyboardHit) {
    if(kbhit()) {
        Beep(800, 600);
        keyboardHit = true;
    }
}
That would beep when you pressed a key and then exit.

As I mentioned, conio is a non-standard header. Look for a blocking way to read a key from the keyboard and then write a '\a' character to the console (alarm beep).
Cerulean is offline   Reply With Quote
Old Apr 8th, 2005, 10:31 AM   #3
duomax51
Newbie
 
duomax51's Avatar
 
Join Date: Apr 2005
Posts: 12
Rep Power: 0 duomax51 is on a distinguished road
Lightbulb

You might not have the right libary for Dev C++ I only use one kind of compiler and libary. So I am not sure if this is possible.
__________________
:D :eek: :cool: Joe Thompson
duomax51 is offline   Reply With Quote
Old Apr 8th, 2005, 2:16 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
i didn't think conio.h was a borland specific header?!? Doesn't matter much to me though, either way because I rarely use it.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 9th, 2005, 12:08 AM   #5
Hotepric
Newbie
 
Join Date: Nov 2004
Posts: 2
Rep Power: 0 Hotepric is on a distinguished road
Thanks for the response, and I'm working at learning more but the book I have is horrible. I need it for a class at school though because all the tests are based off of it. Its really outdated and for Borland Turbo C++, many of the instructions are including borland libraries. Anyways thanks again see you around.
Hotepric is offline   Reply With Quote
Old Apr 9th, 2005, 12:30 AM   #6
anandt4u
Newbie
 
Join Date: Feb 2005
Posts: 22
Rep Power: 0 anandt4u is on a distinguished road
no i dont think conio.h is borland specific. I have used it in Turbo C as well. But in gcc it does not work

anyway why not try "\a" ..even that gives a beep sound
anandt4u is offline   Reply With Quote
Old Apr 9th, 2005, 4:54 AM   #7
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
no i dont think conio.h is borland specific. I have used it in Turbo C as well.
Well gee.. who is Turbo C made by? Borland.. conio.h is a non-standard header - it's not defined by the ANSI C/C++ standards.
Actually the digital mars compiler includes it too but last time I checked that compiler is far too outdated to be considered useable.
Cerulean is offline   Reply With Quote
Old Apr 9th, 2005, 6:54 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Visual C++ includes it as well. But as people have said, it's non-standard.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC