|
Look for SHBrowseForFolder ... I believe there is a decent article on it in CP ... and Google finds some others as well.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
|
I'm trying to code a function which returns the number of words in an array, strSentence[100, 1000].
Algorithm:
1. return the first occurrence of strWord.
2. Remove strWord from strSentence.
3. Repeat the above steps until (1) returns no value.
I am using strstr to return the first occurrence of strWord within strSentence. However, the following error is being displayed:
SentenceAr.cpp(94): error C2664: 'strstr' : cannot convert parameter 1 from 'char [100][1000]' to 'const char *'
Code:
int CSentenceAr::HowManyWords(const char strWord[MAX_WORD_LENGTH])
{ char *strCurrentSentence;
ZeroMemory(strCurrentSentence,sizeof(strCurrentSentence));
char *ptrPosition;
for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{ strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrPosition = strstr(strSentence, strWord);
}
Any work around for this, please?
Jon
|
|
|
|
|
strstr doesn't know about arrays of arrays of chars, it only knows about arrays of chars.
so... ptrPosition = strstr(strSentence[some_index], strWord);
|
|
|
|
|
jon_80 wrote: ZeroMemory(strCurrentSentence,sizeof(strCurrentSentence));
...
strcpy(strCurrentSentence, strSentence[iCurrent]);
These two statements look suspect as strCurrentSentence does not point to a valid memory address.
jon_80 wrote: ptrPosition = strstr(strSentence, strWord);
This should probably be:
ptrPosition = strstr(strCurrentSentence, strWord);
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
CSentenceAr.h
int HowManyWords(const char strWord[MAX_WORD_LENGTH]);
CSentenceAr.cpp
int CSentenceAr::HowManyWords(const char strWord[MAX_WORD_LENGTH])
{ char strCurrentSentence;
ZeroMemory(strCurrentSentence,sizeof(strCurrentSentence));
char ptrPosition;
for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{ strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrPosition = strstr(strSentence, strWord);
}
}
Now I'm getting more errors:
SentenceAr.cpp(89): error C2664: 'memset' : cannot convert parameter 1 from 'char' to 'void *'
SentenceAr.cpp(93): error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
SentenceAr.cpp(94): error C2664: 'strstr' : cannot convert parameter 1 from 'char [100][1000]' to 'const char *'
Jon
|
|
|
|
|
jon_80 wrote: ZeroMemory(strCurrentSentence,sizeof(strCurrentSentence));
This is not necessary (if strcpy() is to be used later on).
jon_80 wrote: SentenceAr.cpp(93): error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
The compiler is telling you exactly what the problem is. Parameter 1 needs to be a char* , but strCurrentSentence is just a char .
jon_80 wrote: SentenceAr.cpp(94): error C2664: 'strstr' : cannot convert parameter 1 from 'char [100][1000]' to 'const char *'
Parameter 1 cannot be a 2D array. Chris showed you a solution. Why did you not use it?
|
|
|
|
|
The program uses a fixed array, so I need to work around it. Sorry, but I'm a newbie.
Now I'm getting: SentenceAr.cpp(100): error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{ //retrieve line of text
strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrCurrentSentence = &strCurrentSentence;
while (bFound == true)
{ ptrPosition = strstr(ptrCurrentSentence, strWord);
if (ptrPosition != NULL)
{ iResult++;
}
else if (ptrPosition = NULL)
{bFound = false;}
}
}
return 0;
}
Jon
|
|
|
|
|
jon_80 wrote: Now I'm getting: SentenceAr.cpp(100): error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
strCurrentSentence needs to be at least as large as strSentence[iCurrent] .
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Try this instead:
int CSentenceAr::HowManyWords(const char strWord[MAX_WORD_LENGTH])
{
const char* delimiters = " ,.!?;:\t\r\n()";
char buffer[MAX_WORD_LENGTH] = {0};
memcpy(buffer, strWord, MAX_WORD_LENGTH);
char* token = 0;
unsigned long wordCount = 0;
token = strtok(buffer, delimiters);
while (token != 0)
{
if (strlen(token) > 0)
{
++wordCount;
}
token = strtok(0, delimiters);
}
return (int)wordCount;
}
-- modified at 16:22 Wednesday 26th July, 2006
|
|
|
|
|
I'm not sure, but after following through the code, the code checks whether the word we are looking for is greater than length zero, hence always returning a value of at least 1.
What I was trying to do was to check for how many times $searchword appears within an array of an array of chars (simply put an array of strings) $sentences.
Jon
|
|
|
|
|
My apologies, the algorithm I posted will count the number of words in a character array. To find the number of times a substring appears in the array, you need to modify the algorithm to use strstr instead of strtok:
unsigned long HowManyWords(const char strWord[MAX_WORD_LENGTH], const char* substring)
{
char buffer[MAX_WORD_LENGTH] = {0};
memcpy(buffer, strWord, MAX_WORD_LENGTH);
char* token = 0;
unsigned long wordCount = 0;
token = strstr(buffer, substring);
while (token != 0 && *token != 0)
{
if (strlen(token) > 0)
{
++wordCount;
token += strlen(substring);
if (token >= buffer + MAX_WORD_LENGTH)
{
break;
}
}
token = strstr(token, substring);
}
return (int)wordCount;
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks.. I have updated the code accordingly, adding also a memcpy so that memcpy is used to copy the variables to local variables. I assume that this is a workaround for strstr (right?).
However, token seems to have a value of 0xcccccccc <Bad Ptr> when debugging, so the program never really checks.
Code:
int CSentenceAr::HowManyWords(const char strWord[MAX_WORD_LENGTH])
{
// initialize local variables
char strSearch[MAX_WORD_LENGTH] = {0};
memcpy(strSearch, strWord, MAX_WORD_LENGTH); //copies fixed array to strSearch
char* ptrToken = 0;
char strSentence[MAX_SENTENCE_LENGTH] = {0};
int iWordCount = 0;
for (int iCurrent = 0; iCurrent <= iNumberOfLines; iNumberOfLines++)
{
ptrToken = strstr(strSentence, strSearch);
while (ptrToken != 0 && *ptrToken != 0)
{ memcpy(strSentence, this->strSentence[iCurrent], MAX_SENTENCE_LENGTH);
if (strlen(ptrToken) > 0)
{ ++iWordCount;
ptrToken += strlen(strWord);
if (ptrToken >= strSentence + MAX_WORD_LENGTH)
{break;}
}
ptrToken = strstr(ptrToken, strSearch);
}
}
return (int) iWordCount;
}
Jon
|
|
|
|
|
jon_80 wrote: for (int iCurrent = 0; iCurrent <= iNumberOfLines; iNumberOfLines++)
{
ptrToken = strstr(strSentence, strSearch);
while (ptrToken != 0 && *ptrToken != 0)
{ memcpy(strSentence, this->strSentence[iCurrent], MAX_SENTENCE_LENGTH);
if (strlen(ptrToken) > 0)
{ ++iWordCount;
ptrToken += strlen(strWord);
if (ptrToken >= strSentence + MAX_WORD_LENGTH)
{break;}
}
ptrToken = strstr(ptrToken, strSearch);
}
}
token = strstr(buffer, substring);
while (token != 0 && *token != 0)
{
if (strlen(token) > 0)
{
++wordCount;
token += strlen(substring);
if (token >= buffer + MAX_WORD_LENGTH)
{
break;
}
}
token = strstr(token, substring);
}
Your loop and mine are not the same, which is why you are having problems with it. Where is iNumberOfLines declared and initialized? Your outter for loop is still incrementing this value but checking it against the iterating value that doesn't change. After a while, you will find that token is invalid because you will have gone off into "never-never land" (part of memory that you shouldn't be in).
The function I posted will take any single character array and tell you how often a substring appears in it. If you want to do this for multiple lines, you would simple call that function in a loop:
char SomeWords[20][100] = { };
char SearchString[10] = { };
for (int i = 0; i < 20; ++i)
{
unsigned long count = HowManyWords(SomeWords[i], SearchString);
cout << "Found substring (" << SearchString << ") " << count
<< " times in string(" << i << ")." << endl;
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I see... I was reading the string incorrectly.
Jon
|
|
|
|
|
I'm sorry I misunderstood the scope of the function.
Jon
|
|
|
|
|
Hi again,
I'm not sure if I'm repeating myself (sorry), but, strToken is returning a bad value:
+ strToken 0xcccccccc <Bad Ptr> char *
Code:
unsigned int CSentenceAr::search(const char *strWord)
{
// initialize local variables
unsigned long iWordCount = 0;
char buffer[MAX_WORD_LENGTH] = {0};
/* Algorithm:
strToken shall initially contain the sentence string.
When strWord is found from the string, this is removed
from the string so that this function checks the rest of the
string for other instances of the word.
*/
char *strToken = 0;
for (int iCurrent = 0; iCurrent < Size; iCurrent++)
// copy to buffer, used as parameter for strstr.
{ memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH);
strToken = strstr(buffer,strWord);
while (strToken !=0 && *strToken != 0)
{
if (strlen(strToken) > 0)
{
iWordCount++;
strToken += strlen(strWord);
}
strToken = strstr(strToken, strWord);
}
}
return (int) iWordCount;
}
Jon
|
|
|
|
|
jon_80 wrote: char *strCurrentSentence;
ZeroMemory(strCurrentSentence,sizeof(strCurrentSentence));
char *ptrPosition;
These statements are unnecessary. You could have just declared your pointers as follows:
char *strCurrentSentence = NULL;<br />
char *ptrPosition = NULL;
Calling ZeroMemory on a pointer like this does nothing but waste CPU cycles by jumping to the function in memory.
jon_80 wrote: for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{ strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrPosition = strstr(strSentence, strWord);
}
for (int iCurrent = 1; iCurrent <= iNumberOfLines; iNumberOfLines++)
{
strcpy(strCurrentSentence, strSentence[iCurrent]);
ptrPosition = strstr(strSentence, strWord);
}
There are a several problems here:
1) Where is iNumberOfLines declared and initialized?
2) Doing a loop on an increasing number of iterations (that is, the number of iterations changes as the loop continues) is asking for trouble.
3) You cannot use strcpy to copy the string to strCurrentSentence. strCurrentSentence is a pointer to a character that has no memory allocated for it. I believe what you want to do is: strCurrentSentence = &strSentence[iCurrent];
4) iCurrent is never changed, so chances are this will become an infinite loop
Just a recommendation: Unless you have substrings that you are looking for, try making a copy of the string and using strtok to count the words, or simply doing a split on punctuation and whitespace.
A simple algorithm for the last part would be something like:
1) Split input string on ' ', ',', '.', '!', '?', ';', ':' and store resulting words in a list or vector
2) Call .size() on the list or vector to get word count.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hi all,
Could anybody help me in this please:
I should write some data received from a mobile deice to a MS Access database for further manipulation, and as I am new in programming, I do not know what to do! Could you please let me know how I should go for this issue and what to do?
Appreciates any suggestions.
Nahitan
|
|
|
|
|
|
|
Have you looked here?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: Have you looked
Rhetorical question?
|
|
|
|
|
I got this from a computer science class and it's supposed to create a file for me. I copied it directly, and it doesn't seem to work as nothing shows up in the file. What's wrong? Thanks
#include <fstream.h>
#include<iostream.h>
struct rec
{
char name[25];
int age;
float wt;
};
int main()
{
rec stdrec[8] = {{"helen", 10, 10}, {"julie", 20,20}, {"lena",30,30},{"alan", 40, 40}, {"annie", 50,50},
{"may", 60, 60}, {"lee", 70, 70}, {"ed", 80,80}};
fstream fout("final.dat", ios::out||ios::binary);
int i;
for(i=0; i<8; i++)
{
fout.write((char*)&stdrec[i], sizeof(stdrec[i]));
cout<
|
|
|
|
|