I am working on a program dealing with binary search trees. I have a simple text file that looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$
5 4 3 5 7 13
$
11 6 7 2 12 6
The first line contains integers to be inserted into the tree, the third line is integers to be searched, and the fifth line is integers to be removed. The second and fourth lines are meant to mark the separation of the other lines. They contain a single character '$'. I am struggling with how to ignore the second and fourth lines. I have three while loops separated with two ignore statements. Upon testing, it seems that the second and third while loops are never entered. I'd appreciate any help - my code is below.
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "main.h"
int main(int argc, char *argv[])
{
string filename = "";
ifstream inFile;
filename = argv[1];
inFile.open(filename.c_str());
BST b;
int num;
while(inFile >> num)
{
b.insert(num);
cout << num << endl; //output testing - the first line was successfully output so I think this part is fine
}
inFile.ignore(500, '$');
while(inFile >> num) //this while loop is never entered
{
b.search(num);
}
inFile.ignore(500, '\n');
while(inFile >> num) //this while loop is never entered
{
b.remove(num);
}
inFile.close();
}
When you are done with the loops that read the numbers, add a line to clear the error state of the input stream. Without that, no subsequent reading operations are performed.
while(inFile >> num)
{
b.insert(num);
cout << num << endl; //output testing - the first line was successfully output so I think this part is fine
}
inFile.clear(); // ADD THIS LINE
inFile.ignore(500, '$');
while(inFile >> num) //this while loop is never entered
{
b.search(num);
}
inFile.clear(); // ADD THIS LINE
inFile.ignore(500, '\n');