compile c++ code with code blocks showed NAN but dev gave right answer
<p>When I compile the follow c++ code with Microsoft visual c++ 2010, it gave me right answer but with code blocks, it always said NAN. I have been using code blocks for a while, so I am quite get use to it, donot really want to change. do you have any idea why this is happening?</p>

<p>p.s. I also tried with other two computers to compile with code blocks, it showed the same result. </p>

<p>Thanks advance!</p>

<p>Selena</p>

<pre><code>#include "..\std_lib_facilities.h"

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';//t is a number token
const char name = 'a';


struct Token {
    char kind;
    double value;
    string name;
    Token(char ch)              :kind(ch),name(""),value(0) { cout &lt;&lt; "Token: '" &lt;&lt; ch &lt;&lt; "'" &lt;&lt; endl; }
    Token(char ch, double val)  :kind(ch),name(""),value(val) { cout &lt;&lt; "Token: '" &lt;&lt; ch &lt;&lt; ",' value: " &lt;&lt; val &lt;&lt; endl; }
    Token(char ch, string n)    :kind(ch),name(n),value(0) { cout &lt;&lt; "Token: '" &lt;&lt; ch &lt;&lt; ",' name: '" &lt;&lt; n &lt;&lt; "'" &lt;&lt; endl; }
};

class Token_stream {
public:
//construct
    Token_stream() :full(0), buffer(0) { }
    Token get();
    void unget(Token t) { buffer=t; full=true; }
    void ignore(char);
private:
    bool full;
    Token buffer;
};



Token Token_stream::get()
{
    if (full) {
        full=false;
        return buffer;
    }

    char ch;
    cin &gt;&gt; ch;

    switch (ch) {
        case '(':
        case ')':
        case '+':
        case '-':
        case '*':
        case '/':
        case '%':
        case ';':
        case '=':
            return Token(ch);
        case '.':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        {
            cin.unget();
            double val;
            cin &gt;&gt; val;
            return Token(number,val);
        }
        default:
            if (isalpha(ch)) {
                string s;
                s += ch;
                while(cin.get(ch) &amp;&amp; (isalpha(ch) || isdigit(ch)))
                    s+=ch;
                cin.unget();
                if (s == "let") return Token(let);
                if (s == "quit") return Token(quit);
                return Token(name,s);
            }
            error("Bad token");
    }
}


void Token_stream::ignore(char c)
{
    if (full &amp;&amp; c==buffer.kind) {
        full = false;
        return;
    }
    full = false;

    char ch;
    while (cin&gt;&gt;ch)
        if (ch==c) return;
}

struct Variable {
    string name;
    double value;
    Variable(string n, double v) :name(n), value(v) { }
};

vector&lt;Variable&gt; names;



double get_value(string s)
{
    for (int i = 0; i&lt; (int) names.size(); ++i)
        if (names[i].name == s) return names[i].value;
    error("get: undefined name ",s);
}


void set_value(string s, double d)
{
    for (int i = 0; i&lt;= (int) names.size(); ++i)
        if (names[i].name == s) {
            names[i].value = d;
            return;
        }
    error("set: undefined name ",s);
}

bool is_declared(string s)
{
    for (int i = 0; i&lt;(int)names.size(); ++i)
        if (names[i].name == s) return true;
    return false;
}

Token_stream ts;

double expression();

double primary()
{
    Token t = ts.get();
    switch (t.kind) {
    case '(':
    {
        double d = expression();
        t = ts.get();
        if (t.kind != ')')
            error("'(' expected");
        return d;
    }
    case '-':
        return - primary();
    case number:
    {
        cout &lt;&lt; "Primary: number: " &lt;&lt; t.value &lt;&lt; endl;
        return t.value;
    }
    case name:
        return get_value(t.name);
    default:
        error("primary expected");
    }
}

double term()
{
    double left = primary();

    while(true) {
        Token t = ts.get();
        switch(t.kind) {
        case '*':
            left *= primary();
            break;
        case '/':
        {    double d = primary();
            if (d == 0) error("divide by zero");
            left /= d;
            break;
        }
        default:
            ts.unget(t);
            return left;
        }
    }
}
double expression()
{
    double left = term();
    while(true) {
        Token t = ts.get();
        switch(t.kind) {
        case '+':
            left += term();
            break;
        case '-':
            left -= term();
            break;
        default:
            ts.unget(t);
            return left;
        }
    }
}

double declaration()
{
    Token t = ts.get();
    if (t.kind != 'a') error ("name expected in declaration");
    string name = t.name;
    if (is_declared(name)) error(name, " declared twice");
    Token t2 = ts.get();
    if (t2.kind != '=') error("= missing in declaration of " ,name);
    double d = expression();
    names.push_back(Variable(name,d));
    return d;
}

double statement()
{
    Token t = ts.get();
    switch(t.kind) {
    case let:
        return declaration();
    default:
        ts.unget(t);
        return expression();
    }
}

void clean_up_mess()
{
    ts.ignore(print);
}


const string prompt = "&gt; ";
const string result = "= ";

void calculate()
{
    while(true) try {
        cout &lt;&lt; prompt;
        Token t = ts.get();
        while (t.kind == print) t=ts.get();
        if (t.kind == quit) return;
        ts.unget(t);
        cout &lt;&lt; result &lt;&lt; statement() &lt;&lt; endl;
    }
    catch(runtime_error&amp; e) {
        cerr &lt;&lt; e.what() &lt;&lt; endl;
        clean_up_mess();
    }
}


int main()
{
    try {
        cout &lt;&lt; "Calculator is online." &lt;&lt; endl;
        calculate();
        keep_window_open();
        return 0;
    }
    catch (exception&amp; e) {
        cerr &lt;&lt; "exception: " &lt;&lt; e.what() &lt;&lt; endl;
        char c;
        while (cin &gt;&gt;c&amp;&amp; c!=';') ;
        return 1;
    }
    catch (...) {
        cerr &lt;&lt; "exception\n";
        char c;
        while (cin&gt;&gt;c &amp;&amp; c!=';');
        return 2;
    }
}
</code></pre>
