【问题标题】:scanf requires more inputs than requested [duplicate]scanf 需要比请求更多的输入[重复]
【发布时间】:2014-02-19 16:03:02
【问题描述】:

我正在尝试使用 scanf 从用户那里获取 5 个浮点值,问题是用户需要输入 6 个值才能完成程序。 虽然我知道我不应该使用 scanf,但它让我感到困扰,我无法掌握它。任何见解,关于如何在使用 scanf 时解决它的任何建议?

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

int main()
{
int i = 0 , j = 0;
char buf[128] = {0};

float numbers[5] = {0.0};
float keep = 0.0;

printf("Please input 5 numbers : \n");

for (i = 0; i < 5; i++)
{
 scanf("%f\n", &numbers[i]);
}

printf("Done!");

谢谢,

MIIJ

【问题讨论】:

    标签: c scanf


    【解决方案1】:

    scanf("%f\n", &amp;numbers[i]); 应该是scanf("%f", &amp;numbers[i]);

    【讨论】:

      【解决方案2】:

      你必须在 scanf() 函数中删除 \n

      #include <stdio.h>
      #include <stdlib.h>
      
      int main()
      {
          int i = 0 , j = 0;
          char buf[128] = {0};
      
          float numbers[5] = {0.0};
          float keep = 0.0;
      
          printf("Please input 5 numbers : \n");
      
          for (i = 0; i < 5; i++)
          {
              scanf("%f", &numbers[i]);
              printf("number %i is %f \n",i,numbers[i]);
          }
      
          printf("Done!");
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        • 1970-01-01
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多