Results 1 to 4 of 4

Thread: Type checking in C

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Type checking in C

    If I ask the user for a string using gets(), how can I see if they entered a character instead of a number? I read somewhere that using gets() instead of scanf() was safer because strings can be anything and you need to check what they entered afterward, but how do I check?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    jim mcnamara
    Guest
    ctype.h has a number of macros

    isalpha() returns true if the char is an alphabetic character.
    There are a bunch of related macros all starting with "is".
    Goto Visual Studio help

    [code]

    // checks if a string is all alphabetic

    int checkalpha(char *s){
    int i;
    int result;
    result = TRUE;
    i=0;
    while(i<strlen(s) && result) result=isalpha(s[i++]);
    return result;
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    gets is unsafe because it doesn't let you specify the number of characters you want to read at max. Use fgets instead:
    Code:
    char str[100];
    fgets(str, 99, stdin);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Ok, thanks for your help guys
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width