I have an object called Symbol
which represents a variable. Symbol has the Boolean attribute used
which is initialized as false
. This condition is changed to true in the event that it is called upon to solve a function to show that it was used. For some reason when Symbol
is unused and used
is never changed to true it returns the value 204 instead of false.
Here is SymbolTable.h where I define a Symbol:
class SymbolTable {
public:
SymbolTable() {}
void insert(string variable, double value);
double lookUp(string variable) ;
bool unusedVar() ;
private:
struct Symbol {
Symbol(string variable, double value)
{
this->variable = variable;
this->value = value;
bool used = false;//This is my boolean flag
}
string variable;
double value;
bool used;
};
...
Whenever I look up a value from the SymbolTable to plug into a equation I set used
to true:
double SymbolTable::lookUp(string variable) {
for (int i = 0; i < elements.size(); i++) {
if (elements[i].variable == variable) {
elements[i].used = true;//Right here it changes to true
return elements[i].value;
}
...
Later on when I try to detect if any are still false it wont work. And when I print the value of used
it prints 204!
bool SymbolTable::unusedVar() {// returns error if Var is not used
int count = 0;
for (int i = 0; i < elements.size(); i++) {
std::cout << "Variable: " << elements[i].variable << " was " << elements[i].used << std::endl;
if ( !elements[i].used ) {
std::cout << "Warning: A variable was not used" << std::endl;
return true;
}
}
return false;
}
Why could this be happening?