0

I've read that getchar() should actually be preferred over _getch() ( _getch() also needs including conio.h ). However, if I use getchar() in this sample program...

#include <iostream>
#include <conio.h>
using namespace std;
 
class student {
    int id;
    int marks; 
public:
    void getdata() {
        cout << "\n Enter id";
        cin >> id;
        cout << "\n Enter marks";
        cin >> marks;
    }
    void putdata() {
        cout << "\n" << id << "\t" << marks;
    }
};
 
int main() {
    student tom;
 
    tom.getdata();
    tom.putdata();
 
    getchar();  // vs. _getch();
    return 0;
}

..then using getchar() won't wait for the input of a character to prevent the console window from being closed too early, which _getch() does.

Does anyone know the reason for this? And should getchar() really be preferred over _getch()?

BTW, I'm using MS Visual Studio 2015.

Thanks in advance and kind regards

EDIT: I'd consider my question as not being a duplicate actually, since I was wondering about the reason for the different behavior, which had not been answered under "Is there a decent wait function in C++?", and which got clarified now.

c128linux
  • 25
  • 4
  • 1
    You shouldn't use either of them. http://stackoverflow.com/a/36374595/560648 – Lightness Races in Orbit Mar 03 '17 at 18:43
  • The reason for the different behaviour is that you already entered a character (the last newline) and this is being eaten up. But that doesn't really deserve to be answered given the above. – Lightness Races in Orbit Mar 03 '17 at 18:44
  • 1
    Thanks for both comments and sorry for the duplicate question. So that's the reason, getchar() reads from stdin and _getch reads from the keyboard. I see the point why both variants shouldn't be used (though one must admit that they are really being used in a lot of tutorials and books...). – c128linux Mar 03 '17 at 19:04
  • To be honest I wasn't 100% about the duplicate, so I would be willing to re-open if enough people asked. But I think that should answer your actual need. I'm not sure why `_getch()` works so differently, and that might actually be a good question. But IMO the answer wouldn't solve your root problem. Feel free to come back to me. – Lightness Races in Orbit Mar 04 '17 at 00:38
  • And a big "yes" to your point about those tutorials and books. It's hard to escape such bad advice in the C++ world ;) – Lightness Races in Orbit Mar 04 '17 at 00:39
  • The reason I mentioned (stdin vs. keyboard) is being told here (I didn't verify this, but it sounds very reasonable): [what-is-the-difference-between-getch-and-getchar](http://stackoverflow.com/questions/9180001/what-is-the-difference-between-getch-and-getchar) Of course it's up to you whether you delete my question, but especially if you regard this thing as being important, you may prefer to leave it open to make even more people become aware of it. ;) – c128linux Mar 04 '17 at 07:54
  • Unfortunately those answers are not very good and don't really explain whether there is a different buffer underlying `_getch()` (which is possible I suppose?). It's possible though that this question is more of a duplicate of the one you've posted than the one I marked it as ;) – Lightness Races in Orbit Mar 04 '17 at 12:49
  • No, the answer is right: [_getch](https://msdn.microsoft.com/en-us/library/aa297934(v=vs.60).aspx) "The _getch function reads a single character **from the console** without echoing." vs. [getchar](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getchar.html) "getchar - get a byte from a **stdin stream**" – c128linux Mar 04 '17 at 15:51
  • But which I'd much more like to know (and which might illustrate how much I hate MS Visual Studio / Windows in general in fact, but for some reasons I have to use it currently - anyway, I'm not used to this IDE at all, so sorry if my question is stupid): I can't get your solution with the /K switch working. This is what my "External tools" configuration looks like: [External Tools](http://pasteboard.co/FpADAMInA.png) - There's already a /K switch, but I guess not at the right place or what am I doing wrong? BTW: Most easy work-around is + - Disadvantage is no debug mode then. – c128linux Mar 04 '17 at 15:59
  • I'm not saying the answer is wrong, I'm just saying it's not very good. – Lightness Races in Orbit Mar 04 '17 at 17:51
  • You say you "can't get it working"; what happens? By the looks of it there's a "close on exit" checkbox you can just turn off.. – Lightness Races in Orbit Mar 04 '17 at 17:52
  • I swear that I (of course) unchecked this box previously, which didn't work - Now I tried this again and it works. Either I've missed something / did some mistake before, or this is another reason why I'm not a fan of Microsoft. ;) Anyway, maybe you should update your answer with this information that one has to uncheck this box as it seems. – c128linux Mar 04 '17 at 18:36
  • Well to me this makes sense: If _getch() reads from the console as they say, while getchar() reads from stdin and therefore grabs the return character, it sounds plausible to me. Anyway, this question is not that important I would say, and avoiding both variants is surely the best solution. ;) – c128linux Mar 04 '17 at 18:38
  • Again, I did not claim that it is wrong or not plausible. I'm saying the answers aren't very good, because they do not provide a good, thorough, descriptive explanation. Just making a brief and vague assertion does not make a good answer! – Lightness Races in Orbit Mar 04 '17 at 19:55

0 Answers0