getchar()和scanf中的输入缓冲区 [英] the input buffer in getchar() and scanf

查看:161
本文介绍了getchar()和scanf中的输入缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉因为我想不出任何更好的标题
我在处理输入时遇到问题
这是我的测试代码,它将输入转换为字符串数组,然后再次在屏幕上打印,非常简单

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

第二个测试代码,我确实使用了指针而c [i]代替了该程序没有运行。这很奇怪,因为*(c + i)和c [i]是等价的

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

我改变了

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

to

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

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

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

(3)工作正常,没有问题发生但在(1)和(2)中,输出只是输入的第一个字符,无论字符串输入多长时间

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:输入asdads $

Ex: type asdads$

输出:a

所以问题出在getchar()代码中,使用指针和数组的直接元素获取输入之间有不同之处,但我不知道这里有什么问题

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

我会称这个问题是A因为我遇到了几个问题,因为我尝试使用scanf和getchar的不同类型的代码

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

现在我改变了getchar()在每种情况下都进入scanf

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

(0)(1)(2)(3):问题A发生。现在没有一个与scanf一起正常工作。使用getchar,至少(0)和(3)工作。

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

现在,我将数据输入到d并传递d的值,而不是将数据直接输入到c中to c [i]

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(),问题A发生在所有4个案例中。 scanf给出相同的结果

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

这项工作正常,使用带有%d的scanf是好的,它等我点击1或0继续该过程。然而,这两个代码预计会执行相同的问题。他们不等我打y / n但直接到最后

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';
    }

    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.

推荐答案

是的!首先 scanf(%d,& index); 必须使用你错误的方式使用它。你必须传递变量的地址。

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.

其次,在输入缓冲区中,在给定输入之后按下'\ n'。你必须在这里放置一个虚拟getchar,它将消耗任何额外的'\ n'。

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

现在是第二种情况(使用 int 作为输入)它作为'\ n'(或任何空格)是字符被 scanf 忽略(格式指定是%d不是%c)。

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

这篇关于getchar()和scanf中的输入缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆