A 99% question

This is a discussion on A 99% question within the C Programming forums, part of the General Programming Boards category; One of the people on this board has in their signature - Programming is 99% logic & 1% syntax . ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    A 99% question

    One of the people on this board has in their signature - Programming is 99% logic & 1% syntax . (how true) So this is about that 99%.....



    I'm trying to compare an input with each of 25 particular fields in a database record, then ....


    if it matches any of the 25 fields -> call function(a)



    if it matches none of the 25 fields ->call function(b)



    What I've come up with so far is to create a loop reading each of the 25 fields and doing a strcmp with the field and the input. If it gets a match ->break then call function(a). This part works fine.



    Where I'm stuck is how to call function(b) ONLY if ALL of the 25 loops fail to find a match. Since strcmp returns 0 for a match and either a + or- value for a "not match" I can't think of any way of using any kind of "sum". And if I use a simple "if ! O" to call function(b) then I get function(b) called for numerous times for every round of the loop that doesn't match.



    It might be that I'm using a completely flawed logic on this - I'm still pretty new to this programming stuff.



    Any suggestions are appreciated,



    Regis

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are various ways.. post a bit of your code first though, so we can see what answer will suit you best....

    In the meantime, here's some pseudo code (two different versions)

    Code:
    DoWeHaveIt = InTable(string) /* seperate function, source not shown */
    if (DoWeHaveIt = true)
    	Call FunctionA
    Else
    	Call FunctionB
    
    ------------------------------------------
    
    foundit = false
    for i = 0 to 24
    	if string = array[i] foundit = true
    next
    if foundit = true
    	Call FunctionA
    Else
    	Call FunctionB
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    Ok, here's some code - sorry it's in kind of scratch pad form I'll try to comment what I'm doing. In summary I'm querying table 1 for a key stored in a particular field. Then I'm querying table 2 with the key found in the first query and testing it against each of 25 fields labeled s1 through s25 using a loop and strcmp.





    When this code gets run, it calls function b for each "non match" then calls function a when it finds a match and breaks. But I need to call either "a" if ANY MATCH - or "b" if NONE MATCH - not both. So - I've painted myself into a corner.





    All comments appreciated.





    Code:
    /*
    
    
    int chk_sender(char *Uname, char *Udomain, char *Sname, char *msgID, char *subj)
    
    
    
    {
    
    
    
    	MYSQL_RES *result, *result2;
    
    
    
    	MYSQL_ROW row;
    
    
    
    	MYSQL *connection, mysql;
    
    
    
    	int state, state2, result3;
    
    
    
    	int match;
    
    
    
    	char sql_qry[600];
    
    
    
    	char sql_qry2[600];
    
    
    
    	char trial[40];
    
    
    
    	char comp[40];
    
    
    
    	int numb;
    
    
    
    
    
    
    
    	numb = 1;
    
    
    
    
    
    
    
    	mysql_init(&mysql);
    
    
    
    	connection = mysql_real_connect(&mysql, "localhost","root", "", "db1", 0, NULL, 0);
    
    
    
    						       	
    
    
    
    	if (connection == NULL)
    
    
    
    	{
    
    
    
    	printf("mysql_error(&mysql)\n");
    
    
    
    	return 1;}
    
    
    
    //query database for specific field entry which is a foreign key for the second //table that  is queried
    
    	sprintf(sql_qry,"SELECT ap_senders FROM table1WHERE pw_name='%s' and pw_domain='%s'",Uname, Udomain);
    
    
    
    	state = mysql_query(connection, sql_qry);
    
    
    
    	if (state != 0){
    
    
    
    		printf(mysql_error(connection));
    
    
    
    		return 1;}	
    
    
    
    	//now to call mysql store result to store the value in memory
    
    
    
    	result = mysql_store_result(connection);
    
    
    
    
    
    //add print statement just to verify that it found something
    
    	printf("Rows: %d\n", mysql_num_rows(result));
    
    
    
    
    
    //now create a string with the field result -  set it equal to "trial"
    
    	while ((row=mysql_fetch_row(result)) != NULL)
    
    
    
    		sprintf(trial, "%s", *row);
    
    
    
    	
    
    
    
    	mysql_free_result(result);		
    
    
    
    //now that I have the variable; search for a match
    
    
    
    	
    
    
    
    while(numb<=25)
    
    
    
    {
    
    //query the second table using the key found in the first query which was 
    
    //called "trial"
    
     	
    
    
    
    sprintf(sql_qry2,"SELECT s%i FROM table2WHERE recipient_ID='%s'", numb, trial);
    
    
    
    	state2 = mysql_query(connection, sql_qry2);
    
    
    
           	if (state2 != 0){
    
    
    
    	printf(mysql_error(connection));
    
    
    
    	return 1;}
    
    
    
    	result2 = mysql_store_result(connection);
    
    
    
    	while ((row=mysql_fetch_row(result2)) != NULL)
    
    
    
    	sprintf(comp, "%s",*row);
    
    
    
    
    
    
    
    	result3 = strcmp(comp,Sname);
    
    
    
    	if(result3==0)
    
    
    
    		{
    
    
    
    		printf("Call function A here\n");
    
    
    
    
    
    
    			break;
    
    
    
    		}
    
    
    
    	
    
    
    		else
    
    
    
    		{
    
    
    
    		printf("Call function B here\n");
    
    
    
    		}		
    
    
    
    		numb=numb+1;
    
    
    
    
    
    
    }
    
    
    
    
    
    
      	 //now to free the result set
    
    
    
    	//	mysql_free_result(result);
    
    
    
    
    
    								                                 //and then to close the connection
    
    
    
    		mysql_close(connection);
    
    
    
    		printf("Done\n");
    
    
    
    }
    
    
    
    */
    (sorry about all the white space in the code - my editor an this text box don't seem to agree.)

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I prefer functions that return "natural" booleans, and strcmp doesn't, so here's a "wrapper" function for it...

    Code:
    int Identical(char *a, char *b)
    {
       if( strcmp(a, b) == 0)
         return 1;
          
       return 0;   //...no need for else statement - this will execute if above doesn't
    }




    Code:
       
                 int found = 0; //...declare globally..set to false...
    
    
    //...later, in your while loop...
    
                 //...BTW: I replaced "result3" with "identical"...see why?...
                  
                identical = Identical(comp,Sname);
    
                              if( identical )
    		{
    		func_a();
                                    
                                    found = 1;
     
    		break;
    		}
    
    
        numb++; //...same as numb = numb+1;
    
    } //...end of while loop...
    
    
    
    
    if( !found )    //...same as if(found == 0)
     
    func_b();
     	 //now to free the result set
    
    		
    mysql_free_result(result);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    if     (!strcmp(field1, val1) && !strcmp(field2, val2) && !strcmp(field3, val3))        /*   25 strcmp()'s   */
            function_a();                  /*   ATLEAST ONE MATCH   */
    else if(!strcmp(field1, val1) || !strcmp(field2, val2) || !strcmp(field3, val3))        /*   25 strcmp()'s   */
            function_a();                  /*   ATLEAST ONE MATCH   */
    else
            function_b();                  /*   NONE MATCH          */

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always just set a variable each time there is a match. Something similar to a case statement inside a loop:

    Code:
    while( !done_reading )
    {
        read_into_buf( buf );
        switch( buf[0] )
        {
            case 'A':
                if( !strcmp( buf, "Apple" ) )
                {
                    foundApple = 1;
                    ...do something...
                }
                if( !strcmp( buf, "Ape" ) )
                {
                    foundApe = 1;
                    ...do something...
                }
                ...more stuff...
            break;
            case 'B':
                if( !strcmp( buf, "Bow" ) )
                {
                    foundBow = 1;
                    ...do something...
                }
                ...more stuff...
            break;
            ....more stuff...
        }
    }
    if( !foundApple
    &&  !foundApe
    &&  !foundBow
    &&  !found... )
    {
        functionB( );
    }
    Now you may not need the actual switch, you may just want a series of if checks (like what I did inside the switch). But in either case, it should do the trick.

    Enjoy.


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

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    Excellent!!!!

    Wow, this is outstanding.



    You folks make this look so easy. I'm ashamed to tell you how many hours I've got into this little function.



    It looks to me like all three methods would work, but I decided to use the suggestion by Sabastiani. It used my same "loop logic" but came up with the brilliant idea of creating a wrrapper for the strcmp function so it would return a natural boolean. What a great idea!



    Jeez, this is what's so cool about this C language. If the functions don't work like you want them - just re do them, or "wrap" them.



    Thanks a million!



    Regis

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >while ((row=mysql_fetch_row(result2)) != NULL)
    It also looks like you're missing a bracket here.
    Otherwise all it executes in the while-loop is the sprintf().
    {
    >
    >	sprintf(comp, "%s",*row);
    >
    >	result3 = strcmp(comp,Sname);
    >
    >	if(result3==0)

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Excellent!!!!

    Originally posted by crescen7
    It used my same "loop logic" but came up with the brilliant idea of creating a wrrapper for the strcmp function so it would return a natural boolean. What a great idea!
    Um... strcmp already does return a natural boolean:

    if( !strcmp( a, b ) ) match
    else nomatch

    If you need it to have a "true" value on correct:

    if( !!strcmp( a, b ) ) match

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

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    natural boolean?

    Quzah,

    You're obviously better at this than I am, but just for clarification....

    I thought, according to somwwhere in this stack of books, that strcmp returned 0 on a match and either a positive of negative value on a "no match".

    Am I missing something?

    regis

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: natural boolean?

    Originally posted by crescen7
    Quzah,

    You're obviously better at this than I am, but just for clarification....

    I thought, according to somwwhere in this stack of books, that strcmp returned 0 on a match and either a positive of negative value on a "no match".

    Am I missing something?

    regis
    No, that's correct:

    0 = match
    1 or -1 depending on what string comes first alphabeticly.

    By natural boolean you mean:

    1 = true
    0 = false

    Correct? I guess I just don't see the point in making a wrapper function for strcmp.

    #define isMatch !strcmp

    if( isMatch( "Apple", "Apple" ) ) printf("We have a match.\n");

    There ya go. No function needed.

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

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, you got it. That's how strcmp works. It also aids quite well in sorting strings - the true use of strcmp is just that. The matching bit is simply a natural side-effect.

    Quzah is what we call here at cprogramming.com "a guru". In other words, he considers statements like "if(!!foo()^sqrt(foo()))" to be quite natural, boolean expressions!

    Seriously, though, he has a point. And the truth is, working a *real* software project you will have to get use to such idioms. Now myself, I am my sole collaborator and as such have the priviledge to code as I please.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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