-1

I'm running into this problem trying to take data from input file into my vector<stack<string>>. I don't know why but looks like my code completely skip the first line of input Here is the input file:

1 D
2 C A
3
4 B E

Here is my code

ifstream myfile1;  
ifstream myfile2;                                          //take in the input file

string line, filename, _block;
vector<stack<string>> _stack;
stack <string> _elements;

cout << "Please enter name of beginning state" << endl;
cin >> filename;
// -------------------------------------------------------------------
//      OPEN FILE AND LOAD VERTICES AND EDGES IN APPROPRIATE VECTOR
// -------------------------------------------------------------------
myfile1.open(filename.c_str());
if (myfile1.is_open())
{
    cout << "File found!" << endl;
    myfile1 >> line;       
    while (getline(myfile1, line))
    {
        int i;
        string a;
        stringstream ss ( line );
        ss >> i;                    // "e"
        cout<<"Test stupid: "<< i <<endl;
        while( ss >> a )
        {
            _elements.push(a);
            cout <<"Test dump: "<< a <<endl;
        }
        _stack.push_back(_elements);
        //cout <<"Test: "<<_elements.top()<<endl;
        num_of_stacks = _stack.size();
        num_of_elements = _elements.size();
    }
    //cout <<"Test: "<<_elements.top()<<endl;
    while(!_elements.empty())
    {
        string w = _elements.top();
        cout <<"Test1: "<< w <<endl;
        _elements.pop();
    }
    cout <<"Test2: "<<num_of_stacks<<endl;
    cout <<"Test3: "<<num_of_elements<<endl;
}
Mr Lister
  • 45,515
  • 15
  • 108
  • 150

1 Answers1

0

Before your while (getline(myfile1, line)) { loop, you have myfile1 >> line;, which will read in data that is immediately discarded when you call getline the first time. Perhaps delete the myfile1 >> line; line?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271