Please see the red marked statements below.If i try printf("%d",ch) instead of printf("%c",ch) then the loop is infinte before running for once and asking for the value of ch variable and keeps repeating the menu section of the code. and if I use printf("%c",ch) then while debugging i see that ch contains \n instead of y while checking with the do-while loop. Code: int main() { int i,j; char ch='y'; do { printf("\t\t*** Menu************\n"); printf("\t1.Create \n\t2.Delete \n\t3.Search\n\t4.Add the element\n\t5.Display Tree\n\t6.exit\n"); printf("Enter ur choice:\t"); scanf("%d",&i); switch(i) { case 1: create_tree(); break; case 2: printf("Enter the node to be deleted :\t"); scanf("%d",&j); delete_tree(j); break; case 3: printf("\nenter the element :\t"); scanf("%d",&j); search(j); break; case 4: printf("\nEnter the element to be added:\t"); scanf("%d",&j); add(j,root); break; case 5: display(root); break; case 6: exit(0); } //switch ends here [U][B] [COLOR=Red]printf("\n If wish to continue, press 'y' :\t"); scanf("%d",&ch); [/COLOR][/B][/U][COLOR=Red] [/COLOR] // if %c is used instead of %d,ch is not taken and do while is exited }[COLOR=Red]while( ch=='y')[/COLOR]; //do while ends here exit(0); }
thanks for the reply,sir. i have bypassed this problem using int variable instead of char.so now i want know why this happened?whats going on internally? thanks..
it does not matter whether you are taking int or char... scanf function taking input from standard input like keyboard and stroring in standard input buffer. Scanf is not resetting the values in buffer. For next time properly use of scanf function you have to fflush means you have to reset buffer as already told by shabbir.
if so then why i am getting this problem when i took int ch and now as option i ask for 0/1 and very nicely scanf is obliging now. if the problem is of input buffer then ch as int should also get corrupted like when i took ch as char . thanks for help ..
This kind of problem is exactly why those of us that have been round the block a few times don't bother with scanf for input but use fgets to read a whole line of input from the user and parse it ourselves, e.g.: Code: printf("Enter a number :"); char buf[32]; fgets(buf,30,stdin); // args may be the wrong way round here; this is from memory int num=atoi(buf); printf("You entered %d\n",num); scanf and family are best used when the exact input format is completely known, which is not the case for user input, and is certainly not the case when you don't really understand what it's doing. So I don't see why teachers insist on using scanf for input - maybe because it mirrors printf, but fgets is a lot more reliable and you can use fgets then sscanf if you really want to use a scanf-type function.
thanks for reply... as u said-"scanf and family are best used when the exact input format is completely known". in my case the format is already known and it is char type.and still it goes crazy..instead when i used int type then my program is all perfect. and thanks for giving me another way to take the user input.. thanks again..
shabbir suggested you use fflush before calling scanf with the char type. Did that solve the problem?
hey, thanks all for the help. I tried using fflush just before calling scanf function like: fflush(stdin ); scanf("%c",ch); ..the problem persists..it never ask for the input option once it has taken .and it contains "\n" as value. i use freebsd platform and gcc compiler.
@k0der replace scanf("%c", &ch) with this: scanf("%*[ \n\t]%c",&ch); this will skip blanks, newlines n tabs. in scanf above after '[' there is space define ch as char. cheers