Using getch and Beep, with some other stuff, im trying to make a program that plays music based on your keypresses. however, the console is spammed with tons of keys, even when i dont press anything. here is my code:


Code:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

int FGetKey();
int Input;
int GetKey;
int Atmp;
int Btmp;
int Ctmp;

int main()
{
    while (GetKey != 56)
    {
      FGetKey();
    printf("Key: %d %x %c", GetKey, GetKey, GetKey);
    Atmp = GetKey * 100;
    Beep(Atmp, 200); 
    }
    return 0;
}

int FGetKey()
{
    GetKey = 0;
    while (GetKey != 0)
    {
        GetKey = getch();
    }
}

i cant see why the while loop isnt holding getch(); even if getch should wait for user input. any help?
I think your problem was that you were using getch(which is deprecated, by the way, you should use _getch instead) inside of 2 loops, when it only needs 1. Here's the code that I got to compile under MS Visual C++ 2010 (x86):

Code:

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <Windows.h>
#define WIN32_LEAN_AND_MEAN
 
int GetKey;
int Atmp; 

int main() 

    while (GetKey != '~')
    {
      GetKey = _getch();
      if (GetKey != 0)
      printf("Key: %d %x %c\n", GetKey, GetKey, GetKey);
      Atmp = GetKey * 100;
      Beep(Atmp, 200);   
    }
    return 0;
}

I also changed the quitting key to tilde, because _getch returns a char, not a keycode.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement