C++ Beep不工作

Dav*_*llo 0 c++ beep piano

我是一名新手程序员,我正在尝试用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);
    }

}
Run Code Online (Sandbox Code Playgroud)

我真的找不到任何错误,所以任何帮助都会受到赞赏.我正在编译Visual Studio 2013.

mar*_*rsh 5

虽然您的计算机可能没有内置扬声器.你的代码也陷入无限循环.

while (ciclo);
Run Code Online (Sandbox Code Playgroud)

我建议你循环,只要密钥不是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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)