somethi

Hello guys!I wonder if i could do something like if you press a key then make a sound with Beep() function included in <windows.h>.But i want that without need to press enter in console after entering that variable.

So far i have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#‎include‬ <iostream>
#include <windows.h>
using namespace std;
int main( void )
{
   int nr;
   while(cin>>nr)
   {
     if(nr==1)
      Beep(100,100);
     else if(nr==2)
      Beep(200,200);
     else if(nr==3)
      Beep(300,300);
     else if(nr==4)
      Beep(400,400);
     else if(nr==5)
      Beep(500,500);
     else if(nr==6)
      Beep(600,600);
     else if(nr==7)
      Beep(700,700);
     else if(nr==8)
      Beep(800,800);
     else if(nr==9)
      Beep(900,900);
     else if(nr==0)
      return 0;
   }
}
I don't think there's a standard way to do that without pressing enter.

On Windows, you may find the non-standard header <conio.h> and the function getch() or _getch().

#include <conio.h>

1
2
3
4
5
6
7
8
   char ch;
   
   while (ch = getch())
   {
       int nr = ch - '0';

       // etc.
   }


Here I converted from char to int merely for compatibility with the existing code. You could just test the value of ch directly instead - including alphabetic as well as number keys.


Topic archived. No new replies allowed.