Program already runs, but can someone explain why my program is displaying the wrong numbers from a read file.

Hello, below I'll attach the prompt and requirements for easier understanding. My program also runs but I can't seem to figure out how to make it count the right amount of integers. Thank you for your time.

In this assignment, you are to write a program that stores the text from an input file, counting the number of times each word appears in the text and allows users to query the data structure.

Specifications & Requirements

For purposes of this assignment, a word is any consecutive string of letters and the apostrophe character, so don’t counts as a single word, and best-selling counts as two words: best and selling.

Your word counts must ignore capitalization, so the, The, THE, and tHe all increase the count for the word “the” by one.



The input file must not contain numbers.

You must store the words and the the number of occurrences of each word in a single binary search tree. Each word occurring in the text can only be stored once in the tree.

Create a class for the nodes of the tree called WordNode; and call the branches in this structure left and right.

Create a class implementing a binary search tree called WordTree. It must also contain the following public methods:


output file:

a against against against against age and and and and and at at bay be because blaze bless blind blinding bright burn by caught close could crying curse danced dark day death deeds do do do do dying dying dying dying end eyes father fierce flight forked frail gay gentle gentle gentle gentle go go go go good good good good good grave green grieved had have height how i in in into into into into is it its know last late learn light light light light lightning like me men men men men meteors might my near night night night night no not not not not now of of of of of old on on pray rage rage rage rage rage rage rage rage rave right sad sang see should sight sun tears that that that that the the the the the the the the the the the their their their there they they though too wave way who who wild wise with with words you your
There are 1135201625 in the file
There are 1135201625 unique words in the file
Alphabetical first word: a
Alphabetical last word: your

Process returned 0 (0x0)   execution time : 0.052 s
Press any key to continue.


main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 #include <iostream>
#include <bits/stdc++.h>
#include "WordTree.h"
#include "WordNode.h"

using namespace std;

int main()
{
    fstream file;
    string word, filename;
    filename = "test.txt";
    file.open(filename.c_str());

    WordTree * tree = new WordTree;

    while (file >> word) {
        word.erase(remove(word.begin(), word.end(), ','), word.end());
        word.erase(remove(word.begin(), word.end(), '.'), word.end());
        word.erase(remove(word.begin(), word.end(), ';'), word.end());
        word.erase(remove(word.begin(), word.end(), '-'), word.end());
        transform (word.begin(), word.end(), word.begin(), ::tolower);
        tree->Insert(word);
    }

    tree->PrintTreeInOrder();

    cout << "There are " << tree->CountWords() << " in the file" << endl;
    cout << "There are " << tree->CountUniqueWords() << " unique words in the file" << endl;
    cout << "Alphabetical first word: " << tree->FindMin() << endl;
    cout << "Alphabetical last word: " << tree->FindMax() << endl;

    return 0;
}  



the functions not working from the .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int WordTree::CountWords(WordNode * node)
{
    if(node == NULL)
        return 0;
    else
    {
        int words = node->count;
        words =  words + CountWords(node->Left);
        words =  words + CountWords(node->Right);
        return words;
    }
}

int WordTree::CountWords()
{
    return CountWords(root);
}

int WordTree::CountUniqueWords(WordNode * node)
{
    if(node == NULL)
        return 0;
    else
    {
        int words = node->count;
        words =  words + CountUniqueWords(node->Left);
        words =  words + CountUniqueWords(node->Right);
        return words;
    }
}

int WordTree::CountUniqueWords()
{
    return CountUniqueWords(root);
} 

Last edited on
Why are you using <bits/stdc++.h>???
https://www.geeksforgeeks.org/bitsstdc-h-c/

It is nonstandard, like <conio.h>, <unistd.h>, or <windows.h>. IMO headers like those should be avoided unless you're absolutely sure they will only be used with compilers that support them.

According to geeksforgeeks, it apparently just includes every standard C++ library, which, while it does eliminate the need to type in every library that you want, it will slow down compiling and may cause issues.

It's kind of like using namespace std;. It is a one-size-fits-all, comprehensive solution to laziness on the part of people who don't want to type std:: for every standard object, or don't want to put using std::cout;, etc. Or it might have just been what they were taught, in which case that applies to their teacher.

In addition, it can cause problems, like with namespace std, if you call a variable something like "string" then that will cause a compiler error with the std::string library. If it doesn't, there is something wrong with your compiler.

To end my rant, solutions like this are ok for just dinking around on your own, or if you are absolutely sure it won't cause problems, but they should never find their way into production code where they can cause errors that can be extremely hard to find and fix.
FanOfThe49ers, without seeing the implementation of your tree, we are only guessing as to what your problem is.

But notice that the implementations of your CountWords and CountUniqueWords functions are exactly same except by name. Your CountWords function looks correct as far as I can tell, but your CountUniqueWords function is wrong. Instead of adding the count, simply add 1 each time a node is visited. This assumes that when you insert an existing word, a new node is not created, rather the count is incremented for that node.

PS: You can just say: WordTree tree; without 'new' (this is not necessarily the case for the individual WordNodes).
Also, learn how to use a debugger - it will save you hours of staring at code.
Topic archived. No new replies allowed.