c++ bell sound in eclipse using '\a' - c++

I am using Windows, Eclipse CDT and Mingw-w64. I want to call an alert ( aka bell aka beep) by using '\a' character literal.When I run this:
cout<<'\a';
computer doesn't produce any sound. I tried with endl, it didn't work. Sound on computer is on. I referred to this question, which technically asks the same thing, but it doesn't have any solution for Eclipse, it just tells that we need to try it in terminal, and tells that '\a' is not configured in Eclipse. My question is how can I force Eclipse to make the sound instead of printing square character?
P.S. I know there are other alternatives implementations for the beep sound, I want to use explicitly '\a'.
P.P.S In addition to alert literal, \b, \v, \f (backspace, vertical tab, formfeed) don't work as expected(maybe because output doesn't go to a printer), they print square chars and \r (carriage return) starts newline instead of going to the beginning of the current one.

Related

Scrolling character letter in C++

I am building a part of a tower defense game in a strictly console environment and i am stuck at the moving of a creature lets say "c", i would like the letter "c" to start on the left and move a space at a time to the right on the same line basically:
c (one second later)
c (one second later)
c and so on....
i thought that this could be implimented with an array but am lost, i want to be able to use simple code, not weird libraries and weird methods, just simple as possible. Thank you
One method is display all the characters, then a carriage return ('\r') and then reprint the line.
This allows you to "walk" characters across. This will only work on video terminals that do not advance a line upon receiving a CR.
Another method would be to print 10 backspace characters, a space, then your 10 'c'. This may not be as fast as the carriage return method above, but worth looking at.
As others have said, you may want to look into a terminal library such as ncurses. The library allows you to position the cursor on the screen, based on the terminal type. This may require setting up the console window to emulate a terminal.

Linux Printing - How To

I find it hard to explain but I will try my best. Some times in Linux- in the Terminal- things get printed but you can still write over them. eg when using wget you get a progress bar like this:
[===================> ]
Now if you type something while it is doing this it will 'overwrite' it. My question is how to recreate this in c++.
Will you use something like
cout <<
or something else?
I hope you understand what I am getting at...
btw I am using the most recent version of Arch with xfce4
Printing a carriage return character \r is typically interpreted in Linux as returning you to the beginning of the line. Try this, for example:
std::cout << "Hello\rJ";
The output will be:
Jello
This does depend on your terminal, however, so you should look up the meaning of particular control characters for your terminal.
For a more cross-platform solution and the ability to do more complex text-based user interfaces, take a look at ncurses.
You can print the special character \b to go back one space. Then you can print a space to blank it out, or another character to overwrite what was there. You can also use \r to return to the beginning of the current output line and write again from there.
Controlling the terminal involved sending various escape sequences to it, in order to move the cursor around and such.
http://www.ibiblio.org/pub/historic-linux/ftp-archives/tsx-11.mit.edu/Oct-07-1996/info/vt102.codes
You could also use ncurses to do this.

How to program a spinning cursor in c++ on OSX?

I need a progress indicator in the form of a spinning cursor for a command line program. I read a couple of threads with the advice of using the backspace character \b to delete the last printed character. However, when using \b with cout on OSX, the result is some UTF-8 character (an upside-down question mark).
Does anyone know if there is a way to get this problem solved with standard c++ means?
You can use the ncurses library for cross-platform terminal access. You csn make nice text-only UIs using this.

Why aren't my hyphens displaying correctly using std::cout?

I am trying to print out the following string using std::cout :
"Encryptor –pid1 0x34f –pid2"
the '-' characters appear as u's with a circumflex above them (I'm not sure how to type this).
How do I print out the hyphen as intended?
That was not a hyphen.
It was a "n-dash", which will render differently across consoles based on encoding settings.
The hyphen key is usually on the number row of your keyboard, on Western layouts.
Make sure your terminal's idea of the character encoding matches that of your source code. How to do this, of course, depends on your operating system, which terminal emulator (assuming it's an emulator at all) you're using, and so on, neither of which you state.
Also, that's not a hyphen in your example, it's too long. It's probably an "em dash".

Clearing terminal in Linux with C++ code

Okay, I have been researching on how to do this, but say I am running a program that has a whole bit of output on the terminal, how would I clear the screen from within my program so that I can keep my program running?
I know I can just type clear in terminal and it clears it fine, but like I said, for this program it would be more beneficial for me.
I found something that works, however, I'm not sure what it is or what it is doing.
cout << "\033[2J\033[1;1H";
That works but I have no clue what it is, if you could explain it, than I would much appreciate it.
These are ANSI escape codes. The first one (\033[2J) clears the entire screen (J) from top to bottom (2). The second code (\033[1;1H) positions the cursor at row 1, column 1.
All ANSI escapes begin with the sequence ESC[, have zero or more parameters delimited by ;, and end with a command letter (J and H in your case). \033 is the C-style octal sequence for the escape character.
See here for the full roadshow.
Instead of depending on specific escape sequences that may break in unexpected situations (though accepting that trade-off is fine, if it's what you want), you can just do the same thing you'd do at your shell:
std::system("clear");
Though generally system() is to be avoided, for a user-interactive program neither the extra shell parsing nor process overhead is significant. There's no problem with shell escaping either, in this case.
You could always fork/exec to call clear if you did want to avoid system(). If you're already using [n]curses or another terminal library, use that.
For portability you should get the string from termcap's cl (clear) capability (Clear screen and cursor home). (Or use std::system("clear") as told by Roger Pate).
man 3 termcap (in ncurses)
man 5 termcap
set | grep TERMCAP
you can write in a terminal "clear > data" and read in data the escapes sequance
0x1B[H0x1B[2J0x1B[3J
so
std::cout << "\033[H\033[2J\033[3J" ;