Thread: cant understand why this node is not functioning correctly

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    cant understand why this node is not functioning correctly

    anyways i have this small problem i cant seem to understand

    i have this code below

    Code:
     if( isdigit(c)) {    
    
           
            MsgNode *search;
            
          search = list;  
          
              while(search!=NULL){
                     
                if( search->messageNum == c)  
                currentItem=search;    
                
              search=search->next;
            
          }
    what the above does is if a digit is pressed when the program is run
    it is supposed to look through the nodes in the Linked Lists and specifically in the nodes struct access a data which holds the node number in the linked list, and compare it with the number which was inputed and stored in c

    c is a char because it can also take character options for the menu when program is run

    now the pointer to the node currentItem is supposed to be the one to store the pointer to that node in the Linked list which has that number in its data

    no matter what i do, i dont get any results, what am i missing here?

    i know for sure the data is there, but for some reason the if condition always fails

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    48
    Quote Originally Posted by yukapuka View Post
    anyways i have this small problem i cant seem to understand

    i have this code below

    Code:
     if( isdigit(c)) {    
    
           
            MsgNode *search;
            
          search = list;  
          
              while(search!=NULL){
                     
                if( search->messageNum == c)  
                currentItem=search;    
                
              search=search->next;
            
          }
    what the above does is if a digit is pressed when the program is run
    it is supposed to look through the nodes in the Linked Lists and specifically in the nodes struct access a data which holds the node number in the linked list, and compare it with the number which was inputed and stored in c

    c is a char because it can also take character options for the menu when program is run

    now the pointer to the node currentItem is supposed to be the one to store the pointer to that node in the Linked list which has that number in its data

    no matter what i do, i dont get any results, what am i missing here?

    i know for sure the data is there, but for some reason the if condition always fails
    also to add search is a pointer and so is list(list being a pointer to the head of the linked list)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    isdigit works with characters. It tells you whether the character in question corresponds to a digit, like '0', '1', '2', ..., '9'. Those are very different from the actual numbers 0 through 9 (note the lack of single quotes). Take a look at an ASCII chart (link) to get some insight into the difference, particularly notice the discrepancy between the Dec and Chr columns. I am guessing when the user enters '7', you want 7, but you are actually getting decimal 55, thus the compare doesn't work.


    You can also put the following line of code in there, to see the difference:
    Code:
    printf("The character entered was '%c', it's numeric value is %d\n", c, c);
    A quick fix:
    Code:
    if (isdigit(c)) {
        c -= '0';  // now c has been "translated" from it's ASCII value to a "regular" number
        ...
            if (search->messageNum == c)  // this should now work as expected
    }

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    48
    thank you very much your a legend!!! for clarifying that!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  2. precision - making sure I understand this correctly.
    By hamsteroid in forum C Programming
    Replies: 4
    Last Post: 03-25-2007, 06:41 AM
  3. Do I understand pointers correctly?
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 01-05-2006, 08:30 AM
  4. Program Not Functioning Correctly
    By TWIXMIX in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2005, 06:04 PM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM