How to make getch() accept taking an enter hit (\n)? - string

I made a touch typing console program in C++.
It reads the text from a file and load it to the screen.
User must enter the right letter in order for him to proceed to the next letter.
My only Problem is with the '\n',
so if I had something like this in the text file (the file I'm reading from):
"
hello
dude
Sup
"
After the user enters "hello",
he should press enter right?
But whenever he presses enter, getch() takes him back to the beginning for the current line.
How can I fix this?
I'm reading the whole file and storing it to a string, like this:
void getTextFromFile()
{
text.assign(istreambuf_iterator<char>(fin), istreambuf_iterator<char>());
}

First of all, getch() is deprecated (just an FYI if you start having more problems with it). From my understanding you're trying to accept character input 1 char at a time. If you're using getch() for the '\n' enter press you should be fine. If not please explain more.

I just tried a quick experiment. Apparently getch() (which, as reagan says, is deprecated; use _getch() instead) returns '\r', not '\n', when you press Enter.
With your current program, try typing Ctrl-J instead of Enter; that should give you a '\n' result from getch().
And for future reference, you should show us the actual code that calls getch(). I have no idea how the currentLetter = getch(); that you mentioned in a comment relates to the code in the question.

Related

Is there an end= equivalent for inputs?

So as I'm sure you know there's a specific operator for print() functions called end.
#as an example
print('BOB', end='-')
#would output
BOB-
So is there something like this for inputs? For example, if I wanted to have an input that would look something like this:
Input here
►
-------------------------------------------------------
And have the input at the ► and be inside the dashes using something like
x = input('Input here\n►', end='-------')
Would there be some equivalent?
EDIT:
Just to be clear, everything will be printed at the same time. The input would just be on the line marked with the ►, and the ---- would be printed below it, but at the SAME time. This means that the input would be "enclosed" by the ---.
Also, there has been a comment about curses - can you please clarify on this?
Not exactly what you want, but if the --- (or ___) can also be on the same line, you could use an input prompt with \r:
input("__________\r> ")
This means: print 10 _, then go back \r to the beginning of the line, print > overwriting the first two _, then capture the input, overwriting more _. This shows the input prompt > ________. After typing some chars: > test____. Captured input: 'test'
For more complex input forms, you should consider using curses.
When using basic console IO, once a line has been ended with a newline, it's gone and can't be edited. You can't move the cursor up to do print anything above that last line, only add on a new line below.
That means that without using a specialized "console graphics" library like curses (as tobias_k suggests), you pretty much can't do what you're asking. You can mess around a little with the contents of the last line (overwriting text you've already written there), but you can't write to any line other than the last one.
To understand why console IO works this way, you should know that very early computers didn't have screens. Instead, their console output was directly printed out one line at a time on paper. While some line printers could print several characters on the same spot (to get effects line strikethrough or underline), you couldn't unprint anything once it was on the paper. Furthermore, the paper feed only worked in one direction. Once you had sent a newline character that told the printer to advance the paper, you couldn't go back to an old line again.
I believe this would solve your problem:
print(f">>> {input()} ------")
OR
print(f"{input(">>>")} ------")
F-strings are quite useful when it comes to printing text + variables.

How to print text behind an input in python 3.x

Sorry the title is confusing, but i'm not too sure how to word it appropriately.
My goal here is to have regular python input but for text for the input to be displayed to the right of where i'm typing.
For example what the user would see:
<- input your age
i'm looking for the cursor and where they type to be in that blank space, where the arrow is pointing.
Is this possible or am I being an idiot here?
Sorry if this is confusing or if I have worded this badly,
thankyou for your time.
The simplest way to achieve this is to use the carriage return (CR) ascii escape sequence. The "\r" character ill move the cursor t the beginning of the current line and begin taking input. Be careful with the amount of white space you have in the prompt since long names will begin to overwrite the prompt itself.
Note that as in #Carcigenicate's comment this will not work in every terminal or my behave differently.
name = input(" <- input your age\r")
Try the python-prompt-toolkit package.
In particular, the Adding a right prompt section.

How to go back to previous when doing find replace confirm in vi?

Frequently when I am doing a find and replace in vi I will do it like this:
:%s/find/replace/gc
This gives you the option to skip by pressing n, or replace by pressing y. But, sometimes I will accidentally skip over one in a large file by pressing n when I meant to press y.
How do I go backwards to the previous one and give me a second change?
Essentially, how to I find (search) the other direction temporarily? thanks.
I'm not sure if you would like to interrupt current find-replace operation and resume it again. But if that is acceptable, here is my suggestion:
Start your find-replace the way you mentioned:
:%s/find/replace/gc
After you accidentally skip over a substitution by pressing n, interrupt the search by pressing <ctrl-C>
Press <shift-N> to go back to the previous occurrence of your find term
Run find-replace a little differently while you are at this word: :.,$s/find/replace/gc
Continue the operation
All this functionality works with vim native capabilities without having to install any addon.
Note: The .,$ range specifier indicates to perform :s (substitute) operation over a range of lines that start with current line (indicated by .) and until last line (indicated by $).
Note2: It might be known to you, but reiterating for anyone else who stumbles upon this post searching for something similar - The % range specifier indicates to perform :s (substitute) operation over all lines of currently active buffer.
This is not answer to the question, but a very good alternative. I recently discovered the CtrlSF plugin and it improves the search /replace process dramatically.
Basically, you have the search results in a buffer and you can do all the replacements in this single buffer.
In your scenario, you first do :CtrlSF find, get a buffer with all the matches in all files and then you do /find and move with n over your targets and change them (of course, you can actually change only the first one and then repeat the replacement with .).
If you miss some target, you just hit N to go back to the previous result and replace it.
Seems like you can't back to previous match using this pattern. Appeared bar propose following commands y/n/a/q/l/^E/^Y but no one of them will return backward to previous match.
But you can use little different pattern described bellow:
Type this /pattern replacing pattern with interested word;
Your cursor is navigated to first occurrence, if you don't need to change it press n it will navigates you to the next occurrence;
Since you understand you need to replace a word, do this by typing cw, this command cuts the forward word and turns you to insertion mode;
Type in desired text on the released place and press ESC to switch back to command mode;
Now again press n until desired occurrence;
Since you realize that you need to change an occurrence, just press on . it will repeat previously mentioned actions;
If you want to go back just use N.

what is the "count" in vim?

Seeing the help of vim I have problem to understand what refers the word count I see it many times while reading the manual:
i Insert text before the cursor [count] times
It would be awesome if you give an example for it.
Vim's "Count" allows you to repeat an operator or command several times. For example, if you are on the first cursor of this line:
Hello world, how are you?
And you type dw you will have
world, how are you?
Rather than typing dwdwdwdw, you may simply type 4dw or d4w and you will have
are you?
More specific to your example, you may type something like 5ihello<esc> and this will insert
hellohellohellohellohello
Like Kevin said in a comment, you can read up more in the help docs with :h count, which says:
*count* *[count]*
[count] An optional number that may precede the command to multiply
or iterate the command. If no number is given, a count of one
is used, unless otherwise noted. Note that in this manual the
[count] is not mentioned in the description of the command,
but only in the explanation. This was done to make the
commands easier to look up. If the 'showcmd' option is on,
the (partially) entered count is shown at the bottom of the
window. You can use <Del> to erase the last digit (|N<Del>|).
Just in case somebody is wondering how count plus insert works, you have to type a number before pressing i, then it will enter in insert mode, where you can type what you want, after you press esc to go back to normal mode, it will repeat count times what you typed.
Can you provide a link to the place in the documentation where you are confused? I would have posted this as a comment instead of an answer, but my reputation is not high enough. In attempting to answer your question without clarification:
Usually [count] can be substituted with a number.
For example; consider the delete command d. If you position your cursor in the middle of a text line and type :d[count] (ex: :d4) then press an arrow key (left or right arrow); you will end up deleting [count] (e.g. 4 characters) in the corresponding direction of the arrow key.
In the case of insert I am not sure to what the argumentcount is referring.
EDIT: I forgot to mention that you must hit the enter key after :d4. Then press the left arrow key. Of course, in order for VIM to realize that you are entering a command you must exit insert mode by pressing the esc key prior to entering the command :d4.

Append general buffer to the end of every line in VI

I'm trying to add the contents of the general buffer to the end of every line. I'm sure this is fairly simple, however, an hour of google searches have lead me nowhere.
This is what my file looks like
::Things to bring camping
--matches
--tent
--sleeping bags
--inflatable bed
--firewood
--camping stove
--skillet
I want to add "::Things to bring camping" to the end of every line.
This is i have figured out so far.
/:: -> brings me to the line in question
Y -> yanks the entire line to the general buffer
I tried :%s/$/\p -> this added a "p" to the end of every line.
My problem is with step 3. How do I tell the "search and replace command" to used the "p" (the contents of the general buffer) instead of the "p" the character
Thank you so much for your help.
Just a suggestion: If you try doing it with a macro, you will be able to use 'p' to add the contents of the general buffer.
Sorry, I had to go into vim and find out.
The way to copy your entire line while in command mode, is:
^r "
(that's CTRL and r, then " )
That should paste the entire line you yanked into your search and replace command
For step three, instead of \p, you should use ctrl-R-a. Hold down the control key and type an uppercase "R", continue holding control, and type a lowercase "a".
For a line with multiple words, use ctrl-R-" instead.
I agree with using a macro - they're very powerful.
In this case I took your list example and positioned it at the first colon.
I used y$ to grab the remainder of the line in the buffer.
Then I recorded the macro - I chose 1.
q1
j$pq
Then you can call it for any number of rows in your list. E.g. 10#1
Learned something figuring this one out ...
:%s/$/\=getreg()/
The \= says that what follows is an expression to be evaluated, and the getreg() call gets the contents of the register, by default the "general buffer" as it used to be called by vi.

Resources