Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Reading string in subfunction does not work (http://www.go4expert.com/forums/reading-string-subfunction-t29318/)

smp 2Dec2012 15:09

Reading string in subfunction does not work
 
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

smp 2Dec2012 15:17

Re: Reading string in subfunction does not work
 
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


xpi0t0s 3Dec2012 01:44

Re: Reading string in subfunction does not work
 
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?

smp 12Dec2012 12:47

Re: Reading string in subfunction does not work
 
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

xpi0t0s 12Dec2012 16:48

Re: Reading string in subfunction does not work
 
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 18Dec2012 16:02

Re: Reading string in subfunction does not work
 
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.

Chong 6Feb2013 19:18

Re: Reading string in subfunction does not work
 
Hi
As a quick fix, put fflush(stdin); immediately after your scanf("%d",&x);


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