The last cout statement does not output

I was using my own smaller test code, and the final cout statement "The most frequent word is: " printed out the correct answer.

However, when using my supplied test code, suddenly, the last statement refused to show up? I have no idea what could have gone wrong. I would appreciate any help.

Here is my code below:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 #include <iostream>
#include <fstream>


using namespace std;

class node {
public:
  string word;
  int count = 0;
  node *left, *right;
  node(string iword)
  {
    word = iword;
    left = right = NULL;
  }
  void insertLeft(node *p)
  {
    left = p;
  }

  void insertRight(node *p)
  {
    right = p;
  }
};


void inOrder(node *r)
{
  if(r != NULL)
  {
    inOrder(r -> left);
    cout << r->word << " ";
    inOrder(r->right);
  }
}



void bInsert(string c, node **t)
{
  if (*t == NULL)
     *t = new node (c);

  else if((*t)->word <= c)
  bInsert(c, &((*t)->right));
  else
    bInsert(c, &((*t)->left));
    return;
}


node *&find(string c, node* &t){
  if (t == NULL ){
    return t;
  }
  else if (c == t->word){
      return t;
  }
  else if (c < t->word) {
      return find(c, t->right);
  }
  else {
      return find(c, t->left);
  }
}

node *findOrAdd(string c, node * &t)
{
    node * &p = find(c, t);
    if (p == nullptr) {
        p = new node(c);
    }
    return p;
}

node* previous = NULL;
string word = " ";
int count = 1;
int maxx = 0;

string mostFrequent(node *node){

if(node == NULL)
  return " ";

mostFrequent(node->left);
if(previous != NULL){
  if (node->word == previous->word){
  count++;
  }
  else {
    count = 1;
  }
}

  if(count > maxx){
    maxx = count;
    word = node->word;
  }

  previous = node;
  mostFrequent(node->right);

}

string findMostFrequent(struct node* node){
  mostFrequent(node);
  return word;
}


int uniqueCount(node* root, string c){
  int count = 0;
  if(root == NULL)
    return 0;

else
  if (root->word == c)
  count++;
  return 1 + uniqueCount(root->left, c) + uniqueCount(root->right, c);
}


string longestWord(node* node){
  string leftMax, rightMax, word = " ";
    if (node == NULL)
      return " ";

word = node->word;
leftMax = longestWord(node->left);
rightMax = longestWord(node->right);

  if(leftMax.size() > word.size())
    word = leftMax;
  if(rightMax.size() > word.size())
    word = rightMax;

return word;
}



int main()
{
  node *T, *R;
  ifstream fin;
  string c;
int counter;

  fin.open("C:\\Users\\owner\\Documents\\mytest.txt");
  /*if(fin.fail())
  {
    cout << "Could not find your file. Shutting down.";
    exit(1);
  }*/
  T = nullptr;
  R = nullptr;
  while (!fin.eof()) {
      node *p = findOrAdd(c, T);
        if (c != " ")
        bInsert (c, &R);
      counter = uniqueCount(T,c)-1;
      fin >> c;
  }
fin.close();



  cout << "In-order\n";

  inOrder(R); cout << endl;
  cout << endl;
  cout << "The number of unique elements in the tree are: " << counter << endl;
  cout << "The longest word is: " << longestWord(R) << endl;
  cout << "The most frequent word is: " << findMostFrequent(R) << endl;

}
Last edited on
Here is the file that we were given: http://txt.do/1qn90
Sorry guys, this is the last thing I'll need help with
When you say "the last statement refused to show up" does that mean that the words "The most frequent word is: " don't show up either? Or is it just the answer that is blank?

If it is the latter, then it may be that R is null when mostFrequent is called. You are going to have to test if that is the case and see why that is.

Furthermore, why are you returning node * 's by reference? That seems a bit silly. Just pass/return them by value.
Last edited on

When you say "the last statement refused to show up" does that mean that the words "The most frequent word is: " don't show up either? Or is it just the answer that is blank?


Yes. It's supposed to look like this: http://prntscr.com/sawem9
But it comes out looking like: http://prntscr.com/sawf0f

Hello KittyIchigo1,

On lines 153 and 157 remove the comments and change "exit(1)" to "return 1". "exit" may not leave the program properly and since you are in "main" return would be better. And if (!fin) is all you need.

On line 160 you enter the while loop because "eof" is not set yet and the then the first thing you do is call a function sending it a character that has not been read from the file yet.

Then you finish the while loop using a character variable that has no value until the last line where you finally give it a value for the next iteration of the loop.

That is what I see without testing. There could be more.

Andy
Topic archived. No new replies allowed.