开发者

Problem while scanning a char and float simultaneously

开发者 https://www.devze.com 2023-01-18 01:20 出处:网络
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++)

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.


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));

    }


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();
    }
}


You should try this:

    int main(){
    char c[6];//char array size must be 6 to store five charecter
              //as null-terminator('\0')will use the last one
    float q[5];
    int i;
    for(i=0;i<5;i++){
            printf("\n%d\n",i);fflush(stdout);
            scanf("%c",&c[i]);fflush(stdin);//fflush(stdin) to clear input stream that
                                            //next scanf() won't skip
            scanf("%f",&q[i]);fflush(stdin);//fflush(stdin) to clear input stream that
                                            //scanf() won't skip at new loop
    }
    return 0;
    }


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


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.


Problem in scanning a char value after a float value.

Solution is very simple!!!

instead of writting your code like this

scanf("%c",&...)

try this,

scanf(" %c",&...)

A Space before the"%c" will resolve the problem by neglecting the value of the Return(Enter) key when pressed after typing the value of the float as an input.

When a float value is scanned before a character value. The value obtained by pressing the Return(Enter) key is collected in the following char variable. Using a space before(" %c",&...) discards the value collected of the Return(Enter) Key, Causing the Char value to be scanned in the next line. Thus solving The Scanning Float-Char problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消