Vector emptying itself...

Somehow a vector, operands, is emptying itself, yet I make no mention of the operands.erase()! Here is main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int main()
{
    int n = 0;
    string line;
    vector <string> lines;
    string filename, directory;
    bool keepgoing = true;
    while (keepgoing)
    {
        cout << "Please enter the program for which you want to generate the machine code for (omit the '.txt'; it will be assumed) : ";
        getline(cin, filename);
        cout << "Now enter the full directory (if you are using a Windows OS, click the address bar next to the Refresh button and type the text here)"<<endl;
        cout << "You can just enter '-1' if it is in the same directory as this program."<<endl;
        getline(cin,directory);
        filename += ".txt";
        if (directory != "-1") chdir(directory.c_str());    //Function may be deprecated...
        for (size_t pos = filename.find_first_of('\\'); pos != filename.npos; pos = filename.find_first_of('\\', pos + 1))
        {
            directory.at(pos) = '/';
        }   //end for
        ifstream thefile (filename.c_str());
        if (thefile.is_open())
        {
            while (thefile.good())
            {
                getline(thefile, line);
                lines.push_back(line);
                n++;    //n == size of heap
            }   //end while
            thefile.close();
            keepgoing = false;
        }   //end if
        else cout << "Invalid filename and extension. Please try again.\n"<<endl;
    }   //end while
    assemblytoopcode * a = new assemblytoopcode[n];
    for (int m = 0; m < n; m++)
    {
        a[m] = assemblytoopcode(lines[m]);
        //This is the place to use iscomment() to check for lines that are either comments or empty.
        if (!(a[m].iscomment()))
        {
            a[m].findlabels();
            a[m].findoperands();
        }   //end if
        a[n-1].setlabels(a[m].getlabels()); //Pushing every non-empty templabel into labels
        a[n-1].setoperands(a[m].getoperands()); //Pushing every operand into operands...
        cout << "a["<<m<<"].getlabels() == "<<a[m].getlabels()<<endl;
        cout << "a["<<m<<"].getoperands() == "<<a[m].getoperands()<<endl;
    }   //end for
    if (!(a[n-1].checkduplicates()))
    {
        a[n-1].minvariableline();
    }   //end if
    a[n-1].PressEnterToContinue();
    return 0;
}

Here is my getoperands():
1
2
3
4
string assemblytoopcode::getoperands()
{
    return tempoperand;
}   //end getoperands 

Here is setoperands():
1
2
3
4
void assemblytoopcode::setoperands(string str)
{
    if (str != "") operands.push_back(str);
}   //end set operands 

Here is also a test assembly program (in .txt format): http://www.megafileupload.com/en/file/374221/basic-txt.html. The goal of this program is to simulate the object code for an SIC/XE assembly program.
The entire program (if necessary): http://megafileupload.com/en/file/374368/assembler-main-2--cpp.html
All I need is this mystery taken care of, and then I can move on to simulate pass two of the assembler (at least for the very basic file I have sent).
Last edited on
closed account (DSLq5Di1)
1
2
3
4
5
6
7
    for (int m = 0; m < n; m++)
    {
        a[m] = assemblytoopcode(lines[m]);
        ...

        a[n-1].setlabels(a[m].getlabels()); //Pushing every non-empty templabel into labels
        a[n-1].setoperands(a[m].getoperands()); //Pushing every operand into operands... 

What if m is equivalent to n-1?
Then the (n-1)th line in the file (starting at 0) will be the constructor value, which will be parsed into templabels and tempoperand. These values will be pushed into the vectors labels and operands, respectively.
closed account (DSLq5Di1)
The (n-1)th line is the last line of input, and you are using the object at this position to push back labels and operands. What happens to that object when you read in the last line of input? it is replaced with a new instance by a[n-1] = assemblytoopcode(...);.
I knew I should have kept the second loop for that one! Thanks, man! Problem solved!! (idk why I am overlooking the obvious...)
Last edited on
Topic archived. No new replies allowed.