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;
}
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...
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.
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(...);.