1

I write this program to do validation of selection (integer type variable) that entered by users. But the problem is after a valid input, the next invalid input (e.g: character type variable) will not be stored in the integer variable (selection). How can I solve this?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#pragma warning (disable:4996)

void main()
{
    int selection;
    while (1)
    {
        while (1)
        {
            printf("Enter Your Selection (0-4) > ");
            scanf("%d", &selection);
            rewind(stdin);
            if (!selectionCheck(&selection, 0, 4))
                printf("Invalid\n");
            else break;
        }
        printf("Success\n");
    }

    system("pause");
}

int selectionCheck(int *input, int min, int max)
{
    char str[100] = "";
    itoa(*input, str, 10);
    if (isdigit(str[0]))
    {
        if (*input < min || *input > max)
            return 0;
        else return 1;
    }
    else
    {
        return 0;
    }
}
1
  • Not an answer, but why are you passing a pointer to selectionCheck() if you never assign a value to it?, just pass an int Commented Dec 19, 2019 at 17:45

1 Answer 1

1

Some notes: 1) You aren't checking the scanf() return value, and this is very usable: the negative return means that the entered characters can't be converted to int (because of "%d" format), and the return value equal 0 means that the input is empty (no characters entered).

2) In case that the user entered wrong character(s) (not digit(s)), the input buffer will remain busy until you read it in other way. Good idea is to use additional scanf("%s") here to read any characters as string, so buffer will be empty after this call. Using rewind() is not enough here.

3) There is no need to additional checking of input in selectionChecking() for isdigit(), because "%d" format in scanf() doesn't allow to read anything else but number.

4) There is no need to pass pointer to selection value in selectionChecking() call - it will be enough to pass it as value.

So, try this below:

// declaration of 'selectionCheck()'
int selectionCheck(int input, int min, int max);

void main()
{
    int selection;
    while (1)
    {
        while (1)
        {
            printf("Enter Your Selection (0-4) > ");

            int ret = scanf("%d", &selection);
            if (ret < 0) // invalid characters on input
            {
                printf("Invalid characters\n");
                scanf("%s"); // empty buffer, reading it as string and putting readed characters to nowhere ;)
                continue; // go to top of loop
            }

            if (ret == 0) // empty input
            {
                printf("No (empty) input\n");
                continue; // go to top of loop
            }

            // here 'ret' is greather than 0, so valid number was entered

            if (selectionCheck(selection, 0, 4)) // is value between 0 and 4 ?
                break; // yes, success, break current loop!

            printf("Invalid value\n");
        }

        printf("Success\n");
    }

    system("pause");
}

int selectionCheck(int input, int min, int max)
{
    if (input < min || input > max)
        return 0;
    else 
        return 1;
}

Of course, you can write 'selectionCheck()' more condensed:

int selectionCheck(int input, int min, int max)
{
    return (input < min || input > max) ? 0 : 1;
}

or simply:

int selectionCheck(int input, int min, int max)
{
    return (input >= min && input <= max);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.