首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ Beep不工作

C++ Beep不工作
EN

Stack Overflow用户
提问于 2015-08-16 03:31:36
回答 3查看 3.7K关注 0票数 0

我是一个新手程序员,我试图用C++制作一架钢琴,使用Beep功能。问题是,当我按下键时,我听不到声音。这是我的密码:

代码语言:javascript
复制
#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上编译。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-16 03:33:25

也许你的电脑没有电脑扬声器。看看这些文档:https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx

票数 2
EN

Stack Overflow用户

发布于 2015-08-16 03:45:01

虽然您的计算机可能没有内置的扬声器,但这是事实。您的代码也被困在一个无限循环中。

代码语言:javascript
复制
while (ciclo);

我建议您循环,只要键不是q,这样用户就可以退出。

下面是代码工作的一个示例。

代码语言:javascript
复制
#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);
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2015-08-16 03:43:18

大多数现代电脑都没有内置的扬声器。

通过您的include语句,我可以看到您正在使用Windows。Windows通常使用系统的默认消息声音(对话框出现时听到的“叮”)。确保你的扬声器打开,或者尝试另一种不使用Beep的解决方案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32031540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档