Hello,

I have been working on this code. What it does basically is read a text file, store the words in a binary search tree, prints out the words in alphabetical order along with line number where the word occur.

Example:
Input:
I need help with
this code

Output:
code 2
help 1
I 1 1
need 1 2
this 2 1
with 1 4

My code reads a text file, store it in a BST, prints out the word in alphabetical order. What I couldn't figure out is how to find the word's location in a line. If anyone has any ideas it will be highly appreciated.

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

class BSTree
{
private:
    struct treeNode
    {
      treeNode* left;
      treeNode* right;
      string word;

      treeNode(string s)
      {
        word = s;
        left = NULL;
        right = NULL;
      }
    };

public:
treeNode* root;

BSTree()
  {
root = NULL;
  }

  void insert(treeNode *&root, string newNode)
  {

    if(root == NULL)
      {
        root = new treeNode(newNode);
        return;
      }
    else if(newNode < root->word)
      {
        insert(root->left, newNode);
      }
    else
      {
        insert(root->right, newNode);
      }
    }

void print(treeNode *node)
  {
    if(node != NULL)
      {
        print(node->left);
        cout << node->word << endl;
        print(node->right);
      }
  }
};

main()
{
ifstream fis;
string str = "";
string fileName
BSTree bst;

cout << "Enter file: " << endl;
cin >> fileName;

fis.open(fileName.c_str());

while(!fis.eof())
    {
      fis >> str;
      bst.insert(bst.root,str);
    }
    bst.print(bst.root);
}

>>What I couldn't figure out is how to find the word's location in a line
According to the description of the program you posted all you need is the line number, not the location within the line. For line numbers, just use an integer to keep track of them. You also need to add int lineno to the BSTree class so that line number and word can be printed together.

I see what you are saying. I added the lineno, but what I'm having trouble with is the "keep track" part. Here is what I have in my client:

main()
{
ifstream fis;
//string str = ""; <--- not using this
char str[10];
char *c;
int lineNum = 1;
string fileName
BSTree bst;

cout << "Enter file: " << endl;
cin >> fileName;

fis.open(fileName.c_str());

while(!fis.eof())   //while file is open
    {
      fis >> str;   //load words

      c = strtok(str, " ,.");   //function for splitting string into tokens
      if(c == "\n")
          lineNum++;

      bst.insert(bst.root, c, lineNum);
    }
    bst.print(bst.root);
}

but I get the words with 1s. That means that lineNum is not actually counting the lines. So my question is how can I let lineNum count the lines? In other words, how can I let it go through the text and count how many '\n'?

Thanks ...

I'll let others more knowledgable than me comment on your logic but I will make a couple observations on your syntax.

char *c;
if(c == "\n")

You can't compare C style strings using the == operator. You have to use a strcmp() function, or roll your own.

fis >> str;
c = strtok(str, " ,.");

str will never have a space in it so using space as a delimiter in strok() doesn't make much sense. If str needs to be able to have embedded whitespace like a space char, then use getline() as input function, not >>.

Using the return value of eof() to control a loop like this,

while(!fis.eof()) //while file is open

is a bug that will cause duplication of the last valid input (or some other hard to find error) sooner or later. Don't use it. Use the return value of an input protocol like >> or get() or getline() instead.

you can use getline() to read each line, and count line numbers. Then use stringstream to split the line into words.

#include <string>
#include <fstream>
#include <iostream>
#include <sstream> // for stringstream
#include <vector>
using namespace std;

struct words
{
    string word;
    int lineno;
};


int main()
{
    vector<words> wordlist;
    words thisWord;
    int lineno = 0;
    string line;
    ifstream in(filename.c_str());
   while( getline(in, line) )
   {
         lineno++;
         stringstream s;
         s = line;
         string word;
         while( s >> word )
         {
             thisWord.word = word;
             thisWord.lineno = lineno;
             wordList.push_back(thisWord);
         }
     } 
}
This article has been dead for over six months. Start a new discussion instead.