我是一名新手程序员,我正在尝试用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 Studio 2013上进行编译。
答案 0 :(得分:5)
虽然您的计算机可能没有内置扬声器。你的代码也陷入无限循环。
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);
}
}
}
答案 1 :(得分:2)
也许你的电脑没有PC扬声器。查看文档:{{3}}
答案 2 :(得分:2)
大多数现代个人电脑都没有配备内置扬声器。
通过include语句,我可以看到您正在使用Windows。 Windows通常使用系统的默认消息声音(出现对话框时听到的“ding”)。确保打开扬声器,或者尝试其他不使用Beep的解决方案。