Validating string for integers

This is a discussion on Validating string for integers within the C Programming forums, part of the General Programming Boards category; I want to validate the string below for integers, and if there is a letter, I want the program to ...

  1. #1
    Registered User mlsbbe's Avatar
    Join Date
    Mar 2003
    Posts
    24

    Validating string for integers

    I want to validate the string below for integers, and if there is a letter, I want the program to exit.

    >>name,1,2,3,4

    if the string is something like this

    >>name,1,23,34,2a

    I want the program to exit.

    Does anybody know where to start? What functions should I use? Just point me in the general direction.

    Thanks.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hmm, try building a function by yourself, the function runs in the string and check every char to see if one of them is character (ascii value of a char) if yes, return false and this will stop the program...

    A little example can be found here, I did it in 2 minutes so don't look at the code

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int existCh (char *str) {
    	while (*(str++))
    		if (isalpha(*str)) //if its alphabetic
    			return 1; //return true, yes, exist a char
    	
    	return 0; //return false, don't exit a char
    }
    
    int main(void) {
    	char *test = "14s43";
    
    	if (existCh(test))
    		printf("Hello, in the string: %s, there is a alphabetic char!\n",test);
    	else
    		printf("Lucky, in the string: %s, there isn't any alphabetic char\n",test);
    
    	return 0;
    }
    Last edited by Vber; 04-03-2003 at 07:31 AM.

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I find that the str* functions are handy for stuff like this, just set up a string of bad characters to look for and use something like strpbrk to search for them
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static void die(char *msg);
    
    int main(void)
    {
        int rc;
        char *p;
        char buf[1001];
        /* Use whatever letters you want based on locale and testing needs */
        char *inval = "abcdefghijklmnopqrstuvwxyz";
    
        printf("Enter a string: ");
        fflush(stdout);
    
        /* Get the line */
        rc = scanf("%1000[^\n]", buf);
        if (rc == EOF)
            die("End of file reached...");
        else if (rc < 1)
            die("I/O error detected...");
        else
        {
            scanf("%*[^\n]"); /* Get rid of extraneous input */
            getchar();
        }
    
        /* Check it */
        p = strpbrk(buf, inval);
        if (p != NULL)
            fprintf(stderr, "Invalid number detected >%c<\n", *p);
        else
            printf("%s\n", buf);
    
        return EXIT_SUCCESS;
    }
    
    static void die(char *msg)
    {
        fprintf(stderr, "Fatal error: %s\n", msg);
        exit(EXIT_FAILURE);
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    while (*(str++))
      if (isalpha(*str)) //if its alphabetic
        return 1; //return true, yes, exist a char
    A minor problem, incrementing str within the while statement causes it to be incremented before the first character in the array gets checked, so you could end up with this:
    Code:
    Lucky, in the string: a1443, there isn't any alphabetic char
    There are some validation examples in the FAQ . Also, some number reading examples.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    correct sir thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21