Reading string in subfunction does not work

Discussion in 'C' started by smp, Dec 2, 2012.

  1. smp

    smp New Member

    Joined:
    Jul 8, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    I am having a rather interesting problem. I was doing a program and I was trying to read a string in a subfunction after reading an integer in main function. Now a problem occured, I couldnt read the string. The reading of string does not work.

    It worked properly when the subfunction was called without reading the integer. The problem showed up when I tried to call the fucntion after reading the integer.

    This is a small program which depicts the bug.

    Code:
    #include<stdio.h>
    void test()
    {
        char x;
        printf("testt");
        scanf("%c",&x);
    
    }
    
    main()
    {
        int c;
        scanf("%d", &c);
        test();
    }
    
    I would love to know the reason for this strange problem.

    I tried flushing the stdin also. It didnt work as well.

    Thanks
     
  2. smp

    smp New Member

    Joined:
    Jul 8, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Also if anyone wants a look at the original program where the bug was found, please find the page below. I cannot post the link because it is not allowed I guess.

    Github -> sajinmp -> nonweb-blog

    I would love every suggestion and feedback from you to improve it. It has just started and thats when i met with this bug.

    Thanks

     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    char is a single character, not a string, and %c is the format string for reading a single character.
    Try defining x as an array of char instead (make sure it is large enough for the string you want to read) and use %s.

    "Does not work" is not a very helpful description of the problem. What exactly did you input, and what exactly happened? There is no display so how do you know x doesn't contain what you put into it?
     
  4. smp

    smp New Member

    Joined:
    Jul 8, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Sorry about that. I expected to give a simple title and an example hoping that i could make it clear to you. I will give an expanded question then.

    Suppose I have a code
    Code:
    #include<stdio.h>
    void test
    {
        char a[10];
        printf("Read a string : ");
        gets(a);
    }
    main()
    {
        int x;
        printf("Enter a number : ");
        scanf("%d",&x);
        test();
    }
    
    Suppose we execute the above code.


    Running it
    Code:
    
    smp@debian$: gcc <filename> -o output
    smp@debian$: ./output
    
    
    Now I should be able to read an integer as well as a string.
    The output of executing the program

    Code:
    Enter a number : 10                         #Number could be read.
    Read a string :                                 #String could not be read. It just went past without waiting for an input.
    
    

    This is the problem. I hope you can understand it now. And sorry about the title. I thought of posting the problem but then it would turn out too big and when I thought of editing it, i couldnt edit a thread. Sorry about that

    Thanks for your help
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    This is down to the way scanf works. When you enter a number you press 1, 0, Enter. scanf reads the 1 and 0, then sees that the next character is Enter, which isn't part of a number, so it stops reading. It converts 1 and 0 to 10. The gets then looks at the input stream, which contains an Enter, so in fact it has read your string, but not the string you thought it was reading.

    If you want "10<Enter>" converting to a number, your best bet is to ditch scanf and use gets instead (reading into a temporary string), then use atoi to convert the input string to a number.
     
    smp likes this.
  6. smp

    smp New Member

    Joined:
    Jul 8, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    ok, so its the <enter> that makes the problem. Thanks boss. I got it figured out now.

    The <Enter> is basically a newline character. So this newline character is still in the input stream. To get rid of it I just need to call a getchar() function. It automatically reads this '\n' and moves on to the next line. So the simple solution is to call a getchar() function after every scanf functions.

    Thanks xpi0t0s.
     
  7. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    Hi
    As a quick fix, put fflush(stdin); immediately after your scanf("%d",&x);
     
    shabbir likes this.

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