日期类型/ scanf()

时间:2018-04-14 11:49:57

标签: c scanf

我正在努力弄清楚为什么我的scanf()正在阅读空格字符。我搜索并发现可以通过在引号和%c之间添加空格来修复问题。例如:

scanf(" %c..);

然而,这并不能解决我的问题。

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

int main(void) {
    char ch, ch1, ch2, ch3, ch4;
    ch = 'a';

    unsigned short int;

    double b = INFINITY;

    short u;
    char c;
    float f;

    printf("%c\n", ch);

    printf("%d\n", ch);

    printf("%d\n", SHRT_MAX);

    printf("%lf\n", b);

    printf("Enter char int char float: \n");
    scanf("%c %d %c %f", &ch1, &u, &ch2, &f); // This line reads correctly. Ex.  
                                              // a 5 b 5.5 

    printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

    printf("Enter char float int char: \n");
    scanf(" %c %f %d %c", &ch3, &f, &u, &ch4); // This line reads    5.5 5 a 
                                               // Here is where the first %c
                                               // is being read as a white space.
    printf("You entered: %c %0.3f %d %c\n", ch3, f, u, ch4);
    return 0;
}

修改 这是我得到的。

a
97
32767
1.#INF00
Enter char int char float:
a 5 b 5.5
You entered: a 5 b 5.500
Enter char float int char:
c 5.5 5 d
You entered:   5.500 5 d

Process returned 0 (0x0)   execution time : 11.929 s
Press any key to continue.

编辑#2

我尝试过以下操作 - 所有这些都会产生相同的输出。

printf("Enter char int char float: \n");
scanf("%c %d %c %f", &ch1, &u, &ch2, &f);
printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

fflush(stdin);

printf("Enter char float int char: \n");
scanf(" %c %f %d %c", &ch3, &f, &u, &ch4);
printf("You entered: %c %0.3f %d %c\n", ch3, f, u, ch4);

编辑#3

这是我问题的更简洁版本。

    printf("Enter char int char float: \n");
    scanf("%c %d %c %f", &ch1, &u, &ch2, &f); // This line reads correctly. Ex.  
                                              // a 5 b 5.5 

    printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

    printf("Enter char float int char: \n");
    scanf(" %c %f %d %c", &ch3, &f, &u, &ch4); // This line reads    5.5 5 a 
                                               // Here is where the first %c
                                               // is being read as a white 
                                               // space. I am expecting there 
                                               // to be a character here. Not 
                                               // a white space. See Edit #1 
                                               // for output results.
    printf("You entered: %c %0.3f %d %c\n", ch3, f, u, ch4);
    return 0;
}

3 个答案:

答案 0 :(得分:0)

试试这个:

char ch5;

scanf("%c %c %f %d %c",&ch5,&ch3, &f, &u, &ch4);

它对我有用。我所做的就是制作另一个char变量来捕获尾随的换行符。

答案 1 :(得分:0)

我找到了解决方案。但是,我仍然不确定为什么有些人成功回归,而我却没有。

至少对我来说,答案是short u;

short应为int

我不太了解C来解释原因。它实际上让我感到困惑,因为我的问题是%c,而不是%d

答案 2 :(得分:-1)

原因:您遇到此问题的原因是因为scanf()读取您作为缓冲区输入命中的返回键。在这里阅读更多相关信息:

https://cboard.cprogramming.com/c-programming/88352-scanf-also-reading-return-character-when-i-enter-value-hit-return.html

<强>解决方案: 只需在两个scanf()语句之间添加一个getchar(),它就可以正常工作。

scanf("%c %d %c %f", &ch1, &u, &ch2, &f); // This line reads correctly. Ex.  
                                          // a 5 b 5.5 
getchar();
printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

printf("Enter char float int char: \n");
scanf(" %c %f %d %c", &ch3, &f, &u, &ch4); // This line reads    5.5 5 a 
                                           // Here is where the first %c

输出

97

32767

INF

输入char int char float:

您输入了:1 b 2.500

输入char float int char:

您输入了:b 2.500 1 f

指向提交的链接https://ideone.com/GPd3HK

相关问题