need help! what's wrong with my code? it compiles but outputs nothing. it requires the user to create a "word.txt" text file for it to start counting the frequency of occurrence of words. i'm a newbie here in daniweb and a novice in C++ programming. i really appreciate any help. thanks!:)
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
#include<cstdlib>
#define MAX_SIZE 1000//maximum length of inputted line
struct BSTNode{
char *word;//pointer to the word that is being stored
int count;//variable used to count the number of times the word encounter
struct BSTNode *left;//points to the left subtree
struct BSTNode *right;//points to the right subtree
};
void print(struct BSTNode *);//function to print the tree
int isNonword(char *p);//function to determine if it is a word or not
struct BSTNode *allocate(char *word){//create a node
struct BSTNode *newNode= new BSTNode;//pointer to new node
newNode->word= word;
newNode->count=1;
newNode->left=NULL;
newNode->right=NULL;
return(newNode);
}
struct BSTNode *insert(struct BSTNode *root1, char *word){//insert a word into
int compare;//binary tree, variable used to get the result from strcmp
if(root1==NULL){
return(allocate(word));
}
if((compare= strcmp(word,root1->word))==0){//if it is equal to zero it means
root1->count++;//the two words are equal
}
else if(compare>0){//if it is less than zero then the word pointed to by
root1->right=insert(root1->right,word);//word is greater than to the word
}//pointed to by root1
else{//if it is less than zero then
root1->left=insert(root1->left,word);//then the word pointed to by
}//word is less than to the word pointedto by root1
return (root1);//return the pointer to the root of the tree
}//end of the function
int main(){//main program
char line[MAX_SIZE];//variable for inputted line
char word[MAX_SIZE];//variable for word
int lines=0;//variable for current line number
char *p;//variable for current line position
char *w;//variable that is used for loading a line
struct BSTNode *root;//the root of the tree
ifstream inWordFile("words.txt",ios::in);//opens a file to read
if(!inWordFile){
cerr<<"File could not be opened,"<<endl;
exit(1);
}
while(inWordFile>>(fgets(line, 100, stdin))){//read the file a line
lines++;//at a time
p=line;
while(*p){//loops until the end of the line
while(*p && (isNonword(p)!=1))//omit leading
p++; //non-word
w= word;//stop at word, then put it into word[]
while(isNonword(p)==0)
*w++= *p++;
*w= '\0';
if(word[0])//inserting it into the tree
root= insert(root, word);
}
}
print(root);//print the word
system("puse");
return 0;//indicate that program exited cleanly
}//end of main program
void print(struct BSTNode *root1){//struct BSTNode *root1 is the root of the tree
if(root1==NULL)//if there is no treee it means nothing to print
return;
print(root1->left);//prints the left subtree
cout<<"\n"<<root1->count<<" "<<root1->word<<endl;
print(root1->right);//prints the right subtree
}//end of print funtion
int isNonword(char *p){//checks if it is a character or functuation mark
if((*p=='.')||(*p==',')) return 1;//returns 1 if it is a functuation mark
else if((*p=='!')||(*p=='?')) return 1;
else return 0;//else return 0 if character/letter
}//end of isNonword function