|
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 ?
|
|
|
|
|
See MSDN article Q138681.
"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
|
|
|
|
|
I'm guessing that Form1 is your main dialog for your application? That being the case, when you click "OK", it ends up exiting the program. So even if you spawn another dialog, the main thread is exiting.
Use a different button, or don't call CDialog::OnOK in your OnOK handler ... or don't use Form1 for your main dialog.
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
|
|
|
|
|
Do you get error?if yes what if no whats happen?
|
|
|
|
|
i made a program using flexgrid ActiveX control and when i run and call a function from the flexgrid class i got a debug assertion failed mesaage
|
|
|
|
|
|
Use your debugger to see where your program crashes.
And if you want more chances to get an answer you have to provide a lot more informations (but still, use your debugger and if you are stuck, THEN post informations that comes from your debugger).
|
|
|
|
|
when calling msflexgrid::clear() for example the program fails in this line
ASSERT(m_pCtrlSite != NULL); in the vs/src/WINOCC.cpp file
|
|
|
|
|