Displaying String Arrays

I have a weird problem with displaying a string array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string WordFile::loadWordList (string wordListString)
{
   // Load the file, then put it into a string.
   wordFile_fstream.open ("..\\..\\file.txt", ios::in);
	
   if (wordFile_fstream.is_open())
   {
      while (! wordFile_fstream.eof()) 
      {
         getline (wordFile_fstream, wordListString);
      }
    }	
    wordFile_fstream.close();

    wordFileLength = wordListString.length();

    return wordListString;
}

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
int main ()
{
   WordFile aWordFile;

   string *stringArray, stringTemp, WordListString;
   string::iterator it;

   // Returns a string with the output of a text file.
   wordListString = aWordFile.loadWordList (stringTemp);
   stringTemp.clear();	

   stringArray = new (nothrow) string [aWordFile.getWordFileLength()];

   unsigned int n =0;

   // My first attempts at read formating from output via a file.
   for (it= wordListString.begin(); it < wordListString.end(); it++)
   {
      stringTemp= stringTemp + *it;     // Read a word into stringTemp until a 
                                        // white space is found . . .	
      switch (*it)
      {
         case ' ':
         case '.':	// . . . until a period is found, ect.
         case '?':
         case '!':
            stringArray[n] = stringTemp;	
            stringTemp.clear();	// Clear stringTemp 
            n++;
            break;
      }
   }

   n = 0;  // counter reset.
   for (it = wordListString.begin(); it < wordListString.end(); it++)
   {
       cout << stringArray[n] << '\n';
       n++;
   }

   cout << "\n\n " << stringArray->max_size();
   delete[] stringArray;

   cout << "\n\n" << "FileLength: " << aWordFile.getWordFileLength() << "\n\n";

   cout << "Press ENTER to exit . . . ";

   cin.get();
   return 0;
}

Compiles fine, but when in runtime . . .
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
file.txt:

This is the output from a text file! Hello programming universe. Bye? Ok bye bye.

Runtime Output: 

This
is 
the
output
from
a 
text
file!

Hello
programming
universe.

Bye?

Ok
bye
bye.

. . . 25+ newlines later . . .    <- The problem

 4294967294

FileLength: 81

Press Enter to exit . . .

Whats the reason behind the the huge jump in the console? There are no newlines/returns written in the file. I did some searching on the internet to find solutions and have found nothing that is remarkable or similar.

Is anyone willing to help me solve this bizarre error?
Last edited on
Line 35 is wrong. It is looping a number of times equal to the number of characters in the line, not the number of words.


BTW, your terminating conditions should be it != str.end(), not i < str.end().

Jsmith,
thanks for your help. By taking a count of the words (or number of elements) in the array I was able to get the proper output without the many extra lines. It seems rather silly that I didn't see that . . .
1
2
3
4
5
6
  int wordCount = n;
  for (n = 0  ; n != wordCount; n++)
  {
      cout << stringArray[n];
      cout << '\n';
  }
Topic archived. No new replies allowed.