Jump to content

Ambiguous code?

Golden

Hello all,

I'm working on this program that takes a text file and reads it like a programming language. Whenever the program reads the VAR keyword, it takes the variable name and hashes it to a table. The hash function takes the ascii character values and multiplies it with an iterator starting at 1. Then new variable is added to the table after taking its molulo with the table size. The logic is mostly there but the hash function says its ambiguous. I'm not sur what the problem is. Looking around the internet first, I think the problem is with the scope in which the function is called. Any help is appreciated. Thanks!

Best, Golden
 

 #include <iostream>#include <string>#include <sstream>#include <ctype.h>#include <fstream>#include <math.h>using namespace std;class node{public:    node();    node* next;    string getName();    int getNum();    int getScope();    void setName(string);    void setNum(int);    void setScope(int);private:    string name;    int num;    int scope;};node::node(){    next = 0;    name = "";    num = 0;    scope = 0;}string node::getName(){    return name;}int node::getNum(){    return num;}int node::getScope(){    return scope;}void node::setName(string s){    name = s;}void node::setScope(int x){    scope = x;}void node::setNum(int x){    num = x;}int hash(string s){    int hash=0,size = s.size();    for(int i=;i<size+1;i++){        hash+= (int) s[i] * i;    }    return hash%7;}int main(){    int curr_scope=0,num_line=0;    node table[7];         ifstream myfile ("C:\\Users\\Tony\\SkyDrive\\School\\DataStructures\\Programs\\Program4\\BORG\\borg.txt");     if (myfile.is_open()){    string s;          while (getline (myfile,s)){            num_line++;              stringstream line(s);            line >> s;            if(s == "START"){                curr_scope++;                while(line >> s);            }            else if(s == "FINISH"){                curr_scope--;                while(line >> s);            }            else if(s == "COM"){                while(line >> s);            }            else if(s == "VAR"){                node temp;                line >> s;                cout << s << endl;                temp.setName(s);                line >> s;                if(s == "="){                    line >> s;                    cout << s << endl;                    temp.setNum(atoi(s.c_str()));                    temp.setScope(curr_scope);                    if(table[hash(temp.getName())] != 0){                        temp.next = table[hash(temp.getName())];                        table[hash(temp.getName())] = temp;                        while(line >> s);                    }                    else if(table[hash(temp.getName())] == 0){                        table[hash(temp.getName())] = temp;                        while(line >> s);                    }                    else{                        cout << "UNABLE TO ADD " << temp.getName() << "TO THE TABLE" << endl;                        while(line >> s);                    }                }            }            else if(s == "PRINT"){                line >> s;                node temp = table[hash(s)];                if(temp.getScope == curr_scope){                    if(line >> s){                        if(s == "++"){                            cout << temp.getName() << " IS " << temp.getNum() + 1 << endl;                            while(line >> s);                        }                        else if(s == "--"){                            cout << temp.getName() << " IS " << temp.getNum() - 1 << endl;                            while(line >> s);                        }                        else if(s == "+"){                            line >> s;                            cout << temp.getName() << " IS " << temp.getNum() + atoi(s.c_str()) << endl;                            while(line >> s);                        }                        else if(s == "-"){                            line >> s;                            cout << temp.getName() << " IS " << temp.getNum() - atoi(s.c_str()) << endl;                            while(line >> s);                        }                        else if(s == "/"){                            line >> s;                            cout << temp.getName() << " IS " << temp.getNum() / atoi(s.c_str()) << endl;                            while(line >> s);                        }                        else if(s == "*"){                            line >> s;                            cout << temp.getName() << " IS " << temp.getNum() * atoi(s.c_str()) << endl;                            while(line >> s);                        }                        else if(s == "%"){                            line >> s;                            cout << temp.getName() << " IS " << temp.getNum() % atoi(s.c_str()) << endl;                            while(line >> s);                        }                        else if(s == "^"){                            line >> s;                            cout << temp.getName() << " IS " << pow(temp.getNum(),atoi(s.c_str())) << endl;                            while(line >> s);                        }                    }                }                else{                    cout << s << "IS UNDEFINED" << endl;                    cout << "ERROR HAS OCCURED ON LINE " << num_line << endl;                    while(line >> s);                }            }            else{                if(table[hash(s)].getName == s){                    node temp = table[hash(s)];                    line >> s;                    if(s == "="){                        if(temp.getScope() == curr_scope){                            line >> s;                            table[hash(temp)].setNum(atoi(s.c_str()));                            while(line >> s);                        }                    }                    else if(s == "++"){                        table[hash(temp)].setNum(table[hash(temp)].getNum()+1);                        while(line >> s);                    }                    else if(s == "--"){                        table[hash(temp)].setNum(table[hash(temp)].getNum()-1);                        while(line >> s);                    }                }                else                    cout << s << "IS UNDEFINED" << endl;            }      }    myfile.close();    }    system("PAUSE");    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

The problem may be that there is already a "hash" defined in the std namespace. It's best not to use "using namespace std;" but actually prefix anything that needs it with std::

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×