在c中查找一组数字的范围

时间:2016-09-20 09:00:13

标签: c

#include "stdafx.h"

int main()
{
    int num, max = -32768, min = 32767, range;
    char choice = 'y';
    while (choice == 'y')
    {
        printf("\nenter any number ");
        scanf("%d", &num);
        if (num>max)
            max = num;
        if (num<min)
            min = num;
        range = max - min;
        printf("Range Is %d", range);
        printf("\nYou Want To Add Another Number(y/n) ");
        fflush(stdin);
        scanf("%c", &choice);
    }
    return 0;
}

一次输入后,即使“&#39; y”,控制也会退出程序。键是按下的。 我试图理解为什么它退出我的主循环

2 个答案:

答案 0 :(得分:1)

您的代码存在的问题是scanf保留了'\ n'输入。

您应该开始使用readline代替scanf,有关详细信息,请参阅here

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

int     main()
{
  int           ret, num, max = -32768, min = 32767, range;
  char          *line = NULL;
  size_t        len = 0;
  ssize_t       read;
  char          choice = 'y';

  ret = 0;
  while (choice == 'y')
    {
      printf("\nenter any number ");
      if ((read = getline(&line, &len, stdin)) == -1)
        break ;
      num = atoi(line);
      if (num > max)
        max = num;
      if (num < min)
        min = num;
      range = max - min;
      printf("Range Is %d", range);
      printf("\nYou Want To Add Another Number(y/n) ");
      if ((read = getline(&line, &len, stdin)) == -1)
        break ;
      choice = line[0];
    }
  if (line) // line should be freed even if getline failed
    free(line);
  return 0;
}

所以它是如何运作的?

if ((read = getline(&line, &len, stdin)) == -1)
  break ; // break the while if getline failed

此处getline采用 3 参数:

  • 指向存储用户输入的line的指针
  • 指向len的指针,其中line的大小以字节存储
  • stdin这是标准输入的文件流(FILE *

来自man 3 getline

  

ssize_t getline(char ** lineptr,size_t * n,FILE * stream);

     

getline()从流中读取整行,存储地址   缓冲区包含*lineptr的文本。缓冲区是   以null结尾并包含换行符(如果找到)。

     

如果*lineptr设置为 NULL 并且*n在调用之前设置为0,则getline()将分配缓冲区来存储该行。这个   即使getline()失败,用户程序也应释放缓冲区。

答案 1 :(得分:0)

首先解决您的问题。将 " %c" 替换为 #include <stdio.h> int main(void) { int max, min, holder, no_of_inputs, tally; char consent; int array[2]; printf("TOTAL NUMBER OF INPUTS ANTICIPATED: "); scanf("%d", &no_of_inputs); printf("INPUT 1: "); scanf("%d", &array[0]); printf("INPUT 2: "); scanf("%d", &array[1]); if (array[0] > array[1]) max = array[0], min = array[1]; else max = array[1], min = array[0]; for (tally = 3; tally <= no_of_inputs; ++tally) { printf("INPUT %d: ", tally); scanf("%d", &holder); if (holder > max) max = holder; else min = holder; } printf("RANGE: %d.", max - min); return 0; }. 很简单。是的,在 C 之前留一个空格。我也写了一个编。如果你想看:

#include <stdio.h>

int main(void)
{
int max, min, holder, no_of_inputs;
char consent;
int array[2];
printf("INPUT:  ");
scanf("%d", &array[0]);
printf("INPUT:  ");
scanf("%d", &array[1]);
if (array[0] > array[1])
{
    max = array[0], min = array[1];
}
else
{
    max = array[1], min = array[0];
};
while (1 > 0)
{
    printf("WANNA GIVE MORE INPUT(y/n): ");
    scanf(" %c", &consent); //notice the space.
    if (consent == 'n')
    {
        break;
    }

    else if (consent == 'y')
    {
        printf("INPUT:  ");
        scanf("%d", &holder);
        if (holder > max)
        {
            max = holder;
        }
        else if (holder < min)
        {
            min = holder;
        }
    }
    else
    {
        printf("ERROR!!\n");
        break;
    };
}
printf("RANGE: %d", max - min);
return 0;
}

如果你真的想去是/否,那就是:

{{1}}

谢谢。