views:

63

answers:

4

I am trying to take five character and 5 float input.

main()  
{    
char c[5];    
float q[5];    
int i;

for(i=0;i<5;i++)  
{   
        printf("\n%d ",i);  
    scanf("%c",c+i);   
    scanf("%f",q+i);  
    }  
}

But the output is absurd. After two sequential scans, it skips third scan and then again skips fifth scan. I am not able to understand why is it showing such a behaviour. I am working on gcc compiler.

A: 

fflush() is not defined on an input stream, like stdin. Don't do it.

If you want to "read and discard until newline", then do:

int ch;
do {
    ch = getchar();
} while (ch != EOF && ch != '\n');

Note that %c means "read the next character in the input stream, even if it's whitespace, then stop". %f means "read and discard whitespace, then try to read a float from the input stream, then stop."

caf
Its still not working.
Mohit
+1  A: 

Use of scanf is not recommended because of problems like this.

Instead use fgets to read the entire line and then use sscanf to extract what you want(a char or a float) from the line just read:

    char line[MAX];
    for(i=0;i<5;i++)
    {
            if( fgets(line,MAX,stdin) && sscanf(line,"%c", c+i)!=1 )
                    *(c+i) = 0;
            if( fgets(line,MAX,stdin) && sscanf(line,"%f", q+i)!=1 )
                    *(q+i) = 0.0;
            printf("%c %f\n",*(c+i),*(q+i));

    }
codaddict
I am not able to find the reason for such a behaviour of my code. Can you please explain it to me.
Mohit
+1  A: 

To directly answer why the 3rd and every other scan "skips", it is the way scanf() and the %c format works. When there is a call to scanf(), you typically have to press enter to "submit" the input. Consequently that inserts a newline character into the stdin stream.

When the previous float scan got inputted, the newline is still left in the stream. When the character scan gets reached, that remaining newline character is read in since it fits effectively "skipping" the call.

You should use fgets() with sscanf() as codaddict suggests.

But as a quick fix, you could try adding a call to getchar() after the float scan to consume that newline character from the stream.

edit:
You're saying this doesn't work? (assuming you input the correct kinds of values, one per scanf call)

main()
{
    char c[5];
    float q[5];
    int i;

    for(i=0;i<5;i++)
    {
        printf("\n%d ",i);
        scanf("%c",c+i);
        scanf("%f",q+i);
        getchar();
    }
}
Jeff M
even the getchar() thing is not working. I even used fflush(stdin), but still the result is same.
Mohit
Its still not working.
Mohit
Thank you, This is working. I got it wrong first time.
Mohit
A: 

Your code should be like this :

main()  
{    
char c[5];    
float q[5];    
int i;

for(i=0;i<5;i++)  
{   
        printf("\n%d ",i);  
    scanf("%c",c+i);
    while (getchar()!='\n');   
    scanf("%f",q+i);
    while (getchar()!='\n');   
    }  
}

the sentence while (getchar()!='\n'); search till the end of input, so it would not take '\n' as an input value for q+i.Also another while (getchar()!='\n'); after scanf q+i,since you use loop.

Singgum3b