Niharika Sharma
Niharika Sharma

Reputation: 17

Find the range of a set of numbers in 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;
}

After one input, control exits from program even if the 'y' key is pressed. I am trying to understand why does it exit my main loop

Upvotes: 2

Views: 787

Answers (2)

Samarthya Singh
Samarthya Singh

Reputation: 96

First the solution of your problemo. It's simple replace "%c" by " %c". Yeah give a space before C. And I too wrote a prog. if you'll like to see:

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

If you really wanna go yes/no way here it is:

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

THANKS.

Upvotes: 0

plean
plean

Reputation: 124

The problem with your code was that scanf kept the '\n' input.

You should start using readline instead of scanf, see here for more info

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

So How does it work ? :

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

here getline take 3 parameters :

  • a pointer to line where user input is stored
  • a pointer to len where the size of line is stored in bytes
  • stdin which is the File Stream (FILE *) of standard input

From man 3 getline

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

getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-terminated and includes the newline character, if one was found.

If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed.

Upvotes: 1

Related Questions