Help with using Class Object

I have an assignment where I have to use a String Class and implement that into another .cpp file. Everything compiled ok with the implementation file. However, when I tried to do a command line redirection to run the program the program will time out and won't run at all. I believe it's in a specific line of code. If you can give me a pointer on what I missed. I can't for the life of me see what's going on.
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
#include "string2.cpp"
#include <iostream>
#include <iomanip>
#include <ctype.h>

using namespace std;

const int MAX_WORD_LENGTH = 254;

// The type definition below permits much easier prototyping of
// functions that pass arrays of strings.

char CH[MAX_WORD_LENGTH + 1]; //  + 1 for null terminator.


void WriteWords (String  Word[],
                 int     Count[],
                 int     TotalWordCount,
                 int     DistinctWordCount);

void StoreWord (String  NewWord,
                String  Word[],
                int     Count[],
                int    &TotalWordCount,
                int    &DistinctWordCount,
                int     MaxWordCount);

/***************************  main  ***********************************/

int main()
{
  const  int MAX_WORD_COUNT  = 130;

  String  NewWord,
          Word[MAX_WORD_COUNT + 1] = {"", ""};       // ALWAYS allow 1 extra slot!

  int     Count[MAX_WORD_COUNT + 1] ={0}, // Initialize array to zeros
          DistinctWordCount = 0,
          TotalWordCount = 0;

  cin >> NewWord;

  while ( NewWord != NULL )            //  While NewWord is not null string
    {
    StoreWord (NewWord, Word, Count, TotalWordCount,
               DistinctWordCount, MAX_WORD_COUNT);

    cin >> NewWord;             //  Get the next word
    }

  WriteWords(Word, Count, TotalWordCount, DistinctWordCount);

  return 0;

}

/***************************  StoreWord   ********************************

DESCRIPTION  Adds a string NewWord (holding up to MAX_WORD_LENGTH
             characters) to the end of the array Word.

PARAMETERS   NewWord            The string (array of char) to be added
             Word               An array of String (char[MAX_WORD_LENGTH])
             DistinctWordCount  The number of different words in the array
             TotalWordCount     The total number of word in the file
             MaxWordCount       The max number of words that can be stored
                                in the array Word. If DistinctWordCount ==
                                MaxWordCount, the function is exited.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void StoreWord (String NewWord,
                String  Word[],
                int     Count[],
                int    &TotalWordCount,
                int    &DistinctWordCount,
                int     MaxWordCount)
{
  int i, k = 0;

  while ( (NewWord == Word[k]) > 0 && k < DistinctWordCount )
    ++k;

  // Assert:  k is NewWord's correct position in the ordered array Word

  if ( (NewWord==Word[k]) == 0 )   // NewWord is already there
    {
    ++Count[k];
    ++TotalWordCount;
    }

  else if ( DistinctWordCount < MaxWordCount ) // Room for a new word
    {
    ++DistinctWordCount;  // If this line reached, found new word
    ++TotalWordCount;

    for ( i = DistinctWordCount-1;  i > k;  --i ) // Make room for
      {                                             //  NewWord
      Word[i]=Word[i-1];
      Count[i] = Count[i-1];
      }
    Word[k]=NewWord; // Store NewWord in array
    Count[k] = 1;
    }
}

/***************************  WriteWords   ********************************

DESCRIPTION  Writes the strings in the array Word to standard output.
             The words are left justified. After WORDS_PER_ROW words have
             been written a new line is started.

PARAMETERS   Word       An array of String (char[MAX_WORD_LENGTH])
             WordCount  The number of words in the array

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void WriteWords (String  Word[],
                 int     Count[],
                 int     TotalWordCount,
                 int     DistinctWordCount)
{
  const char *Header1   = "    Word          Count | ",
             *Header2   = "                        | ",
             *Separator = "   | ";

  const int WORD_FW       = 17,
         COUNT_FW      = 4,
         WORDS_PER_ROW = 3;

  int C;

  cout << "\n\n";

  if ( TotalWordCount == 0 )
    return;

  for ( C = 1; C <= WORDS_PER_ROW;  ++C )
    cout << Header1;
  cout << endl;

  for ( C = 1; C <= WORDS_PER_ROW; ++C )
    cout << Header2;
  cout << endl;

  int N = 0;

  while ( N < DistinctWordCount )
  {
    for ( C = 1; C <= WORDS_PER_ROW && N < DistinctWordCount; ++C )
    {
      cout << setiosflags(ios::left)  << setw(WORD_FW) << Word[N];
      cout << setiosflags(ios::right) << setw(COUNT_FW)<< Count[N];
      cout << Separator;
      cout << resetiosflags(ios::right);
      ++N;
     }
    cout << endl;
   }

  cout << "\nTotal    Word Count : " << TotalWordCount    << endl;
  cout << "\nDistinct Word Count : " << DistinctWordCount << endl;
}
Topic archived. No new replies allowed.