I need to tabulate a word count from an external file using a dynamic array of pointers to structures.
I can compile fine, but it screws up in my add function, during this line:
strcpy((*nptr)[*t].new_word, *until_space);
(*nptr)[*t].count = 1;
Here is my full program for reference:
Thank you in advance for the help.Code:#include <iostream> #include <iomanip> #include <fstream> #include <ctype.h> using namespace std; struct Word { char new_word[50]; int count; }; #define MAX_FILENAME 255 void program(); void run(char in_file[MAX_FILENAME], char out_file[MAX_FILENAME]); void check(Word *** ptr, char ** until_space, int t, int *d); void add(Word *** ptr, char ** until_space, int *t); void main() { program(); } void program() { char in_file[MAX_FILENAME]; char out_file[MAX_FILENAME]; cout << "Enter the name of the input file ->"; cin.ignore(cin.rdbuf() -> in_avail()); cin.getline(in_file, 256); cout << "Enter the name of the output file ->"; cin.ignore(cin.rdbuf() -> in_avail()); cin.getline(out_file, 256); run(in_file, out_file); } void run(char in_file[MAX_FILENAME], char out_file[MAX_FILENAME]) { Word ** ptr = new Word*[]; int t(0); // Total unique words int d; // Descision char * until_space = new char[256]; ifstream fin(in_file); ofstream fout(out_file); if(fin.fail() || fout.fail()) cout << "Error opening files" << endl; else { do { d = 0; //READ IN NEXT WORD fin >> until_space; check(&ptr, &until_space, t, &d); if(d == 0) add(&ptr, &until_space, &t); }while(!fin.eof()); for(int l(0); l < t; l++) fout << ptr[l]->count << " " << ptr[l]->new_word << endl; fin.close(); fout.close(); } delete [] ptr; } void check(Word *** ptr, char ** until_space, int t, int *d) { int f(0); // Found douplicate int s; // Sting length s = strlen(*until_space); //TAKE ANY PUNCTUATION OFF for(int p(0); p < s; p++) if( ispunct( (*until_space)[p] ) ) (*until_space)[p] = '\0'; s = strlen(*until_space); //CONVERT TO LOWER CASE for(int l(0); l < s; l++) (*until_space)[l] = tolower( (*until_space)[l] ); if( isdigit( (*until_space)[0] )) ; else { //COMPARE AGAINST ALREADY HAVE. ADD 1 IF DUPLICATE for(int i(0); i < t; i++) { if(strcmp (*until_space, (*ptr)[i]->new_word) == 0) { (*ptr)[i]->count += 1; *d = 1; } } } } void add(Word *** ptr, char ** until_space, int *t) { Word ** nptr = new Word*[*t + 1]; for(int n(0); n < *t; n++) { strcpy((*nptr)[n].new_word, (*ptr)[n]->new_word); (*nptr)[n].count = (*ptr)[n]->count; } ::GLITCHES ON THIS NEXT LINE:: strcpy((*nptr)[*t].new_word, *until_space); (*nptr)[*t].count = 1; delete *ptr; *ptr = new Word*[*t + 1]; for(int c(0); c < *t + 1; c++) { strcpy((*ptr)[c]->new_word, (*nptr)[c].new_word); (*ptr)[c]->count = (*nptr)[c].count; } *t++; delete nptr; }
P.S. If there is an other glitches that you can see, please let me know![]()



LinkBack URL
About LinkBacks



