Click here to Skip to main content
15,916,842 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Visual Studio Net 2003 does not catch divide by zero with doubles Pin
Joe Woodbury4-Aug-06 16:42
professionalJoe Woodbury4-Aug-06 16:42 
Questionbad pointer Pin
jon-804-Aug-06 6:15
professionaljon-804-Aug-06 6:15 
QuestionRe: bad pointer Pin
David Crow4-Aug-06 6:32
David Crow4-Aug-06 6:32 
AnswerRe: bad pointer Pin
jon-804-Aug-06 6:53
professionaljon-804-Aug-06 6:53 
GeneralRe: bad pointer Pin
David Crow4-Aug-06 7:16
David Crow4-Aug-06 7:16 
GeneralRe: bad pointer Pin
jon-804-Aug-06 9:34
professionaljon-804-Aug-06 9:34 
QuestionRe: bad pointer Pin
David Crow4-Aug-06 9:40
David Crow4-Aug-06 9:40 
AnswerRe: bad pointer [modified] Pin
Zac Howland4-Aug-06 6:57
Zac Howland4-Aug-06 6:57 
Break up the scope of your function:

Function 1 will take a single sentence and count how many times a substring appears in it.
Function 2 will take a collection of sentences and call Function 1 on each of them and sum the results.

const unsigned long MAX_SENTENCE_LENGTH = 100;

unsigned long HowManyInSentence(const char* sentence, const char* substring)
{
        char buffer[MAX_SENTENCE_LENGTH + 1] = {0};
        memcpy(buffer, sentence, 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 wordCount;
}

unsigned long HowMany(const char* substring)
{
	// assume that g_sentences is declared somewhere and has MAX_SENTENCES entries
	unsigned long count = 0;
	for (int i = 0; i < MAX_SENTENCES; ++i)
	{
		count += HowManInSentence(g_sentences[i], substring);
	}

	return count;
}


Make sure that your buffer is null-terminated otherwise token will end up off in the weeds.

Oh, and token doesn't need to be assigned with scope (that is, you don't need char* token = {0};). char* token = 0; is fine and more readable.
-- modified at 12:58 Friday 4th August, 2006

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

QuestionRe: bad pointer Pin
jon-804-Aug-06 9:51
professionaljon-804-Aug-06 9:51 
AnswerRe: bad pointer Pin
David Crow4-Aug-06 9:56
David Crow4-Aug-06 9:56 
AnswerRe: bad pointer Pin
Zac Howland4-Aug-06 10:05
Zac Howland4-Aug-06 10:05 
AnswerRe: bad pointer Pin
Stober4-Aug-06 8:19
Stober4-Aug-06 8:19 
GeneralRe: bad pointer Pin
jon-804-Aug-06 9:29
professionaljon-804-Aug-06 9:29 
Questionusing search in VC++ 2005 Pro Pin
Stober4-Aug-06 4:39
Stober4-Aug-06 4:39 
AnswerRe: using search in VC++ 2005 Pro Pin
Sarath C4-Aug-06 5:35
Sarath C4-Aug-06 5:35 
GeneralRe: using search in VC++ 2005 Pro Pin
Stober4-Aug-06 6:17
Stober4-Aug-06 6:17 
GeneralRe: using search in VC++ 2005 Pro Pin
Sarath C4-Aug-06 6:53
Sarath C4-Aug-06 6:53 
AnswerRe: using search in VC++ 2005 Pro Pin
Rob Graham4-Aug-06 5:41
Rob Graham4-Aug-06 5:41 
QuestionSocket Error Pin
Daniel Kanev4-Aug-06 4:34
Daniel Kanev4-Aug-06 4:34 
AnswerRe: Socket Error Pin
charlieg4-Aug-06 4:58
charlieg4-Aug-06 4:58 
AnswerRe: Socket Error Pin
Varchas R S5-Aug-06 7:14
Varchas R S5-Aug-06 7:14 
Questioninterpolation routines Pin
9ine4-Aug-06 3:42
9ine4-Aug-06 3:42 
AnswerRe: interpolation routines Pin
Chris Losinger4-Aug-06 4:40
professionalChris Losinger4-Aug-06 4:40 
GeneralRe: interpolation routines [modified] Pin
9ine4-Aug-06 6:01
9ine4-Aug-06 6:01 
Question'mfcs42.lib' could not be found!! Pin
sach!!4-Aug-06 3:04
sach!!4-Aug-06 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.