Comparison is always false due to limited data type error help

This is a discussion on Comparison is always false due to limited data type error help within the C Programming forums, part of the General Programming Boards category; Code: do { printf("Edit the entry's cellphone number:"); scanf("%s", addressbook[4][num]); length = strlen(addressbook[4][num]); //gets the length of the input (should ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    Comparison is always false due to limited data type error help

    Code:
    do
       { 
       printf("Edit the entry's cellphone number:");
            scanf("%s", addressbook[4][num]);
            length = strlen(addressbook[4][num]); //gets the length of    the input (should be 10)
            //checks if the input is composed of 11 elements wherein the first 2 are 0 and 9 respectively
                for(i=0; i<11; i++){
                    if ((addressbook[4][num][0] == '0') && (addressbook[4][num][1] == '9') || (addressbook[4][num][i]>='0') && (addressbook[4][num][i]<='9') && (length=='11'))
                    {
                        flag = 1;
                    break;
                    } //end if
                 
                    else { //prints restrictions/parameters for the cellphone number
                    printf("The format of the input is: 09xxyyyyyyy\nwherein x and y are integers from 0 -> 9\n");
                    break; } //end else
            } 
        } 
        while(flag!=1);
    why do I get an error here?

    Code:
     if ((addressbook[4][num][0] == '0') && (addressbook[4][num][1] == '9') || (addressbook[4][num][i]>='0') && (addressbook[4][num][i]<='9') && (length=='11'))
    And how do I fix this?

    The error message is:
    comparison is always false due to limited data type

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    You haven't really provided enough information, so I'm only guessing. In particular, the types of your variables are probably a contributor.

    However, the culprit is probably the comparison "(length == '11')" at the end of that if statement. The value of the multi-character literal is implementation defined (not 11 in your case). If length is an unsigned type, and '11' yields a negative value, that test will always give a false result.

    You might also want to look up the standard function isdigit() which is declared in <ctype.h>. It will simplify your code.

    The fact you have "break" statements in both the if and else blocks means the for loop will not iterate 11 times.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2011, 10:26 PM
  2. Replies: 8
    Last Post: 12-04-2010, 01:34 AM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 12:02 PM
  4. Data type mismatch in criteria expression ERROR
    By Aga^^ in forum C# Programming
    Replies: 2
    Last Post: 02-11-2009, 01:21 AM
  5. Replies: 2
    Last Post: 01-31-2008, 09:52 AM

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