0

This is really weird. Without the ifs, string action get assigned but if I throw the ifs between, action stays always "dim".

Code without the ifs:

void Interaction::LoadFile(string fileName)
{
    int arg1, arg2;
    string line, action;
    vector <string> v;
    istringstream cmd;
    ifstream file;

    file.open(fileName + ".txt");
    if (file.is_open())
    {
        while (getline(file, line))
            v.push_back(line);

        file.close();

        for (int i = 0; i < v.size(); i++)
        {
            cout << i << ": " << v[i] << endl; // DEBUG PURPOSES

            cmd.str(v[i]);
            cmd >> action;

            // STRING MUST BE CLEARED??
            action.clear();
    }
    else
    {
        cout << "Can't open " << fileName << endl;
        return;
    }
}

Code with the ifs:

void Interaction::LoadFile(string fileName)
{
    int arg1, arg2;
    string line, action;
    vector <string> v;
    istringstream cmd;
    ifstream file;

    file.open(fileName + ".txt");
    if (file.is_open())
    {
    while (getline(file, line))
        v.push_back(line);

    file.close();

    for (int i = 0; i < v.size(); i++)
    {
        cout << i << ": " << v[i] << endl;

        cmd.str(v[i]);
        cout << "CMD: " << cmd.str() << endl;
        cmd >> action;

        cout << "action: " << action << endl;

        if (getDebug())
            cout << "action : " << action << endl;

        // DIM ASSIGNMENT
        if (action == "dim")
        {
            cmd >> arg1 >> arg2;
            mapLines = arg1;
            mapColumns = arg2;
        }

        // COINS ASSIGNMENT
        if (action == "coins")
        {
            cmd >> arg1;
            coins = arg1;
        }

        // OPONENTS ASSIGNMENT
        if (action == "oponents")
        {
            cmd >> arg1;
            cout << arg1;
            numOp = arg1;
        }

        // STRING MUST BE CLEARED??
        action.clear();
    }

Why is action changing without the ifs but when I add them, action stays always "dim"?

This is really strange.

Thanks in advance

g4rrucho
  • 13
  • 1
  • 4

0 Answers0