【问题标题】:How to end an input when enter is passed通过输入时如何结束输入
【发布时间】:2014-11-27 05:53:21
【问题描述】:
#include <stdio.h>
#include <string.h>
int main()
{
    int array[1000]={0}, n, c, d, swap;
    printf("Enter number of elements\n");
    scanf("%d", &n);

    printf("Enter %d integers\n", n);

    for (c = 0; c<n; c++)
        scanf("%d", &array[c]);


    for (c = 0 ; c < ( n - 1 ); c++)
    {
        for (d = 0 ; d < n - c - 1; d++)
        {
            if (array[d] > array[d+1]) /* For decreasing order use < */
            {
                swap       = array[d];
                array[d]   = array[d+1];
                array[d+1] = swap;
            }
        }
    }

    printf("Sorted list in ascending order:\n");

    for ( c = 0 ; c < n ; c++ )
        printf("%d\n", array[c]);

    int result= array[n-1]-array[0];
    printf("The difference between the largest and smallest: %d",result);
    puts("");
    return 0;
}

这个程序首先对输入进行冒泡排序,并给出最大和最小数字之差的输出。我想在输入enter 时结束输入。例如,输入 = 6 4 2,输出 = 4。 (以'enter'结束输入)

【问题讨论】:

  • How to end an input when enter is passed 对于几乎所有的 i/p API,已通过按 ENTER 标记输入结束。请正确陈述您的问题。
  • 不,输入结束(即终端上的文件结束)不是 ENTER 键(在 Linux 上,它是 Ctrl-D

标签: c


【解决方案1】:

Enter'\n' 发生时结束输入是使用 scanf("%d",... 的挑战,因为 "%d" 首先会消耗任何空白,包括 '\n'。首先需要一种不同的方式来观察'\n'

for (c = 0; c<n; c++)
  int ch;
  while ((ch = fgetc(stdin)) != '\n' && isspace(ch));
  if (ch == '\n' || ch == EOF) break;
  ungetc(ch, stdin);
  if (scanf("%d", &array[c]) != 1) Handle_NonNumericInput();
}

或者更好的是,使用fgets()。轻松捕捉各种无效输入。

#include <limits.h>
#define MAX_INT_SIZE (sizeof(int)*CHAR_BIT/3 + 3)

c = 0;
char buf[n*(MAX_INT_SIZE + 1) + 2];
if (fgets(buf, sizeof buf, stdin)) {
  char *p = buf;
  for (; c<n; c++)
    int n;
    if (sscanf(p, "%d %n", &array[c], &n) != 1) break;
    p += n;
  }
  if (*p) Handle_Missing_or_Extra_or_Nonnumeric_input(); 
}

【讨论】:

    【解决方案2】:

    您需要阅读整行,例如使用fgets(3)getline(3),甚至(如果在终端上运行,例如在 Linux 下)readline(3)(提供编辑功能)。然后使用sscanf(3)strtol(3)将每一行转换为数字,并测试何时转换失败。

    类似

     const int arraysize = sizeof(array)/sizeof(array[0]); // 1000
     printf("Enter at most %d integers ended by a blank line\n", arraysize);
     for (c = 0; c<arraysize; c++) {
        char linebuf[80];
        memset(linebuf, 0, sizeof(linebuf)); // perhaps unneeded
        if (!fgets(linebuf, sizeof(linebuf), stdin))
           break;
        if (sscanf(linebuf, "%d", &array[c])<=0)
           break;
     }
     // here c contains the number of actually read integers.
    

    阅读您正在使用的每个函数的文档。请注意,scanffscanfsscanf 正在返回成功扫描项目的计数。

    清除memset(3) 可能是不需要的.. 但以防万一我总是清除缓冲区。

    【讨论】:

    • 次要:' '" %d" 中不需要。 "%d" 即使没有 ' ' 也会消耗前导空格
    【解决方案3】:

    我从您的问题中了解到,您想在输入数字后打破以下 for 循环。

    for (c = 0; c<n; c++)
        scanf("%d", &array[c]);
    

    如果是这样的话。然后你可以使用以下循环。

        for (c = 0; c < n; c++) {
            if (scanf("%d", &array[c]) != 1 ) {
                  n = c; // actual number of integer input.
                  break;
            }
        }
    

    现在,如果您输入一些字母,然后按回车键,则它会中断 for 循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 2016-08-01
      • 2014-01-06
      相关资源
      最近更新 更多