Riccado
Riccado

Reputation: 7

How to end an input when enter is passed

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

This program bubble sorts the inputs first and gives the output of the difference between the largest and smallest number. I want to end the input when I type enter. For example, input = 6 4 2 , output= 4. (end the input by 'enter')

Upvotes: 0

Views: 2515

Answers (3)

Vishal Gupta
Vishal Gupta

Reputation: 244

What I understand from your problem is you want to break the following for loop after entering the numbers.

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

If that's the case. Then you can use following loop.

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

Now if you enter some alphabet followed by a enter key then it breaks the for loop.

Upvotes: 0

You need to read a whole line, e.g. with fgets(3), getline(3), or even (if running on a terminal, e.g. under Linux) readline(3) (which offers editing abilities). Then convert each line to a number using sscanf(3) or strtol(3) and test when the conversion fails.

Something like

 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.

Read the documentation of every function that you are using. Notice that scanf, fscanf and sscanfare returning the count of successfully scanned items.

The clearing memset(3) is probably unneeded .. but just in case I always clear buffer.

Upvotes: 0

chux
chux

Reputation: 153488

To end input when Enter or '\n' occurs is challenging using scanf("%d",... as "%d" first consumes any white-space including '\n'. Need a different way to watch for '\n' first.

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();
}

or better yet, use fgets(). Easy to catch all sorts of invalid input.

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

Upvotes: 1

Related Questions