C++ File Input Skipping Lines

24 views Asked by At

I'm writing a program that reads LISP notation into binary trees, then evaluates them given a target sum to see if a path where each node's "value" is added to a running sum can equal the total sum. If there is such a path, it will write the path. If there is not, it just says there is no path. Or it's supposed to, at least.

PROBLEM As it is now, the code skips the second and fourth LISP inputs that it is supposed to read from the file.

I know with a good certainty that the functions which read the LISP into a binary tree work. I know with good certainty that the evaluate function works properly (I know this because I swapped the input lines around and they all work properly when they are actually read by the inFile). My current best guess is that my main file is configured incorrectly, causing it to only pick up the first and third LISP inputs only.

Here is my main file:

#include <iostream>
#include <fstream>
#include "binaryTree.h"
#include "myStack.h"
using namespace std;

int main() {

    // initialize the tree, the root pointer, and the stack to be used in the function
    binaryTree<int> myTree;
    binTreeNode<int> *root = nullptr;
    myStack<int> path;

    // create the inFile for reading the data from input.txt
    ifstream inFile("input");

    if (!inFile.is_open()) {
        cerr << "Unable to open file" << endl;
        return 1;
    }

    //declarations to be used in the while loop
    bool validPath;
    int targetSum;
    int plusSignModulator;

    // while there is input in the file
    while(inFile >> ws >> targetSum) {

        if (inFile.fail()) {
            // clear errors
            inFile.clear();
            continue;
        }

        cout << "The target sum is: " << targetSum << endl;

        // initialize root, left, and right to avoid segmentation error
        root = new binTreeNode<int>;
        root->left = nullptr;
        root->right = nullptr;

        cout << "Reading the tree..." << endl;

        // run the readLISP function to read the data and build it into a binary tree
        myTree.readLISP(root, inFile);

        // run the evaluate function to find out if there is a path or not
        validPath = myTree.evaluate(root, 0, targetSum, path);
        if (validPath) {

            cout << "Valid path in tree exists:" << endl;

            // integer that toggles from 1 to 0 to determine if a plus sign is needed when printing
            plusSignModulator = 1;

            // print out every element of the correct path in the stack
            while (!path.isEmpty()) {
                if (path.top() != 0) {
                    // if the modulator is set at 1 (only happens on the first item) do not print  
                    // the plus sign
                    if (plusSignModulator == 1) {
                        cout << path.top();
                        plusSignModulator--;
                    }
                        // if the modulator is not set at 1 print the plus sign to fill in the
                        // expression
                    else {
                        cout << " + " << path.top();
                    }
                }
                path.pop();
            }
            cout << " = " << targetSum << endl << endl;
        } else {
            cout << "There is no path." << endl << endl;
        }
        inFile.ignore(numeric_limits<streamsize>::max(), '\n');
        while (!path.isEmpty()) {
            path.pop();
        }
        delete root;
    }
    return 0;
}

Here is the input file:

22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))

20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))

10 (3
(2 (4 () () )
(8 () () ) )
(1 (6 () () )
(4 () () ) ) )

5 ()

The output looks like this:

The target sum is: 22
Reading the tree...
Valid path in tree exists:
5 + 4 + 11 + 2 = 22

The target sum is: 10
Reading the tree...
Valid path in tree exists:
3 + 1 + 6 = 10


Process finished with exit code 0

I'm not sure if this matters, but I code in CLion and I am pretty new to C++.

Any help would be greatly appreciated.

Thank you in advance! :)

0

There are 0 answers