0

I am trying to write a program that calls a custom function. Unfortunately, once it enters the function, it ceases to print what I told it to and gets stuck indefinitely. (I run a mac 10.9, and the newest Xcode)

The two functions are:

int main(void)
{

    string filePath;
    treeNode* root;
    int words;

    //filePath = getFileName();

    cout << "Choosing pathway.\n";
    root = readIn(words, "/Users/Noah/Desktop/SmallFile.txt");

    cout << "File read.\n";
    root->printInOrder(root);

}

and:

treeNode* readIn (int& count, string file)
{
    cout << "Here.  ";
    treeNode* root = new treeNode;

    string insert;
    ifstream reading;
    count = 0;                // DECLARE VARIABLES!
    int size = 0;

    cout << "Made variables.  ";

    reading.open(file); // Open and check file

    cout << "File up.  ";

    //for  (int i = 0; i < 15; i++) // See coder's note at top.
    while (!reading.eof())
    {
        cout << "In loop.  ";

        char c = reading.get(); // Obtain character to check

        if (isalnum(c))
        {
            insert += c; // Word Storage update
        }

        else
        {
            if (count != 0)
            {
                insertWord(insert, root); // Insert word

                insert.clear();
                size = 0;
                count ++;

            }
        }

    }

    reading.close();
    return root; // Gives top node back.
}

My question is: Are there any obvious errors that would cause this? If not, is it compiler related? And is there anything I can do about it?

vsoftco
  • 52,188
  • 7
  • 109
  • 221
HadesHerald
  • 131
  • 1
  • 7
  • Not directly related, but `while (!reading.eof())` [is almost always wrong](http://stackoverflow.com/q/5605125/3093378). – vsoftco May 19 '15 at 00:55
  • Your `if (count != 0)` test will never be true because you initialize `count` to zero and increment inside the body of that `if` (which will never be entered). – nobody May 19 '15 at 01:02
  • 1
    Compiler not entering functions properly or my code has a bug. One of those is more likely than the other... – John3136 May 19 '15 at 01:04
  • You can change `while(!reading.eof())` to `while(cin>>c)`,that might work. – icecity96 May 19 '15 at 01:09
  • You have no newlines (or `std::endl`) in your print statements in the function so the output may be getting buffered and not displayed. Try using a debugger to break in to the code and see what it's doing. – nobody May 19 '15 at 01:16
  • It looks like you don't put any payload data into your "root" treenode. – Mike Crawford May 19 '15 at 03:53

0 Answers0