how to use isDigit?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khaichiew85
    New Member
    • Feb 2009
    • 10

    how to use isDigit?

    im having a program.I want it to check if it is a character or integer is entered
    where shall i put the isDigit and how to use?i put it as highlighted below but got error.

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

    int is_prime( int input, int z);

    int main (void)
    {
    int input;
    char on;
    char option;

    printf("Press <enter> to continue\n");
    scanf("%c",&on) ;
    system("cls");
    fflush(stdin);

    //if(isdigit(inpu t))

    printf("Please enter a number:");
    scanf ("%d",&input );

    fflush(stdin);

    if( is_prime(input, 2))
    printf("\nThis number is PRIME\nThank you.\a\a\n\n");
    else
    printf("\nThis number is NOT PRIME\nThank you.\a\a\n\n");

    do{
    printf("Do you still want to continue?(Press 'Y' for YES or 'N' for No)\n");
    scanf ("%c",&optio n);

    if(option=='Y' || option=='y')
    {
    system("cls");
    return main();
    }
    else if(option=='N' || option=='n')
    return 0;
    else
    printf("Incorre ct input.The program will quit now.\n\n");
    }while(option== 'Y' || option=='y' || option=='n' || option=='N');
    }

    int is_prime( int input,int n)
    {
    if( input < 2 )
    return 0;

    else if( input == 2 )
    return 1;

    else
    if( !(input % n ))
    return 0;
    else if(n < input-1)
    return is_prime(input, n+1);
    else
    return 1;
    }
  • khaichiew85
    New Member
    • Feb 2009
    • 10

    #2
    Problem solved.
    if(!(isdigit(in put)))
    fflush(stdin);
    if( is_prime(input, 2))
    printf("\nThis number is PRIME\nThank you.\a\a\n\n");
    else
    printf("\nThis number is NOT PRIME\nThank you.\a\a\n\n");

    do{
    printf("Do you still want to continue?(Press 'Y' for YES or 'N' for No)\n");
    scanf ("%c",&optio n);

    fflush(stdin);

    if(option=='Y' || option=='y')
    {
    system("cls");
    return main();
    }
    else if(option=='N' || option=='n')
    return 0;
    else
    {
    printf("Incorre ct input.The program will quit now.\n\n");
    return 0;
    }
    }while(option== 'Y' || option=='y' || option=='n' || option=='N');

    Comment

    Working...