mysterious error in printf

Discussion in 'C' started by k0der, Apr 14, 2009.

  1. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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);
    }
     
    Last edited by a moderator: Apr 14, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    This is expected and try using fflush before scanf and you would be fine.
     
  3. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    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..
     
  4. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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.
     
  5. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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 ..
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
    shabbir likes this.
  7. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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..
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    shabbir suggested you use fflush before calling scanf with the char type. Did that solve the problem?
     
    Last edited: Apr 18, 2009
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    [COMMENT]
    I Myself also do something like this at times[/COMMENT]
     
  10. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  11. cyber_raj

    cyber_raj New Member

    Joined:
    May 12, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    programmer
    Location:
    earth
    @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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice