I think I found the error. I try a
much simpler program and crash the program when I
intentionally enter a value not in the array of structs. Here is some code:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
struct symboltable
{
string symbol;
int value;
}; //end symboltable
symboltable * symtab;
int findvalue(string yourstring)
{
int y = 0;
while ((yourstring != symtab[y].symbol) && (y < 59))
{
y++;
} //end while
if (y != 59) return symtab[y].value;
else cout << "An error occurred. Take measures"<<endl;
} //end findvalue
int main()
{
int x = 0;
size_t pos = 0;
string * str;
string command;
string tempstr;
ifstream myfile ("symbol definitions.txt");
bool dothis;
if (myfile.is_open())
{
while (myfile.good())
{
getline(myfile, tempstr);
x++; //Opening the file once to obtain the size of the dynamic array....
} //end while
myfile.close();
dothis = true;
} //end if
else
{
cout << "Uh-oh. Something went wrong!" << endl;
dothis = false;
} //end else
if (dothis)
{
myfile.clear();
myfile.open("symbol definitions.txt");
cout << "x == " << x << endl;
str = new (nothrow) string [x];
symtab = new (nothrow) symboltable [x];
cout << "Your data structure looks like this: \n"<<endl;
if ((str != 0) && (symtab != 0))
{
for (int w = 0; w < x; w++)
{
getline(myfile, str[w]);
//Parsing element of string array into the data structure
pos = str[w].find_first_of('\t');
symtab[w].symbol = str[w].substr(0, pos);
str[w].erase(0, pos+1);
stringstream(str[w])>>hex>>symtab[w].value;
cout << symtab[w].symbol << '\t' << symtab[w].value<<endl;
} //end for
myfile.close();
} //end inner if
else cout << "ERROR: Cannot allocate memory."<<endl;
cout << "Please enter an assembler command for which I should fetch the value of (in all caps): ";
cin >> command;
cout << "The integer value for what you have entered is: "<<findvalue(command)<<endl;
} //end if
return 0;
} //end main
|
This is the exact same program that I got the struct-making code for the big program from. Why does the program crash if the string is not in the array of data structures? How do I prevent such things from happening? Better yet, how do I search a dynamic array of strings?
The other idea I had was to allocate some more memory to store the names of each register used so. I could do this in the simulated first pass. Then, I could just find out
if (operands[x].find_first_of(',') == 1)
, and if it is, compare it against the dynamic array holding the register names. This would work because, in the SIC/XE machine, register names are all one character. I just wish C++ would be able to handle the possibility that either a string is not equal to any of it's like-type members of a dynamic array, or the process of checking the sizes string to be matched (and the one to compare with in the array), and then using an integral comparison between the two. I wonder if using
operands[x] = operands[x].c_str()
and doing this all C-style (which is pretty much what I was getting ready to try...) would work.