Checking if characters is a valid digit

This is a discussion on Checking if characters is a valid digit within the C Programming forums, part of the General Programming Boards category; Code: int main() { const char* filename = "prefixRanges.txt"; char str[10]; int area; FILE* fp; printf("Enter:\n"); scanf("%s", str); valid(str); //fp ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Checking if characters is a valid digit

    Code:
    int main() {
        const char* filename = "prefixRanges.txt";
        char str[10];
        int area;
        FILE* fp;
        printf("Enter:\n");
        scanf("%s", str);
        valid(str);
        //fp = open(filename);
        //registered(fp, area);
        return 0;
    }
    int valid(const char* str){
        printf(str);
    }
    i need to check if the variable in the valid parameters is a digit, but i dont know how to make a char a int to check

    i tried this
    Code:
    int valid(const char* str){
        int n = (int)str;
        printf("%d", n);
    }
    but i get a random number

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    you may use atoi();

    Code:
    int n;
    n = atoi(str);
    requires the stdlib.h header.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i was thinking atoi but after i check if they are digits i need to use each digit seperately

    ie. if i have 196, i may want to do this 1+9+6 to get the sum

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    you can go back and use str to manipulate it as a string. That way you can use each digit separately.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    but it can't use the string like an array, it wont work

    like i tried str[0] but that won't work

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    a c string IS an array.
    str[0] is a char
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >like i tried str[0] but that won't work
    Code:
    #include <ctype.h>
    .
    .
    if (isdigit(str[0]))
    {
    	/* Digit found
    }

  8. #8
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by bigmac(rexdale) View Post
    i tried this
    Code:
    int valid(const char* str){
        int n = (int)str;
        printf("%d", n);
    }
    but i get a random number
    In this function str is a pointer which holds as its value the memory address of a char. We programmers usually look at a char* variable as a string variable, but when you get down to basics it is only a variable that holds the address of a char. string manipulation functions from string.h (such as strlen() and strcpy() ) know how to use the address in the context of a string, but you are still only providing them with the address of a character.

    By casting to an int as in int n = (int)str;, you are converting the address into an integer representation, and then assigning that integer representation to the variable n. So when you print n, although it looks random to you, you are printing the address that str happens to be pointing at.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    5

    Re

    You can use isdigit() function to check it and after converting it to digit with atoi() you can do-
    num=your digit
    sum=0
    loop begin where you check num is 0 or not
    divres=num/10
    sum=sum+(num%10)
    num=divres
    loop end

    so you get also the sum of digits, I think it helps you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. displaying characters per line and checking prime numbers
    By xclarkiex5x in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2007, 12:28 AM
  3. Valid move checking in chess
    By transmitt in forum Game Programming
    Replies: 6
    Last Post: 09-09-2002, 08:12 PM
  4. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM
  5. checking for valid numeric field..
    By ronkane in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2002, 08:04 PM

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