strcmp problems... pointer 2 private pointer

LOTS of source to look at here, basically it does everything I want it to but properly compare the values of both pointers and upon looking at their values in the debugger, when they match, its not registering as true and still defining another word for the array. RUNDOWN : This program takes a text file, and strips words out and keeps a count of how many times they were used, adds to the list if it cannot find it in the array.

*** The piece that isn't working as I would like it to, is in the Statistics.cpp file, the section for strcmp, even comparing as an if statement it does not return true under true conditions ***


==============================
MAIN.CPP
==============================

#include "Reader.h"
#include "Statistics.h"

#include <iostream>
using namespace std;



int main(int argc, char *argv[])
{
char wordPtr[200] = {0}; // Value to swap around the current word
int statCount = 0; // Counter for stats entered into table

Reader myReader; // Instantiate object myReader
Statistics myStats;

// If the number of arguments passed into the command is NOT equal to two return argument instructions
if ( argc != 2 )
{
cout << "Usage: \"Lab 13.exe\" FILENAME.EXTENTION (E.G. \"Lab 13.exe\" hamlet.txt" << endl;
return -1;
}

// File Initialization call, if fails ends program
if (myReader.initialize(argv[1]) == false)
return -1;

// Continues to loop until the end of the file is reached
while (myReader.getNextWord(wordPtr) == true)
{
myStats.addWord(wordPtr);
}


myStats.printWordCount();

return 0;
}


=============
Reader.h
=============
#include <fstream>
using namespace std;

class Reader
{
public :
bool initialize(char *filename); // Checks to see if filename is set
bool getNextWord(char * nextWord); // Gets the next word

private :
ifstream f_handle;

}; // end initialization

==================
Statistics.h
==================

class Statistics
{
public :
Statistics();
void addWord(char *word);
void printWordCount();

private :
char *m_word[500];
int m_wordCount[500];
int m_counter;

}; // end initialization


================
Reader.cpp
================
#include "Reader.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

bool Reader::getNextWord(char *nextWord)
{
char *buffer = ""; // Generate a buffer
char getWord[200] = ""; // Generate a char array for string operations
f_handle >> nextWord; // Get next word from ifstream
buffer = strtok ( nextWord, " .,';:()-!?" ); // Assign the buffer the value of next token

// Loop while tokens are being returned
while (buffer !=NULL)
{
strcat( getWord, buffer ); // Concatanate buffer into getWord
buffer = strtok ( NULL, " .,';:()-!?" ); // Get next token value
}
strcpy( nextWord, getWord ); // Copy new string into nextWord

// Check to see if the file is still active
if ( !f_handle )
return false;
else
return true;
}
bool Reader::initialize(char *filename)
{
bool checkFile;

// Load the file into memory to check
f_handle.open(filename , ios::in);

if (!f_handle)
{
cout << filename << " could not be opened" << endl;
checkFile = false;
}
else
checkFile = true;

return checkFile;
};


=====================
Statistics.cpp
=====================
#include "Statistics.h"

#include <string>
#include <iomanip>
#include <iostream>
using namespace std;

Statistics::Statistics()
{
// Initialize zero values for word count
for (int x = 0; x < 50; ++x)
{
m_wordCount[x] = 0;
m_word[x] = "";
}

m_counter = 0;
}


void Statistics::addWord(char *word)
{
bool found = false;

for (int x = 0; x < m_counter; ++x)
{

int temp = strcmp(word, m_word[x]);

if ( strcmp (word, m_word[x] ) == 0)
{
bool found = true;
++m_wordCount[x];
}

}

if (found == false)
{
m_word[m_counter] = new char[50];
strcpy ( m_word[m_counter], word );
m_wordCount[m_counter] = 1;
++m_counter;
}

}


void Statistics::printWordCount()
{
cout << setw(10) << "Word" << setw(10) << "Uses" << endl;

for (int x = 0; x < m_counter; ++x)
{
cout << setw(14) << m_word[x] << setw(14) << m_wordCount[x] << endl;
}
};

=======================

Yes I do realize I seem to be making the allocation of the statistics rather rough, however its an outline for me to use if I'm unable to impliment use through an object. However the definition of the functions must stay as is and the only rule is that I can add functions at will, but have to use the ones the way they were predefined in the headers :\.... any tips would be greatly appreciated too, however I'd like to understand how come I cant properly compare the two strings and get an output of 1 value for every single word seperated...
Last edited on
I think your problem is in this section of code in addWord:

1
2
3
4
5
if( strcmp( word, m_word[x] ) == 0 )
{
   bool found = true;   // you're redeclaring found here... don't think you want to
   ++m_wordCount[x];
}


Topic archived. No new replies allowed.