J. Doe
J. Doe

Reputation: 184

Stop computer beeping when printing the number 7

I'm printing a bunch of ascii chars to the console as a representation of binary numbers however whenever it prints out the number 7 to the console then windows makes a beeping noise. Looking online I can see some people talking about ascii 7 making a noise but I cant seem to find where to disable it in the code.

for (size_t i = 0; i < 1160; i++)
{
    std::cout << "\n" << (char)decimalarray[i];
}

this occurs when the value in the UIN8 array is 7 and I try printing the value as a char. printing (int)decimalarray[1157] outputs the number 7 printing (char)decimalarray[1157] outputs nothing but makes beeping noise

edit: it would probably be ideal if there was a way to only write printable characters. not easy to hardcode in the values as the program uses every ascii character there is in normal execution.

Can anyone help? thanks

Upvotes: 0

Views: 207

Answers (3)

ctrl-alt-delor
ctrl-alt-delor

Reputation: 7735

Code 7 is bell. It is meant to do that.

To disable it, you have 2 choices.

  • Change the configuration of the terminal or OS (tell it to be silent).
  • Add a conditional to the code, to skip this character.

To do the conditional: use isprint

e.g.

#include <ctype.h>
#include <iostream>

int main(){
    int c =7;
    
    if (isprint(c))
        std::cout <<'\n' << static_cast<char>(c);
}

Upvotes: 5

J. Doe
J. Doe

Reputation: 184

Sorry to be answering my own question, il accept someone elses answer as the best answer, but to print only printable characters and skip all other ones then you need to skip the character codes 0-31 (ones below 31) and only print out the above

Upvotes: 0

Aki Suihkonen
Aki Suihkonen

Reputation: 20027

The purpose of the Ascii character 0x7 or Bell is to start an annoying beep. To disable it, one needs to find the proper control in the terminal or OS preferences, such as Windows Beep Properties.

Upvotes: 4

Related Questions