My ifstream
is not reading integers correctly. My main
is as follows:
#include <fstream>
#include <vector>
#include <string>
int main() {
vector<int> myvec;
Tree myAVL;
ifstream myfile;
string filename;
cout << "Enter the file name you would like to use: ";
cin >> filename;
myfile.open(filename);
while (!myfile) {
cout << "ERROR: INVALID FILENAME -- Please input valid filename: ";
cin >> filename;
myfile.open(filename);
}
while (!myfile.eof()) {
int x;
myfile >> x;
if (myvec.size() == 0) {
myvec.push_back(x);
myAVL.insert(x);
continue;
}
if (checkVecDuplicate(myvec, x))
{
myAVL.insert(x);
myvec.push_back(x);
}
}
for (int i = 0; i < myvec.size(); i++)
{
cout << myvec[i];
}
cout << endl << endl;
Node* temp = myAVL.head;
myAVL.inorder(temp);
cout << endl << endl;
myAVL.preorder(temp);
cout << endl << endl;
system("pause");
return 0;
}
My text file I'm trying to read in is:
14 32 64 55 1 12 3 4 16 72 125 54
Why is this not reading any of the integers? At myfile >> x
it's reading in a null value for the integer and I have no clue why. Thank you!