c - Where did getchar() go in the second run -


I am trying to create an interactive socket communication client program that sends messages to a server when the user asks it and receives it. Also the incoming message on the same socket (once connected) Since these messages can exit the order, and I am using non-blocked IO, I intend to test a flag at the end of each selection If the user Fie option has interrupted the program to enter.

get_user_opts () for the first call, the function runs fine and takes the input from the user, but for later calls, this does not wait for user input.

It says to run in GDB:

  getchar () at getchar.c: 37 37 getchar.c: There is no such file or directory. getchar.c (gdb) at 38 getchar.c (gdb) 37 at getchar.c (gdb) _IO_acquire_lock_fct () libioP.h: 984 984 libioP.h: no such file or directory is LibioP.h (gdb) 985 In libioP.h in (gdb) getchar () getchar.c: 41 41 getchar.c: There is no such file or directory in getchar.c (gdb) at get_user_opts () ../testmain.c:444 444 switch (User_opt) {(gdb) 597 printf ("\ n unknown option exit \ n"); (GDB) Print User_pt $ 1 = 10 '\ n'   

The relevant code segment is:

  zero get_user_opts () {/ ***** *************************************************************************************************** ********************* * User Interactive Variable * / ****************************************************************** ************************************************************************************************ / Unsigned char user_opt; / ************************************************* Start option ********************************* start to ask for the user * / / ***** **************************************************************************************************** *********************************** *************** ********* / Printf ( "\ n ************ *********************** **************************************************************************************************** ******************************************************************** Option: Send an unggester \ n \ t4 - Add session "\" \ n Send \ t5 - Send Session Delete \ n \ t6 - Complete Startup \ n "\" ************************************************************************************** **** ****************************); // stdin; // scanf ("% c", and user_opt); User_opt = getchar (); Switch (user_opt) {case '1': ...   

The signal handler toggles a flag that is checked at the end of the loop of each time. Suspicious that may signal handler culprit is, I try to make the right Hadler without this position, and yet, to no avail.

Any thoughts?

Change:

  user_opt = getchar ();   

to

  user_opt = getchar (); While (user_opt! = '\ N' and & getchar ()! = '\ N') {// nothing}   

This will remove all those characters Follow the fast line buffer with user_opt - including the mandatory '\ n'.

The console line is buffed on most systems, so getchar () will not return until the whole line is buffer (i.e. you press enter); Then you have at least two letters buffered = you have entered and the new line by pressing Enter. If you do not delete the characters are all buffer, getchar () will be returned immediately after the call. The loop continues to get the alphabets, unless it receives a new row, but checks it on user_opt , when the user just presses the Enter.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -