0

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>());       
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
vexe
  • 5,433
  • 12
  • 52
  • 81
  • You don't show us the code that calls `getch()`. There is no such function in standard C or C++. There's a `getch()` function in the curses/ncurses package, and a quite different function declared in `` in Windows. Which one are you using? – Keith Thompson Jul 09 '12 at 22:15
  • (fin) is an ifstream object and (text) is the string variable that i'm storing the text in from the file as you can see. – vexe Jul 09 '12 at 22:16
  • So what about `getch()`? – Keith Thompson Jul 09 '12 at 22:18
  • I just included: #include and used it like this: currentLetter = getch(); – vexe Jul 09 '12 at 22:18
  • Just a reminder, since you appear to be new here. If an answer is helpful, you can indicate this by upvoting it (click the up arrow icon). If one answer solves your problem, you can accept it by clicking the check mark icon. – Keith Thompson Jul 09 '12 at 22:58
  • Careful: clicking it a second time cancels it. – Keith Thompson Jul 09 '12 at 23:01

2 Answers2

1

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.

reagan
  • 653
  • 1
  • 4
  • 16
  • while(true){ char c = getch(); cout << c; if(c == '\n') break; } cout << "it won't reach this :("; – vexe Jul 09 '12 at 22:30
  • @VeXe: Does it reach it if you type Ctrl-J instead of Enter? – Keith Thompson Jul 09 '12 at 22:34
  • You are outputting `char c` before you test if it's a '\n'. If you don't want the carriage return/line feed, then you should test if it's a '\n' before `cout << c;` – reagan Jul 09 '12 at 22:36
  • @VeXe I think I misunderstood your intentions, type `ctrl+J` instead of `enter` as @Keith is saying – reagan Jul 09 '12 at 22:38
  • i also edited the code: while(true){ char c = _getch(); if(c == '\n') break; else cout << c; } cout << "it won't reach this :("; – vexe Jul 09 '12 at 22:46
  • glad we could help, please up-rate us :) – reagan Jul 09 '12 at 23:04
1

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Guys you're super awesome :D the ctrl+j thing worked for getch() and _getch() but the enter key also didn't work even with _getch() im really sorry that i didn't show you more of my code, but it's a class, i mean, am I allowed to post long code ? (it's not that long actually) and even if i did, how can i get it into good format shape in the comment (ctrl + k isn't working) – vexe Jul 09 '12 at 22:38
  • and could you please tell me more about what ;deprecated; means .. ? – vexe Jul 09 '12 at 22:40
  • @VeXe: As I said, `_getch()` returns `'\r'`, not `'\n'`, when you type Enter; check for that. And "deprecated" basically means obsolete. Since POSIX (specifically curses) defines a function by the same name, Microsoft has decided to replace `getch` by `_getch`, and to discourage the use of the old `getch`. – Keith Thompson Jul 09 '12 at 22:45
  • @VeXe: I see that my answer implied that using `_getch` rather than `getch` would fix the `'\r'` vs. `'\n'` problem. In fact, they both behave the same way. I've reorganized the first paragraph to avoid that confusion. So there are two independent things you should do: replace `getch()` by `_getch()`, and check for `'\r'` rather than `\'n'`. (Actually it's probably a good idea to check for both.) – Keith Thompson Jul 09 '12 at 22:48
  • ahaa, IT WORKED :D YOU're SUPER COOL GUYS :) ThX AlooooooT – vexe Jul 09 '12 at 22:55