jeudi 2 mars 2017

How do I declare a variable that is not a number?

i mean, 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;
    }
}

Aucun commentaire:

Enregistrer un commentaire