0

Is there any way to simplify this if statement?

if(num1 != 1 && num1 != 2 && num1 != 3 && num1 != 4 && num1 != 5 && num1 != 6 && num1 != 7 && num1 != 8 && num1 != 9 && num1 != 0){
    printf("Invalid Number");
    return 0;
}

The value of num1 will be from user input, and I want to only allow numbers as value and if the user tries to input anything other than number, I want Invalid Number to be printed.

Dada
  • 6,313
  • 7
  • 24
  • 43

3 Answers3

6

It seems like a valid value for num1 is anything between 0 and 9. Hence...

if ((num1 < 0) || (num1 > 9)) {
    printf("Invalid number");
    return 0;
}

Based on your comments below, I suspect what you really want to do is read the user's input as as a text string and then validate if all the characters he typed were digits. strtol is an appropriate function for that.

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

int main()
{
    
    long value = 0;
    char* endptr = NULL;
    bool input_is_valid = false;
    char inputline[20 + 1] = { 0 };

    printf("Enter a number\n");
    scanf("%20s", inputline);

    value = strtol(inputline, &endptr, 10);
    input_is_valid = (*endptr == '\0') && (value != LONG_MAX) && (value != LONG_MIN);

    if (!input_is_valid)
    {
        printf("Invalid input\n");
        return 0;
    }

    return 0;
}
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Num1 value will be from a user input, and i wanted to only allow numbers as value and if the user try to input anything other than number, i want "Invalid Number to be printed – Randy Tunru Jul 18 '21 at 13:41
  • @RandyTunru - I'm not sure what your question is, but if you want an improved answer, you will need to show more code - specifically how you are reading the number in. (e.g. your code that invokes scanf, gets, etc...). Consider reading in the user's input as text and then comparing each char to be between the numeric literals, '0' and '9' as input validation. – selbie Jul 18 '21 at 18:14
  • I updated my answer to show the extended way to read input and validate it using strtol. – selbie Jul 18 '21 at 18:33
0

@Selbie's answers is probably better for your scenario where the numbers are part of a sequence but if that wasn't the case you could have an array with the possible answers and then check if the number (num1) you are looking for is contained inside the array.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
  • I updated my question, can you check it again – Randy Tunru Jul 18 '21 at 13:42
  • I think you should update the title for the question too, as with this new update it's not the same issue as you first asked. What you're trying now is to check if a string is a number. There are multiple ways to do this such as what is described here: https://stackoverflow.com/a/32313369/6654475 – Francislainy Campos Jul 18 '21 at 17:15
0
#include <stdio.h>
#include <ctype.h>

int main()
{
    char num1;
    printf("Enter num1\n");
    scanf(" %c",&num1);
    if(isdigit(num1) == 0){
        printf("Invalid input");
    }
    else{
        printf("Valid input");
    }

    return 0;
}

More details about isdigit here

HiEd
  • 160
  • 3