C / C ++. Console input (cin / getchar)

1, similar functions are: cin, getchar, fgetc etc.

2, issue:

  The last "\ n" is not taken out ...

 2.1 Strategies: (ZC: Here is my own feeling after use)

  (1) fflush (stdin) ==> did not respond ... _flushall (); no response

  (2) int _kbhit ( void); ==>  Results allowed <conio.h>

   Online information: getchar, scanf, getch, these will be blocked if the user does not enter if you do not want to block, how should I do? Online search, Linux can use fcntl , but Windows would not (can GetAsyncKeyState check button, but do not know if there is content stdin) it was stated: Windows some. With _kbhit () (because it is immediately returned, you may want to put in circulation), judgment is not a key is pressed, then () removed by _getch.

  (3) cin.peek () ==> input buffer which has data, the accurate determination result; an input buffer is empty, the function also blocked ...

 2.2, the above methods are not ye ... now this way with the following:

  After cin, getchar, fgetc function, etc. After each call, to manually added, "getchar ()", the last "\ n" taken away ...

  Example Sub: (CIN >> bufIn) .get ();

3、

4、

5、

 

Guess you like

Origin www.cnblogs.com/cppskill/p/11888529.html