Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   mysterious error in printf (http://www.go4expert.com/forums/mysterious-error-printf-t16908/)

k0der 14Apr2009 16:44

mysterious error in printf
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
then while debugging i see that ch contains \n instead of y while checking with the do-while loop.


int main()
    int i,j;
    char ch='y';
    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");
        case 1:
        case 2:
            printf("Enter the node to be deleted :\t");
        case 3:
            printf("\nenter the element :\t");

        case 4:
            printf("\nEnter the element to be added:\t");
        case 5:
        case 6:
    } //switch ends here
  printf("\n If wish to continue, press 'y' :\t");
                            // if %c is used instead of %d,ch is not taken and do while is exited
    }while( ch=='y');  //do while ends here

shabbir 14Apr2009 17:00

Re: mysterious error in printf
This is expected and try using fflush before scanf and you would be fine.

k0der 16Apr2009 15:15

Re: mysterious error in printf
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?

asadullah.ansari 16Apr2009 15:32

Re: mysterious error in printf
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.

k0der 16Apr2009 15:40

Re: mysterious error in printf
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 ..

xpi0t0s 16Apr2009 20:12

Re: mysterious error in printf
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.:

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.

k0der 18Apr2009 14:55

Re: mysterious error in printf
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..

xpi0t0s 18Apr2009 17:23

Re: mysterious error in printf
shabbir suggested you use fflush before calling scanf with the char type. Did that solve the problem?

shabbir 18Apr2009 21:31

Re: mysterious error in printf
Offtopic comment:

oops it's spelt shabbir not shabbit
I Myself also do something like this at times

k0der 28Apr2009 15:49

Re: mysterious error in printf
thanks all for the help.
I tried using fflush just before calling scanf function like:
fflush(stdin );

..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.

All times are GMT +5.5. The time now is 12:13.