0

I'm trying to prevent the user from entering a wrong value(letters and symbols) in this C program but I did not find any information on this and I have no idea how to do it in my code please help me I will be very grateful

 #include <stdio.h>
    void main ()
    {
 
        int number[30];
 
        int i, j, a, n;
        printf("Enter the value of N\n");
        scanf("%d", &n);
 
        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);
 
        //  sorting begins
 
        for (i = 0; i < n; ++i) 
        {
            for (j = i + 1; j < n; ++j) 
            {
                if (number[i] < number[j]) 
                {
                    a = number[i];
                    number[i] = number[j];
                    number[j] = a;
                }
            }
        }
 
        printf("The numbers arranged in descending order are given below\n");
 
        for (i = 0; i < n; ++i) 
        {
            printf("%d\n", number[i]);
        }
 
    }
FullMetal
  • 1
  • 1
  • 4
    Unless you're working with a computer that has a programmable robotic arm that can reach out above the keyboard and slap the user's hand, you cannot prevent the user from typing something they're not supposed to. The user is going to type what the user is going to type, and all you can do is read it in and evaluate it. But this means that you can *not* read it in using, say, `scanf` and `%d`. – Steve Summit Nov 29 '22 at 19:29
  • Other than removing keys from the keyboard, there's little you can do to prevent a user from *entering* invalid input. But you *can* read their input into a string, then read the numbers from that string, displaying appropriate errors if the string is invalid. – Adrian Mole Nov 29 '22 at 19:30
  • You need to take whatever the user gives, check it, and if not right, ask again or fail. – Eugene Sh. Nov 29 '22 at 19:30
  • 1
    Step 1: always check what `scanf` returns, which must be `1` here. – Weather Vane Nov 29 '22 at 19:30
  • 1
    @Steve I like that idea. Or electric shocks from the 'wrong' keys? – Adrian Mole Nov 29 '22 at 19:31
  • `scanf` returns the number of fields that were successfully converted and assigned. `for (i = 0; (i < n) && (scanf("%d", &number[i]) == 1); ++i);` can be helpful. – David Ranieri Nov 29 '22 at 19:31
  • @AdrianMole The idea comes from the comment thread [here](https://stackoverflow.com/questions/69802478/c-quest-disable-input-values) — which, FullMetal, you might like to read, too. – Steve Summit Nov 29 '22 at 19:34
  • 1
    Unrelated suggestion: Instead of `void main()` make it `int main(void)` which is accepted by the vast majority of C compilers. `void main()` may be accepted as an extension. – Ted Lyngmo Nov 29 '22 at 19:38

1 Answers1

1

You can't stop a user from entering invalid input, but what you can do is try to validate it.

From the man page:

RETURN VALUE:

On success, these functions return the number of input items successfully matched and assigned; this can be fewer than provided for, or even zero, in the event of an early matching failure.

if((scanf("%d", &n) != 1)
{
   ... print error message here 
   exit(EXIT_FAILURE);
}

Note: Scanf would happily match and assign the value to n if the input was a floating point number, by discarding the mantissa. A number followed by some alphabets would also be accepted, and the trailing junk would be left in the input buffer for subsequence input calls to deal with.

A better option would be to read a whole line with fgets, and parse it with sscanf, strtol etc. You can refer to their man pages for help.

Harith
  • 4,663
  • 1
  • 5
  • 20