Hi, what am I doing wrong I'm reading from a file with words and trying to get the output properly. I know it's reading, and counting the amount of words in it. But when I try to output I get a bunch of gibberish. Any help would be appreciated.
In main I call the load function as follows:
Code:
Myobject newobject;
string filename;cout << "enter filename ";
cin >> filename; newobject.loadthisfile(filename);
Then the function is as follows:
Code:
SIZE=4
void Myobject::loadthisfile(const string & filename)
{
ifstream input(filename.c_str());
string newword;
if (input.fail())
{
cout << "couldn't not open file " << filename << endl;
exit(1);
}
while (getline(input,newword)) // read one line
{
newword = newword.substr(0,SIZE); //
if (isalpha(newword[0])) //
{
if (wordscount >= wordlist.size()) // double if needed
{
wordlist.resize(wordlist.size() * 2);
}
wordlist[wordscount] = new Myobject(newword);
cout<<wordlist[wordscount]<<endl; //// produces gibberish ERROR here
wordscount++;
cout<<wordscount<<endl; ///echoes proper number of words from textfile
}
}
}