0

I want to create a program that says if a number is odd or even, but if I type a letter, the program must stop. What I want to do is to create an if-statement that allows the program to understand if what I typed is a number or something else. This is the code.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int a,b;

    printf("type a number ");
    scanf("%d", &a);
    printf("\n");

    if (a<0) //This is supposed to understand if what I typed is a number, but it's not correct at all.
    {
        printf("ERROR, YOU MUST TYPE A NUMBER, NOT LETTERS.");
        system("PAUSE \n");
        return 0;
    }

    b=(a%2);

    if (b != 0)
    {
        printf("it's an odd number! ");
        printf("\n\n");
        system("PAUSE");
        return 0;
    }
    else
    {
        printf("it's an even number! ");
        printf("\n\n");
        system("PAUSE");
        return 0;
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Eduardo
  • 17
  • 5
  • 3
    Check the return value of `scanf()` to see whether the input could be correctly interpreted as a number. – EOF Mar 02 '17 at 20:07
  • 2
    another possibility would be to use [`fgets`](https://linux.die.net/man/3/fgets) which gathers the input as a string, then see if it can successfully be converted to a number using the [`strtol`](https://linux.die.net/man/3/strtol) family of functions... this is more work though. – yano Mar 02 '17 at 20:18
  • 1
    All variables are collections of bits which the processor interprets as numbers. At a slightly higher level, we say that certain numbers are codes for non-numerical data, e.g. English letters, but only in context. – Malcolm McLean Mar 02 '17 at 20:22
  • Possible duplicate of [Check if input is integer type in C](http://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c) – zguesmi Mar 02 '17 at 20:25
  • If you were to actually check the returned value from the call to `scanf()`, it would tell the code if the input was a number or not. If the input is not a number, then `scanf()` would have returned 0. If the input was a number, then `scanf()` would have returned 1. That returned value indicates the number of successful input conversions. (note `scanf()` can also return EOF when certain problems occur or the user typed (depending on the OS) or – user3629249 Mar 03 '17 at 20:55

3 Answers3

2

scanf() returns the number of format specifiers that match, so will return zero if the text entered cannot be interpreted as a decimal integer.

printf("type a number ");
int converted = scanf("%d", &a);
printf("\n");

if( converted == 0) 
{
    printf("ERROR, YOU MUST TYPE A NUMBER, NOT LETTERS.");
    system("PAUSE \n");
    return 0;
}

Testing for less that zero will not work since -1 for example is a perfectly valid decimal integer. Had you initialised a to -1, it might have appeared to work, but you could not then enter a negative number. That said scanf() does not guarantee not to modify the arguments on failure ot match.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    It's not totally correct, because if you enter 25lsjd scanf will return 1 and it's not a valid value – zguesmi Mar 02 '17 at 20:24
  • yeah. I noticed that. But it's a start! – Eduardo Mar 02 '17 at 20:25
  • so just test if the value after the integer is an '\n' or not like in here http://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c – zguesmi Mar 02 '17 at 20:43
  • @ZiedGUESMI : You are correct, but whether a number followed by junk is an acceptable input is a design issue rather than "not correct". – Clifford Mar 04 '17 at 19:32
1

Checking if a<0 does not work in this case, as scanf() either returns 0 if no integer was found, or the number of integers found. Another case is that EOF is returned if the input is reached before any conversion is occuers. Just check if scanf() read one integer successfully:

if (scanf("%d", &a) != 1) {
    printf("Invalid number\n");
    exit(EXIT_FAILURE);
}

/* else, valid integer was found */

/* other code */

You can also just use fgets(3) to read the input string, and strtol(3) for more thorough integer conversion. Here is an example:

char number[50];
char *endptr;
int num;
size_t slen;

if (fgets(number, 50, stdin) != NULL) {

    /* removes newline appended from fgets() */
    slen = strlen(input);
    if (slen > 0 && number[slen-1] == '\n') {
        number[slen-1] = '\0';
    }

    num = strtol(number, &endptr, 10);
    if (endptr == number) {
        /* No digits found */
    }

    if (*endptr != '\0') {
        /* more characters found after number */
    }

    /* more error checking at man page */
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

You could read into a character buffer using format, say, %40s (assuming you buffer is 40 characters), then use strtol() to see if the token was an integer without extra junk at the end.

FredK
  • 4,094
  • 1
  • 9
  • 11