newbie question - checking if a char array stores a number.

This is a discussion on newbie question - checking if a char array stores a number. within the C Programming forums, part of the General Programming Boards category; Hi Yeah, it sounds like a strange thing to do " checking if a char array stores a number", and ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    13

    newbie question - checking if a char array stores a number.

    Hi
    Yeah, it sounds like a strange thing to do " checking if a char array stores a number", and your probably wondering why there'd be a number there anyway. Well, trust me, there is a reason.

    The thing that's confusing me, is that I assumed it would be simple enough to check if there was a specific number in a char array like so:

    Code:
    for ( i=0 ; i<stringlength ; i++){
    	if ( string[count]==1 ){
                        printf("One.\n");
     	}
    }
    What i'm actually trying to do is a little more complicated than that, but that's the problem simplified. For some reason it's just reading straight passed any 1's and continuing on without printing "one".
    I also tried casting it (string[count]) to an integer, but still had no luck.

    Please help me out.

    Thanks,
    turmoil

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    15
    Shouldn't the array index be i not count?

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    13
    eeer yeah . whoops. count is what i'm using in my actual code, so i got a little distracted making that example....

    so i meant:

    Code:
    for ( i=0 ; i<stringlength ; i++){
         if ( string[i]==1 ){
                        printf("One.\n");
         }
    }
    But either way, it still doesn't read any 1's in the string, when there definately are 1's there (amongst the char's).

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    if( string[i] == '1' )

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    And a more general way is to include ctype.h and then use isdigit()

    if(isdigit(myarray[index]))
    .. do something
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    13
    hey thanks heaps quzah! That's all i needed.

    (Can't use isdigit as i'm looking for specific numbers.)

    cheers,
    tumoil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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