vexe
vexe

Reputation: 5615

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

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>());       
}

Upvotes: 0

Views: 6203

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263197

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.

Upvotes: 1

reagan
reagan

Reputation: 653

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.

Upvotes: 1

Related Questions