用c ++发出声音(嘟嘟声)
如何使硬件蜂鸣声与C + +?
谢谢
cout << '\a'; 资源
🙂
 如果你使用的是Windows操作系统,那么有一个名为Beep()的函数 
 #include <iostream> #include <windows.h> // WinApi header using namespace std; int main() { Beep(523,500); // 523 hertz (C5) for 500 milliseconds cin.get(); // wait return 0; } 
来源: http : //www.daniweb.com/forums/thread15252.html
对于基于Linux的操作系统,有:
 echo -e "\007" >/dev/tty10 
 如果你不想在窗口中使用Beep() ,你可以这样做: 
 echo "^G" 
资料来源: http : //www.frank-buss.de/beep/index.html
有几个操作系统特定的例程用于发出哔声。
- 
在类Unix操作系统上,尝试(n)curses beep()函数 。 尽pipe对于大多数terminal仿真器来说,这可能比写 '\a'更容易。
- 
在一些* BSD中有一个PC扬声器设备 。 读取驱动程序源代码后, SPKRTONEioctl似乎与原始硬件接口相对应,但似乎还有一个围绕驱动程序的write()string构build的高级语言,如联机帮助页中所述。
- 
它看起来像Linux有一个类似的驱动程序(例如,看这篇文章 ,如果你向下滚动一下, 这个页面上也有一些示例代码)。 
- 
在Windows中有一个名为Beep()的函数。 
在包含stdio.h之后,可以在c或c ++中select
 char d=(char)(7); printf("%c\n",d); 
(char)7被称为钟形字符。
 std::cout << '\7'; 
这里有一个方法:
 cout << '\a'; 
从C ++字符常量 :
警报:\ a
最简单的方法就是打印一个^ G ascii铃
 #include<iostream> #include<conio.h> #include<windows.h> using namespace std; int main() { Beep(1568, 200); Beep(1568, 200); Beep(1568, 200); Beep(1245, 1000); Beep(1397, 200); Beep(1397, 200); Beep(1397, 200); Beep(1175, 1000); cout<<endl; _getch() return 0 } 
你可以使用条件编译:
 #ifdef WINDOWS #include <Windows.h> void beep() { Beep(440, 1000); } #elif LINUX #include <stdio.h> void beep() { system("echo -e "\007" >/dev/tty10"); } #else #include <stdio.h> void beep() { cout << "\a" << flush; } #endif 
ASCII铃声字符可能是你正在寻找。 这个表中的数字是7。
 cout << "\a"; 
在Xcode中,编译后,你必须手动运行可执行文件才能听到嘟嘟声。