用 C++ 发出声音(哔)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4060601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make sounds (beep) with c++
提问by user319854
How to make the hardware beep sound with c++?
如何用c ++使硬件发出哔哔声?
Thanks
谢谢
回答by RobertPitt
If you're using Windows OS then there is a function called Beep()
如果您使用的是 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;
}
Source: http://www.daniweb.com/forums/thread15252.html
来源:http: //www.daniweb.com/forums/thread15252.html
For Linux based OS there is:
对于基于 Linux 的操作系统,有:
echo -e "echo "^G"
7" >/dev/tty10
And if you do not wish to use Beep()in windows you can do:
如果您不想Beep()在 Windows 中使用,您可以执行以下操作:
char d=(char)(7);
printf("%c\n",d);
回答by asveikau
There are a few OS-specific routines for beeping.
有一些特定于操作系统的蜂鸣例程。
On a Unix-like OS, try the (n)curses beep() function. This is likely to be more portable than writing
'\a'as others have suggested, although for most terminal emulators that will probably work.In some *BSDs there is a PC speaker device. Reading the driver source, the
SPKRTONEioctl seems to correspond to the raw hardware interface, but there also seems to be a high-level language built aroundwrite()-ing strings to the driver, described in the manpage.It looks like Linux has a similar driver (see this articlefor example; there is also some example code on this pageif you scroll down a bit.).
In Windows there is a function called Beep().
在类 Unix 操作系统上,尝试(n)curses beep() 函数。这可能比
'\a'其他人建议的编写更便携,尽管对于大多数可能会工作的终端仿真器。在某些 *BSD 中有一个PC 扬声器设备。读取驱动程序源代码,
SPKRTONEioctl 似乎对应于原始硬件接口,但似乎也有一种高级语言围绕write()驱动程序的 -ing 字符串构建,如联机帮助页中所述。在 Windows 中有一个名为Beep()的函数。
回答by Namit Sinha
alternatively in c or c++ after including stdio.h
在包含 stdio.h 之后,或者在 c 或 c++ 中
std::cout << '';
(char)7 is called the bell character.
(char)7 被称为铃字符。
回答by Benjamin Lindley
#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
}
回答by Krys
cout << '\a';
回答by Ani
Here's one way:
这是一种方法:
#ifdef WINDOWS
#include <Windows.h>
void beep() {
Beep(440, 1000);
}
#elif LINUX
#include <stdio.h>
void beep() {
system("echo -e "cout << "\a";
7" >/dev/tty10");
}
#else
#include <stdio.h>
void beep() {
cout << "\a" << flush;
}
#endif
From C++ Character Constants:
从C++ 字符常量:
Alert: \a
警报:\a
回答by Martin Beckett
Easiest way is probbaly just to print a ^G ascii bell
最简单的方法可能只是打印一个 ^G ascii bell
回答by ndrewxie
You could use conditional compilation:
您可以使用条件编译:
##代码##回答by dams
In Xcode, After compiling, you have to run the executable by hand to hear the beep.
在 Xcode 中,编译后,您必须手动运行可执行文件才能听到哔哔声。

