Results 1 to 6 of 6

Thread: [RESOLVED] How to deal with a string array in C++

  1. #1
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,413

    Resolved [RESOLVED] How to deal with a string array in C++

    I've got this code - and I cannot seem to get it to work...

    Code:
    __declspec(dllexport) int firstKeyRegions(char *strSearch, char **keywords, int nKeywords, int *smarkers, int *emarkers) {
    	int i = 0, j = 0, k = 0, a = 0;
    	int keywordlen = 0;
    	int lenSearch = strlen(strSearch);
    
    	bool gotSpace = false; //, inMatch = false, gotMatch = false, doNext = true;
    
    	int wordcnt = 0;
    	int headers = 0, headerse = 0;
    	int titles = 0, titlee = 0;
    
    	for(i = 0; i < lenSearch; i++)						// Step through the whole text
    	{
    		j = strSearch[i];
    		gotSpace = (j == 32);
    		if(gotSpace) {
    			wordcnt += 1;
    		}
    	}
    
    	//string kw;
    
    	for(k = 0; k < nKeywords; k++)						// Step through the keywords array
    	{
    		if (keywords[k] == "wordcnt") 
    		{ 
    			smarkers[k] = wordcnt;
    		}
    	//{
    	//	switch("xxx")
    	//	{
    	//		case 'wordcnt':
    	//			smarkers[k] = wordcnt;
    	//			break;
    	//		case 'header':
    	//			if (headers != 0)
    	//			{
    	//				smarkers[k] = headers;
    	//				emarkers[k] = hearere;
    	//			}
    	//			break;
    	//		case 'title':
    	//			if (titles != 0)
    	//			{
    	//				smarkers[k] = titles;
    	//				emarkers[k] = titlee;
    	//			}
    	//			break;
    	//	}
    	}
    	return -1;											// Return something
    }
    and my keywords array looks like this

    Code:
    ?keywords[0]
    0x0668e620 "wordcnt"
    ?keywords[1]
    0x066849e8 "header"
    ?keywords[2]
    0x0668e698 "title"
    How do I work with this in C++???

    This is called from VB like this

    Code:
    Dim rKeyTerms As String() = {"wordcnt", "header", "title"}
    Dim kcValue As Integer = rKeyTerms.Count
    Dim rKeyStart(kcValue - 1) As Integer
    Dim rKeyEnd(kcValue - 1) As Integer
    For i As Integer = 0 To kcValue - 1
        rKeyStart(i) = -1
        rKeyEnd(i) = -1
    Next
    
    Dim x As Object = firstKeyRegions(allText, rKeyTerms, kcValue, rKeyStart, rKeyEnd)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,413

    Re: How to deal with a string array in C++

    btw - this is the IF statement that won't work

    if (keywords[k] == "wordcnt")


    How do I look at keywords[] and see if the string is "wordcnt"?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,413

    Re: How to deal with a string array in C++

    Ok - I think I know how to attack this - just wanted to post it here to make sure I was not re-inventing the wheel.

    I believe I need to write a function that will pass in the reference to the array slot - which is really just the first character of the string - "w", for instance with "wordcnt" in the keywords[] array...

    Code:
    ?keywords[0]
    0x0668e620 "wordcnt"
    ?keywords[1]
    0x066849e8 "header"
    ?keywords[2]
    0x0668e698 "title"
    The second argument for that function will be the "string" to match on - although I have no idea how to even make a function in C++ let alone pass in "wordcnt" or "xyz" as the second argument...

    Then once in that function it's a silly loop to see if the reference passed in matches the "string" passed as the second argument.

    Do I really need to write my own function to do such a task in C++.

    If so - any help on how to code up the syntax of the function and the call itself?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,413

    Re: How to deal with a string array in C++

    I wrote this function

    Code:
    	bool isValue(char *strLocation, const char *checkValue) {
    		int lenLocation = strlen(strLocation);
    		int lenValue = strlen(checkValue);
    		int i = 0;
    		if (lenLocation != lenValue) {return false;}
    		for(i = 0; i <= lenLocation; i++)
    		{
    			if (strLocation[i] != checkValue[i]) {return false;}			
    		}
    		return true;
    	}
    and I tested it with a call like this - in the debugger...

    Code:
    gotValue = isValue(keywords[0], "wordcnt");
    Seems to work - I stepped through it and all looked fine to me.

    Is this the correct way to accomplish something like this?

    Now I can loop through the keywords[] array and I guess put up a SWITCH TRUE statement and CASE each IsValue constant...

    Is there anyone on this C++ forum??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 05
    Location
    Sweden
    Posts
    8,013

    Re: How to deal with a string array in C++

    Yeah comparing the character arrays like this:
    Code:
    if (keywords[k] == "wordcnt")
    {
    }
    will only compare the memory addresses of the character arrays and not the contents of the arrays.

    There are plenty of ways to do it really. You could construct STL strings from your char arrays and use its equality operator like so:
    Code:
    if(std::string(keywords[k]) == "wordcnt")
    {
    }
    but personally I find that it is a bit overkill to create std::string instances just for string comparison. Unless you find other needs to be using std::strings in your code I'd go for strcmp:
    Code:
    if(strcmp(keywords[k], "wordcnt") == 0)
    {
        // strings are equal
    }
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Check out the work in progress: Vortex - C++ 3D Game engine for windows and linux - (Development blog - Leave a comment!)

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,413

    Re: How to deal with a string array in C++

    Thank you - I did not know about "strcmp" - very new to all this c++ stuff!

    I guess I can do away with my isValue function! I was proud of it for a short while
    Last edited by szlamany; Jul 28th, 2012 at 07:09 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •