The problem is that I want it to ignore punctuation when adding strings to the BST. But it seems to take the letter(s) after the punctuation and consider it as another word. Ex It's and Its should be the same word (punc ignored) but the program will see the word It's as the word "it" and the word 's'. Also the last word in the text file is not showing up in the list. Hope that's good enough explanation. Im sure there's just one tiny thing missing from my program to fix this. If you need any more info about the program let me know. Thanks so much
/*
File Name: CBST.cpp
This program will read an input file of text and builds a concordance of the words in the file.
After the file has been read and processed, the program will display the list of words (in alphabetical order)
and how many times the word shows up in the input file. This program will ignore punctuation, capital letters
and whitespace.
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include "BST.h"
usingnamespace std;
typedefchar Word[MAX+1];
void make_list(ifstream& infile, Word& array);
//@param ifstream& infile: A stream to read an input file
//@param Word& array: an array of type Word that will hold a single word (string) from the input file
//@output: A BST that contains every word from the input file with the correct word frequency
void read_word(ifstream& infile, Word array);
//@param ifstream& infile: A stream to read an input file
//@param Word array: an array containing a Word that will be used as an input and output
//@output: the result will remove any punctuation and will turn all characters to uppercase
int main(){
char file_name[100];
typedefchar Word[MAX+1];
ifstream infile;
Word array;
cout << "Enter a file name: ";
cin >> file_name; // Get file name
infile.open(file_name); // Open file
if(!infile) // If we couldn't open the file...
{
cout << "Failed to open file." << endl;
return 0;
}
make_list(infile, array); // Make the concordance
infile.close(); // Close the file
return 0;
}
void make_list(ifstream& infile, Word& array){
BST bst;
read_word(infile, array); // Read a word
while(!infile.eof()) // While the file isn't empty...
{
bst.insert(array); // Insert into BST
read_word(infile, array); // Read another word
}
cout << bst;
cout << "The file contains " << bst.length() << " distinct words." << endl;
}
void read_word(ifstream& infile, Word array){
char ch;
int i = 0;
infile.get(ch);
while(!infile.eof() && isalpha(ch) && !isspace(ch) && !ispunct(ch))
{
if(i > MAX-1)
{
while(!isspace(ch) && !ispunct(ch))
infile.get(ch);
break;
}
ch = toupper(ch);
array[i] = ch;
i++;
infile.get(ch);
}
if(i != 0)
array[i] = '\0';
}
/*
File Name: BST.h
This is a BST ADT header file that is made for this concordance program
*/
#ifndef CONCORDANCE_H
#define CONCORDANCE_H
#include <iostream>
#include <cstdlib>
constint MAX = 8;
class BST
{
public:
typedefchar Item[MAX+1];
// Constructor
BST();
// Destructor
~BST();
// Modification Member Functions
void insert(Item entry); // Inserts an Item entry into the BST. Duplicates will be ignored
// Constant Member Functions
int length(); // Returns the number of Nodes in the BST
// Friend Functions
//Output function
friend std::ostream& operator << (std::ostream& out_s, BST& c);
private:
// Data Members
struct Node
{
Item data;
int count;
Node *left;
Node *right;
};
Node *root;
void destroy(Node *r); //Deletes all Nodes and pointers in the BST
void print(Node *p, std::ostream& out_s); // Outputs the data and count from each Node struct in the BST to the terminal
void help_insert(Node* &t, Item entry); // Shell insert function
int count(Node *r); //Returns the number of Nodes in the BST
};
#endif