Reading file input as opcode in C++
<p>I am working on a project for a class at school. It is a simple implementation of stacks and queues. However as part of the project we are require to read opcode in from a file. The opcode is formated as follows:</p>

<pre><code>append 10
serve
append 20
append 30
serve
push 10
push 50
push 20
push 20
pop
</code></pre>

<p>My problem is when I read in the file through a standard fstream it seems to pick up some kind of weird formatting or something, and won't match comparison checks.</p>

<p>I am wonder what I am doing wrong, how to fix it, and if there is a better way to manipulate opcode going forward. As it is, the if-else statement always goes to if. Kind of desperately need to get this working.</p>

<pre><code>#include "StackAndQueue.h"
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

int main(){
    Stack leStack;
    Queue leQueue;

    //Read in the datafile. 
    cout &lt;&lt; "Reading default file: p2datafile.txt";
        fstream data("p2datafile.txt");
        while (data.fail()){
                cout &lt;&lt; " failed." &lt;&lt; endl;
                data.close();
                cout &lt;&lt; "Please enter path to datafile: ";
                string filename;
                cin &gt;&gt; filename;
                data.open(filename.c_str());
        }
        cout &lt;&lt; endl &lt;&lt; "Sucess!" &lt;&lt; endl;

    //Loop through all the commands in the file
    while(!data.eof()){
        // Determine what kind of command is running 
        // and if parsing will be needed.

        string opcode;          
        getline(data,opcode,' ');

        if (opcode == "pop"){
            cout &lt;&lt; "popping!" &lt;&lt; endl;
            leStack.pop();
        }
        else if (opcode == "serve"){
            cout &lt;&lt; "serving" &lt;&lt; endl;
            leQueue.serve();
        }
        else if (opcode == "push"){
            cout &lt;&lt; "pushing";
        }
        else{
            cout &lt;&lt; "else!" &lt;&lt; endl;
        }
    }
    data.close();

    system("pause");
    return 0;
}
</code></pre>

<p>I apologize if the code is difficult to read, and the general half-finished nature of it. I am still pretty new to this.</p>
