the input buffer in getchar() and scanf

2019-06-08 13:16发布

Sorry because I can't think of any better title I have a problem dealing with the input Here is my test code which get the input into a string array then print it on screen again, quite simple

The 1st test code work fine as expected

#include <stdio.h>
int main()
{
    int i, index;
    char d, c[20];
    scanf("%d", index);
    for(i=0; i<index; i++){
        *(c+i) = getchar();
        if (*(c+i)=='$')
            break;
    }
    printf("the string is %s", c);
    return 0;
}

the 2nd test code, I did use pointer but c[i] instead and the program didn't run. It's quite strange since *(c+i) and c[i] is equivalent

I changed

        *(c+i) = getchar(); // (0)
        if (*(c+i)=='$')

to

        c[i] = getchar(); // (1)
        if (c[i]=='$')

or

        c[i] = getchar(); // (2)
        if (*(c+i)=='$')

or

        *(c+1) = getchar(); // (3)
        if (c[i]=='$')

the (3) work fine, no problem happened but in (1) and (2), the output is just the first character of the input regardless how long the string input

Ex: type asdads$

output: a

So the problem lie in the getchar() code there is a different between using pointer and direct element of array to get the input but I don't know what is the problem here

I'll call this problem is "A" because I encounter several problem as I tried different kind of code with scanf and getchar

Now I change the getchar() in each case into scanf

(0)(1)(2)(3): problem A happen. Now none of them work fine with scanf. With getchar, at least (0) and (3) work.

Now, instead of input data directly into c, I input data into d and pass that value of d to c[i]

Usind d = getchar(), problem A happen to all 4 case. scanf give the same result

What is strange is when I hit enter, the program auto break and output is print to screen

That is just a test but here is the current problem I'm dealing with

    int z;
    printf("continue? [1=yes/0=no] ");
    scanf("%d", &z);
    switch (z){
        case 1: printf("next info is: ");
                scanf("%d", &x);
                break;
        case 0: *end = '\0';
                break;
    }

This work fine, using scanf with %d is ok, it wait for me to hit 1 or 0 to continue the process. However the problem happened with these 2 code which are expected to perform the same. They don't wait me to hit y/n but go directly to the end

    char z;
    printf("continue? [y/n] ");
    z=getchar(); // using getchar
    if(z=='y'){
                printf("next info is: ");
                scanf("%d", &x);

    }
    else{

        *end = '\0';
    }

and

    char z;
    printf("continue? [y/n] ");
    z=getchar();
    switch(z=='y'){
        case 1: printf("next info is: ");
                scanf("%d", &x);
                break;
        case 0: *end = '\0';
                break;
    }

So I don't know what is wrong with the code I use and why using pointer to get data is different from using array directly.

2条回答
We Are One
2楼-- · 2019-06-08 14:00
#include <stdio.h>
int main()
{
    int i, index;
    char d, c[20];
    scanf("%d",index);//*2
    for(i=0; i<index; i++){
    c[i] = getchar(); // (1)
    if (c[i]=='$')
        break;
}
printf("%d",index); //*1
printf("the string is %s", c);
return 0;
}

If you printf the no. of index(*1) you will know when some situation index = 1. The only space is for NULL in the string.So, There are some problem of your program and it isn't the problem of (1)(2)(3).

You might change the program:

*2 to scanf("%d",&index);

After then, you will complie (1)(2)(3) sucessfully.

查看更多
来,给爷笑一个
3楼-- · 2019-06-08 14:06

Yes! first of all scanf("%d",&index); must be used you have used it wrong way. You have to pass the address of the variable.

Secondly, in the input buffer the '\n' pressed after a given input is there. You have to put a dummy getchar over here that will consume any extra '\n'.

scanf("%d", index);
getchar();
    for(i=0; i<index; i++){
        *(c+i) = getchar();
        if (*(c+i)=='$')
            break;
    }

Now in the second case(where you use a int as input) it works as '\n'(or any white space) is character is ignored by the scanf (the format specifiee is "%d" not "%c").

查看更多
登录 后发表回答