Change character automatically whilst being input in c++? - c++

I'm having trouble working this out. Basically my program has a standard input, I want to modify this so if the user enters a space it automatically replaces it with a / whilst they're typing. I have no idea how i'd even go about doing this or if it is even possible so I am grateful for any help either way.

I believe that if you want to do manipulation of the text on the console like that, you're going to need a library such as ncurses.

You would need to turn off echoing of the characters typed and then echo them yourself after validating them, replacing characters as necessary. There are different ways to do that for different operating systems (e.g. Linux vs. Windows), and even more unfortunately, that can mean taking control of the entire screen just for this simple function since the backspace/delete key would need to be handled properly (even over multiple lines if necessary). In short, you're taking control away from the user while making yourself responsible for the common functionality that a user would expect, which makes things rather difficult because you can't know how certain terminals behave, not to mention issues with locales. It's easier just to replace the characters yourself after the input is received or issue an error message, depending on your purpose and specific use-case.
Edit: PDCurses is cross-platform, so you might try that if you're still interested. It's easier than coding for specific platforms.

Related

Is it possible to prompt for user input and provide a default?

It is not uncommon to see a command-line program that prompts the user to enter a string to be processed in some way by the program. But is it possible to create a program that does this, and has a default value already there at the prompt - so that if the user wants to use the default value, they need only press Enter? (But if the user wants to use a different value, they will have to set about editing the pre-populated string - which might mean holding down backspace to delete it and then typing something else.)
Provisos:
Needs to work on Windows. Well, for my purposes it needs to work on Windows. You can suggest a solution that only works on other OSes if you like, but I won't accept your answer.
Needs to use only what is provided by the standard library.
It's not sufficient to just say, "leave input empty to use the default value" - that's merely a workaround.
I've seen versions of this question that are specific to other languages, but not one for C++.
One can do it using just the standard library.
But one cannot do it with just the guarantees provided by the standard library.
What you need to have, for any solution, is a way to control the terminal (which implies a guarantee about being connected to a terminal, and knowledge of the idiosyncrasies in controlling said terminal.
Unfortunately, the windows console window is not a proper terminal, which can be controlled that way.

How to make a long beep in c++?

I'm making a program in c++ that reads Morse Code and prints it to the terminal, and gives the user the option to hear it. I know that, in order to make a "beep," you can use:
cout<<'\a';
However, since Morse Code contains with longer and shorter beeps, I would like to know how to make longer beeps than what would be produced by this command.
I'm using a Mac.
If you write multiple bells. i.e. cout << "\a\a\a\a" it will come out as a longer beep.
However you also need a way to write the "silence" between the beeps, and the bell sound may be different from one environment to another, so this is not really a good approach.
There is no standard library support for controlling sound on the computer, but if you look for platform-specific functionality, you are likely to find it, and ultimately this will give much better results.

What should i do if i want to use functions that are not standard and are due to compiler specific API?

I have been using the very very old Turbo C++ 3.0 compiler.
During the usage of this compiler, I have become used to functions like getch(), getche() and most importantly clrscr().
Now I have started using Visual C++ 2010 Express. This is causing a lot of problems, as most of these functions (I found this out now) are non-standard and are not available in Visual C++.
What am I to do now?
Always try to avoid them if possible or try their alternatives :
for getch() --- cin.get()
clrscr -- system("cls") // try avoiding the system commands. check : [System][1]
And for any others you can search for them .
The real question is what you are trying to do, globally.
getch and clrscr have never been portable. If you're trying
to create masks or menus in a console window, you should look
into curses or ncurses: these offer a portable solution for
such things. If it's just paging, you can probably get away
with simple outputing a large number of '\n' (for clrscr),
and std::cin.get() for getch. (But beware that this will only
return once the user has entered a new line, and will only read
one character of the line, leaving the rest in the buffer. It
is definitely not a direct replacement for getch. In fact,
std::getline or std::cin::ignore might be better choices.)
Edit:
Adding some more possiblities:
First, as Joachim Pileborg suggested in his comment, if
portability is an issue, there may be platform specific
functions for much of what you are trying to do. If all you're
concerned about is Windows (and it probably is, since system(
"cls" ) and getch() don't work elsewhere), then his comment
may be a sufficient answer.
Second, for many consoles (including xterm and the a console
window under Windows), the escape sequence "\x1b""2J" should
clear the screen. (Note that you have to enter it as two
separate string literals, since otherwise, it would be
interpreted as two characters, the first with the impossible hex
value of 0x1b2.) Don't forget about possible issues of
redirection and flushing, however.
Finally, if you're doing anything non-trivial, you should look
into curses (or ncurses, they're the same thing, but with
different implementations). It's a bit more effort to put into
action (you need explicit initialization, etc.), but it has
a getch function which does exactly what you want, and it also
has functions for explicitly positionning the curser, etc. which
may also make your code simpler. (The original curses was
developed to support the original vi editor, at UCB. Any
editor like task not being developed in its own window would
benefit enormously from it.)
Well,
People, i have found the one best solution that can be used everywhere.
I simply googled the definitions of clrscr() and gotoxy() and created a header file and added these definitions to it. Thus, i can include this file and do everything that i was doing prior.
But, i have a query too.
windows.h is there in the definition. suppose i compile the file and make a exe file. Then will i be able to run it on a linux machine?
According to me the answer has to be yes. But please tell me if i am wrong and also tell me why i am wrong.

Getting Rid of Unnecessary Input Files

I have the following file. I wrote a C++ program that copies and pastes it on another one (along with additional stuff). However, I want to get rid of it. That is, I do not want to distribute both an executable and this file. I tried hard-coding its contents, but it is tedious since there are special characters (like ", \n, etc.), and a string variable may not always have the necessary memory to hold all of that data.
What else can I do?
The first thing that I was going to suggest is to encode the content of the file. Automatically, of course. I could imagine a nightmare of typing that in. That being said, there is a variety of tools out there. For example, bin2h. Or, for instance, there is a Qt Resources framework that is indeed nice, but is probably not worth pulling in a dependency unless you already use Qt.
However, if the content of whatever you are trying to cope with is large enough that it does not fit into memory, you have no other way but stick with having that file distributed externally along with a program. In fact, this is a pretty common way of doing things. For example, most of the heavy-weight (and not only) applications for OS X (and iOS) are distributed as "bundles", which is nothing but a zip compressed file with resource file(s) and executable(s). That as well might be a solution if you are targeting one of the platforms that promote such distribution practices.
Hope it helps :)

Are there any lint tools for C and C++ that check formatting?

I have a codebase that is touched by many people. While most people make an effort to keep the code nicely formatted (e.g. consistent indentation and use of braces), some don't, and even those that do can't always do it because we all use different editors, so settings like spaces vs. tabs are different.
Is there any standard lint tool that checks that code is properly formatted, but doesn't actually change it (like indent but that returns only errors and warnings)?
While this question could be answered generally, my focus is on C and C++, because that's what this project is written in.
Google uses cpplint. This is their style guide.
The Linux kernel uses a tool that does exactly this - it's called checkpatch. You'd have to modify it to check your coding standards rather than theirs, but it could be a good basis to work from. (It is also designed for C code rather than C++).
Take a look at Vera++, it has a number of rules already available but the nice part is that you can modify them or write your own.
There are several programs that can do formatting for you automatically on save (such as Eclipse). You can have format settings that everyone can use ensuring the same formatting.
It is also possible to automatically apply such formatting when code is committed. When you use SVN, the system to do this is called svn hooks. This basically starts a program to process (or check and deny) the formatting when a commit happens.
This site explains how you can make your own. But also ones already exist to do this.