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;
}
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()];
unsignedint 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;
}
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?
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';
}