2

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?

137
  • 781
  • 1
  • 8
  • 21

1 Answers1

8

Why could this be happening?

Two reasons:

  1. SymbolTable::Symbol::used isn't initialized in the constructor SymbolTable::Symbol(string, double), instead you're initializing the local variable.

    bool used = false;//This is my boolean flag - the bool in the beginning of the line makes it a declaration of a new local variable, hiding the member variable used; removing it will refer to the member variable bool. Alternatively, like Hans mentions in the comments, replacing that line with this->used = false; will fix it too.

  2. You're not enabling the std::boolalpha flag and hence std::cout will print boolean values as integers only; thus the underlying integer value is getting printed; in this case, it's an uninitialized random value (204).

This should solve it:

struct Symbol {
    Symbol(string variable, double value) : variable(variable), value(value), used(false)
    {
    }
...

std::cout << "Variable: " << elements[i].variable << " was " << std::boolalpha << elements[i].used << std::endl;

In the constructor, instead of member-wise assignment, I've used initialization lists, which is better. In the printing code, I've used std::boolalpha for printing boolean as true or false instead of integers.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • This was a fantastic answer! Thank you very much for going out of your way to explain the problem the required the solution. – 137 Oct 12 '13 at 18:39
  • 1
    You're welcome. Since you're new to C++, from Java, I'd suggest you should pick up a beginner book from [The Definite C++ Book Guid and List](http://stackoverflow.com/q/388242/183120). I recommend [C++ Primer, 5th edition by Lippman](http://amzn.com/0321714113). Alternatively, [this article](http://www.horstmann.com/ccj2/ccjapp3.html) may help for a quick preliminary introduction, although it isn't updated for C++11. – legends2k Oct 12 '13 at 18:43
  • Thank you, my friend also recommended C++ Primer. I recently ordered [C Programming Language, 2nd Edition](http://www.amazon.com/gp/product/0131103628/ref=oh_details_o00_s00_i00?ie=UTF8&psc=1). I understand its not C++ but I thought it would give me a good foundation before I tackle C++. After C C++ is next on my hit list. – 137 Oct 12 '13 at 18:54
  • 1
    Although C Programming Language is a great book, it's only if you want to learn C. If your ultimate aim is to learn C++, then you needn't learn C. In a few areas, you've to unlearn a few things in C to write idiomatic C++ code; but I guess it depends on each person's learning style: some learn it bottom up, while others top-down. This [SO post](http://stackoverflow.com/q/598552/183120) and [this FAQ](http://www.parashift.com/c++-faq/dont-learn-c-first.html) will help you decide. – legends2k Oct 12 '13 at 18:58
  • My ultimate aim is to learn C and C++. I'm already pretty good at Java and C#. I'm extremely dependent on OOP and I'm really excited about learning a strictly imperative language. Plus I've read learning C teaches you a lot about how the computer works. – 137 Oct 12 '13 at 19:18
  • 1
    Oh then you can learn C as a procedural language and move on to C++ as a [multi-paradigm language](http://www.stroustrup.com/bs_faq.html#multiparadigm); you can do imperative, procedural, object-oriented, generic programming styles. Both're great languages in their own right. Happy learning :) – legends2k Oct 12 '13 at 19:25