Comparing

This is a discussion on Comparing within the C++ Programming forums, part of the General Programming Boards category; Hi, Im trying to compare a char entered in a text box with another char. At the moment it works ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    29

    Comparing

    Hi,

    Im trying to compare a char entered in a text box with another char.

    At the moment it works fine, if the whole of the data field in the text box matches the stored char. It works.

    eg,

    text box - 01212222567
    stored char - 01212222567

    A match is produced.


    Ideally i would like the user to enter just a small number eg, 0121 and it would recognise that it matches part of 01212222567 for example.

    How would i go about doing this?

    Here is the code im using at the moment.

    Code:
    else if (cli==1)
    	{
    		int item_txt_cli = GetDlgItemText(u_hDlg, IDC_CLI, cCli, 20);
    		
    		if (strcmp(utDtiLine[viLine].sANI, cCli) == 0)
    		{
    				sprintf(sEventMessage, " %-3.3d Drop call %s %s  Cause %d", viLine, utDtiLine[viLine].sANI,  utDtiLine[viLine].sDNI, viCause);
    				vLog(sEventMessage);
    		}
    
    		else
    		{
    			//do nothing
    		}
    	}

    As you can see im using strcmp.

    Thanks for anyhelp whatsoever,

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    29
    Thanks,

    ermm, maybe i should of explaned more.

    The utDtiLine[viLine].sANI char (the char which i want to compare the data in the text box to) is changed extremely often, every second say. It only stores one whole number at a time eg, 01212222567 (or 11 chars!)
    and there is know way of knowing what this number is going to be!

    So, any ideas??!




    cheers,

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    29

    Unhappy

    Can no-one help here??!

    please, cant be that hard! Can it?!

    thanks

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This checks wether SmallString is the same as FullString's beginning (ie: SS = "Hello" & FS = "Hello world!")
    Code:
    bool Compare(char* SmallString, char* FullString)
    {
       int i = 0;
       bool Result = true;
    
       while((SmallString[i] != '\0') && (FullString[i] != '\0') && (Result == true))
       {
          if(SmallString[i] != FullString[i]) Result = false;
          i++;
       }
       return Result;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    yes, thats pretty smart coding magos, but that only finds if SmallString is at the beginning of FullString. If this is a problem, use the strstr() function...

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    29
    thanks guys,

    yeah it would be a problem. ie, i would need it to match say "Worl" with "Hello World" not just the begining.

    So how would this strstr function work exactly within my code?

    Could you give me an example if you dont mind!??

    cheers,
    Last edited by pldd4; 10-30-2002 at 05:16 AM.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    If this line "if (strcmp(utDtiLine[viLine].sANI, cCli) == 0)" compares your two strings for equality, this line "if (strstr(utDtiLine[viLine].sANI, cCli) != NULL) will compare if the first string is part of the second. Look up the strstr function in your helpfile to get more info.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    You could also replace your if statement:
    Code:
    if (strcmp(utDtiLine[viLine].sANI, cCli) == 0)
    with this:
    Code:
    if (strcmp(utDtiLine[viLine].sANI, cCli) > strlen(shorterOne) || strcmp(utDtiLine[viLine].sANI, cCli) == 0)
    to test if the mismatching character is after the shorterOne (which i don't know which one is) or if they are both equal.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  4. bit vs bytes when comparing a file
    By Overworked_PhD in forum C Programming
    Replies: 6
    Last Post: 05-19-2007, 11:22 PM
  5. comparing problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 05:19 AM

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