我是一个新手程序员,我试图用C++制作一架钢琴,使用Beep功能。问题是,当我按下键时,我听不到声音。这是我的密码:
#include <cstdlib>
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main(){
bool ciclo = true;
char tecla = _getch();
while (ciclo);
if (tecla == 'd'){
Beep(261, 100);
}
if (tecla == 'f'){
Beep(293, 100);
}
if (tecla == 'g'){
Beep(329, 100);
}
if (tecla == 'h'){
Beep(349, 100);
}
if (tecla == 'j'){
Beep(392, 100);
}
if (tecla == 'k'){
Beep(440, 100);
}
if (tecla == 'l'){
Beep(493, 100);
}
if (tecla == 'k'){
Beep(523, 100);
}
if (tecla == 'q'){
ciclo = false;
};
if (tecla == 'r'){
Beep(277, 100);
}
if (tecla == 't'){
Beep(312, 100);
}
if (tecla == 'u'){
Beep(370, 100);
}
if (tecla == 'i'){
Beep(415, 100);
}
if (tecla == 'o'){
Beep(466, 100);
}
}我真的找不到什么不对劲的地方,所以我会感谢你的帮助。我正在Visual 2013上编译。
发布于 2015-08-16 03:33:25
也许你的电脑没有电脑扬声器。看看这些文档:https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx
发布于 2015-08-16 03:45:01
虽然您的计算机可能没有内置的扬声器,但这是事实。您的代码也被困在一个无限循环中。
while (ciclo);我建议您循环,只要键不是q,这样用户就可以退出。
下面是代码工作的一个示例。
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main(){
while (char tecla = _getch() != 'q')
{
if (tecla == 'd'){
Beep(261, 100);
}
if (tecla == 'f'){
Beep(293, 100);
}
if (tecla == 'g'){
Beep(329, 100);
}
if (tecla == 'h'){
Beep(349, 100);
}
if (tecla == 'j'){
Beep(392, 100);
}
if (tecla == 'k'){
Beep(440, 100);
}
if (tecla == 'l'){
Beep(493, 100);
}
if (tecla == 'k'){
Beep(523, 100);
}
if (tecla == 'r'){
Beep(277, 100);
}
if (tecla == 't'){
Beep(312, 100);
}
if (tecla == 'u'){
Beep(370, 100);
}
if (tecla == 'i'){
Beep(415, 100);
}
if (tecla == 'o'){
Beep(466, 100);
}
}
}发布于 2015-08-16 03:43:18
大多数现代电脑都没有内置的扬声器。
通过您的include语句,我可以看到您正在使用Windows。Windows通常使用系统的默认消息声音(对话框出现时听到的“叮”)。确保你的扬声器打开,或者尝试另一种不使用Beep的解决方案。
https://stackoverflow.com/questions/32031540
复制相似问题