|
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<
|
|
|
|
|
Fixed line in bold:
#include <iostream>
#include <fstream>
using namespace std;
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);
for(int i=0; i<8; i++)
{
fout.write((char*)&stdrec[i], sizeof(stdrec[i]));
cout << stdrec[i].name << "\t"
<< stdrec[i].age << "\t"
<< stdrec[i].wt <<endl;
}
return 0;
}
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 tried to run it, but the file output is weird. Is this because it's in binary mode? Sorry I'm still new to programming.
|
|
|
|
|
liuphil1 wrote: ...the file output is weird.
Define "weird."
"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
|
|
|
|
|
It's all on one line, and there's a lot of strange characters that were not in the initializations.
|
|
|
|
|
Its a binary file. Reading it in Notepad, Wordpad, or any other text editor will show you lots of weird characters.
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
|
|
|
|
|
You won't be able to read the file. It is in binary. If you want to look at it, open it up in a hex editor to verify its contents. Otherwise, write a method to read in the data in a similar fashion to the way it is written and then print it to the screen:
fstream fin("final.dat", ios::in | ios::binary);
while (!fin.eof())
{
rec r;
fin.read((char*)&r, sizeof(rec));
cout << r.name << "\t"
<< r.age << "\t"
<< r.wt <<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 create a dialog based application
it consists of two from class
form1 and form2
i can display form1 like this
CDialog form1(IDD_FORM1)
form1.DoModal();
if i try to display form2 from
form1 by OnOK()method like this
CDialog form2(IDD_FORM2)
form2.DoModal();
IT IS NOT WORKING
HOW CAN I DO IT
janan
|
|
|
|
|
ningthemcha wrote: IT IS NOT WORKING
what happens ?
can you post the relevant parts of your code ?
|
|
|
|
|