Mothi Mohammed
Mothi Mohammed

Reputation: 43

The scanf function is accepting values infinately


int n;
int a[maxsize];
int b[maxsize];
int c[maxsize];
int i;
printf("enter number of  elements(disks)\n");
scanf("%d",&n);
printf("enter the elements in ascending  order\n");
for(i=0;i<n;i++)
{   
    scanf("%d",&a[i]);
}

This works fine at times, but most of the times this piece of code is going into infinite loop,the 'scanf' in the loop is accepting values infinitely,i tried using the function (fflush) to clear the buffer contents,but still its not working, someone please help me out!! and please explain why !!

Upvotes: 1

Views: 148

Answers (2)

user3121023
user3121023

Reputation: 8286

scanf will return the number of items successfully scanned.
If scanf does not return 1, a character is read and scanf tries again.
scanf ( "%*[^0-9\n]"); will read and discard any characters that are NOT numbers or a newline.

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

#define SIZE 10

int main()
{
    int i;
    int n;
    int a[SIZE];
    printf("enter number of elements(disks) 0-%d\n", SIZE - 1);
    while ( scanf("%d",&n) != 1 || n >= SIZE) {
        scanf ( "%*[^0-9\n]");
        printf ( "problem with input, try again\n");
    }
    printf("number was %d\n", n);
    printf("enter the elements in ascending  order\n");
    for(i=0;i<n;i++)
    {
        while ( scanf("%d",&a[i]) != 1) {
            scanf ( "%*[^-0-9\n]");//[^-0-9\n] characters NOT digit, newline or minus
            printf ( "problem with input, try again\n");
        }
        printf("number for a[%d] was %d\n", i, a[i]);
    }
    return 0;
}

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

The code posted cannot enter an infinite loop, it's probable that the scanf() function is blocking until you input something like a Ctrl+D to end the input stream or maybe another integer, the problem is that you are handling the input in a very dangerous way because you are not checking for errors at all, what might do what you exactly want is this

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

#define clearstdin() do {int chr; while (((chr = getchar()) != EOF) && (chr != '\n')); } while (0)
#define SOMELARGESIZE 1024

int main(void)
{
    unsigned int index;
    unsigned int size;
    int          result;

    fprintf(stderr, "input the desired array size: ");
    while (((result = scanf("%u", &size)) != 1) && (result != EOF))
     {
        fprintf(stderr, "invalid input, try again\n");
        clearstdin();
     }
    if (result == EOF)
     {
        fprintf(stderr, "EOF recieved, ending the program\n");
        return -1;
     }
    if (size < SOMELARGESIZE)
     {
        int array[size];

        for (index = 0 ; index < size ; index++)
         {
            fprintf(stderr, "input an integer: ");
            if (((result = scanf("%d", &array[index])) == 1) && (result != EOF))
                fprintf(stdout, "\tarray[%d] = %d\n", index, array[index]);
            else if (result == EOF)
             {
                fprintf(stderr, "EOF recieved, ending the program\n");
                return -1;
             }
            else
             {
                fprintf(stderr, "invalid input, try again\n");
                index -= 1;
             }
            clearstdin();
         }
     }
    else
     {
        fprintf(stderr, "sorry, you requested a very large array\n");
        return -1;
     }
    return 0;
}

the only problem with the program above, is that if you input any white space character while scanf() is wating for input it will do nothing, until valid or invalid input but specifically non-white-space input is entered.

Upvotes: 1

Related Questions