Here is a small program i have written and I cannot figure out why my keyboard buffer is not behaving bad like it shouldwith this flawed code.
also keep in mind this is not all of the code the part with the menu printf stuff has been removed. sorry had to retype the whole program seeing im not at home.
this code only tested in linux by the way.
Code:
#include<stdlib.h>
#include<stdio.h>
int menu(void);
int main()
{
int vaid = 0;
printf("Below is the return from menu.\n%d\n",menu());
scanf("%d",&vaid);
printf("%d\n",vaid);
}
int menu (void)
{
int option = 0;
int errchk;
char *data;
data = (char *) malloc (4); // shouldent need much more then 4
do {
// in here is some code to display a menu yada yada
scanf("%s",data); // get only one option
errchk = sscanf(data,"%d",&option);
if (errchk != 1) // there should only be one option found
{
return 4; // not one of the options so it makes the main loop loop again.
}
}
while (option == 0 || option > 3);
free (data);
return option;
}
when this is compiled and run here is the output
me@Nightshade$ ./testmen
Please enter a selection
1: To get APM info
2: To get process info
3: To exit
:>tttttttttttttttttttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttt <-- this is what i enterd
below is the return value from menu
4
<-- then it waits for the user to enter something again if the buffer had data still it should auto pop this area
but it doesent?
does free() somehow dump the buffers too ?