Urgent help in code...

This is a discussion on Urgent help in code... within the C++ Programming forums, part of the General Programming Boards category; my assignment is due tomorrow...this is the code for classification...it basically reads file with a tree in it...everything works perfectly...the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Exclamation Urgent help in code...

    my assignment is due tomorrow...this is the code for classification...it basically reads file with a tree in it...everything works perfectly...the algo. i.e. except that the return value from the function is not properly outputted to the screen...i made a post about this earlier...this is hoping that someone can try to run this entire code and figure out what could be wrong...

    Code:
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    
    #include <string>
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <math.h>
    #include <map>
    #include <sstream>
    #include <algorithm>
    
    using namespace std;
    
    const int MAX_STRING = 256;               // The maximum length of any string.
    static char dimensions[MAX_STRING][MAX_STRING];
    static char testdata[MAX_STRING][MAX_STRING];
    
    int num_dimensions = 0;                   //the number of dimensions read in
    string root = "";                         //will store the name of root node
    
    class TreeNode
    {
    public:
    	TreeNode();
    	virtual ~TreeNode();
    	int NodeID;
    	string name;
            map <string, string> ClassMap;    //MAP of attributes and class names
            map <string, string> NodeMap;     //MAP of attributes and node names
    };
    
    TreeNode::TreeNode()
    {
       NodeID = 0;
       name = "";
    }
    
    TreeNode::~TreeNode()
    {
    
    }
    
    class Decision {
    public:
    
            Decision(char *filename);          // Construct a Decision given an info file
            virtual ~Decision();               // Destroy the decision tree
    
            string classify(string tag);       // Classify examples based on the decision tree
            
    };
    
    
    map <string, TreeNode *> treenodes;        //map of treenodes with name of node as key
    
    Decision::Decision(char *filename)
    {
            int x = 0;
            string temp;
            string temp1;                      //should contain attribute name
            string temp2;                      //should contain either null or class name
            char word[MAX_STRING];             // Some scratch space for reading in words
    
            ifstream in(filename);
            streampos position;
    
            if(in.fail()) {
               cerr << "Could not open file " << filename << endl;
               exit(1);
            }
    
            in >> num_dimensions;              //first word is the number of dimensions
    
            in.getline(word, MAX_STRING);
    
            istringstream parser(word);
            while(parser >> temp)
            {
                strcpy(dimensions[x], temp.c_str());
                //cout << "(" << dimensions[x] << ")";
                x++;
            }
    
            x = 0;
    
            //cout << '\n';
    
            in >> word;
    
            TreeNode *treenode = new TreeNode();
            treenode->name = word;
            root = word;
    
            for (int i=0; i<num_dimensions; i++)
            {
                    if (!strcmp(dimensions[i],word))
                    {
                            treenode->NodeID = i;
                            //cout << treenode->NodeID << '\n';
                            break;
                    }
            }
    
            temp1 = "";
            temp2 = "";
            x = 0;
    
            //cout << '\n';
    
            in.getline(word,MAX_STRING);
            istringstream parser2(word);
            while(parser2 >> temp)
            {
                    if (x == 1)                   //word after =
                    {
                            temp1 = temp;
                    }
    
                    if (x == 3)                   //word after :
                    {
                            temp2 = temp;
                    }
                    x++;
            }
    
            //cout << temp1 << temp2 << '\n';
    
            if (temp2 != "")                      //if there is a string in temp2, means that we have a class in temp1
            {
                    treenode->ClassMap.insert(make_pair(temp1,temp2));
            }
    
            if (temp2 == "")                      //if there is nothing in temp2, means that we have a newline
            {
                    position = in.tellg();
                    in >> word;
                    in.seekg(position);
                    treenode->NodeMap.insert(make_pair(temp1,word));
            }
    
            treenodes.insert(make_pair(treenode->name, treenode));    //push treenode onto the allnodes map
    
            temp1 = "";
            temp2 = "";
    
            in >> word;
            while (!in.eof())
            {       
                    map <string, TreeNode *>::iterator j = treenodes.find(word);
    
                    if (j == treenodes.end())                  //word not found
                    {       //cout << "New: " << word << '\n';
                            x = 0;
                            temp1 = "";
                            temp2 = "";
    
                            TreeNode *treenode = new TreeNode();
                            treenode->name = word;
    
                            for (int i=0; i<num_dimensions; i++)
                            {
                                    if (!strcmp(dimensions[i],word))
                                    {
                                            treenode->NodeID = i;
                                            break;
                                    }
                            }
                            
                            in.getline(word,MAX_STRING);
                            istringstream parser(word);
    
                            while(parser >> temp)
                            {
                                    if (x == 1)
                                    {
                                            temp1 = temp;
                                    }
    
                                    if (x == 3)
                                    {
                                            temp2 = temp;
                                    }
    
                                    x++;
                            }
    
                            if (temp2 != "")
                            {
                                    treenode->ClassMap.insert(make_pair(temp1,temp2));
                            }
    
                            if (temp2 == "")
                            {
                                    position = in.tellg();
                                    in >> word;
                                    in.seekg(position);
                                    treenode->NodeMap.insert(make_pair(temp1,word));
                            }
    
                            treenodes.insert(make_pair(treenode->name, treenode));
    
                    }
                    else
                    {       //cout << "Exists: " << word << '\n';
                            x = 0;
                            temp1 = "";
                            temp2 = "";
                            in.getline(word,MAX_STRING);
                            istringstream parser(word);
    
                            while(parser >> temp)
                            {
                                    if (x == 1)
                                    {
                                            temp1 = temp;
                                    }
    
                                    if (x == 3)
                                    {
                                            temp2 = temp;
                                    }
    
                                    x++;
                            }
    
                            if (temp2 != "")
                            {
                                    (j->second)->ClassMap.insert(make_pair(temp1,temp2));
                            }
    
                            if (temp2 == "")
                            {
                                    position = in.tellg();
                                    in >> word;
                                    in.seekg(position);
                                    (j->second)->NodeMap.insert(make_pair(temp1,word));
                            }
    
                    }
    
                    in >> word;
    
            }              
    
            in.close();                         //close the stream
    
        //    for(map < string, TreeNode * >::iterator j  = treenodes.begin(); j != treenodes.end(); j++)
        //    {
        //            cout << (j->second)->name << ",";
        //            cout << (j->second)->NodeID << '\n';
        //    }
    
         //     for(map < string, TreeNode * >::iterator j  = treenodes.begin(); j != treenodes.end(); j++)
         //     {
         //           for(map < string, string >::iterator k  = ((j->second)->NodeMap).begin(); k != ((j->second)->NodeMap).end(); k++)
         //           {
         //                   cout << (k->first) << " " << (k->second) << "\n";
         //           }
         //     }
    
    
    }
    
    Decision::~Decision()
    {
    
    }
    
    string Decision::classify(string treenodename)
    {
            map <string, TreeNode *>::iterator j = treenodes.find(treenodename);
    
            map <string, string>::iterator k = (j->second)->ClassMap.find(testdata[(j->second)->NodeID]);
    
            if( k == (j->second)->ClassMap.end() )           
            {
                    map <string, string>::iterator l = (j->second)->NodeMap.find(testdata[(j->second)->NodeID]);
                    //cout << l->first << " " << l->second << '\n';
                    classify(l->second);                      //recursive call for next node along branch
            }
            else
            {       //cout << k->first << " " << k->second << '\n';
                    return (k->second);                       //return the class name
            }
    
    }
    
    int main(int argc, char* argv[])
    {
            int x = 0;
            char word[MAX_STRING];
            string temp;
            
            //if(argc != 2) {
            //   cerr << "Usage:  decision myfile.example" << endl;
            //   exit(1);
            //}
    
    
            //Decision decision(argv[1]);
            Decision decision("outputfile.txt");
    
            ifstream in("test_data.txt");
    
            ofstream fout;                          //to write out to an output file
            fout.open ("testoutput.txt");       
    
            while (!in.eof())
            {
                    in.getline(word, MAX_STRING);
                    replace(word,word + MAX_STRING,',',' ');
    
                    istringstream parser(word);
                    while(parser >> temp)
                    {
                            strcpy(testdata[x], temp.c_str());
                            //cout << testdata[x];
                            x++;
                    }
                    //cout << "X: " << x;
                    x = 0;
                    cout << '\n';
                    fout << decision.classify(root) << endl;
                   // cout << decision.classify(root) << endl;
            }
    
    
            fout.flush();
            fout.close();
    
            in.close(); 
    
            std::cin.get();
    
    	return 0;
    
    }
    //---------------------------------------------------------------------------
    Treefile: (called outputfile.txt)
    Code:
    10 alt bar fri hun pat price rain res type est
    pat = none : NO
    pat = some : YES
    pat = full : 
       hun = yes : 
          type = burger : YES
          type = french : NO
          type = italian : NO
          type = thai : 
             fri = yes : YES
             fri = no : NO
       hun = no : NO
    Testfile: (called test_data.txt)
    Code:
    no,no,yes,no,some,high,no,yes,french,10-30
    no,no,no,no,full,high,no,yes,italian,>60
    no,no,yes,yes,full,high,no,yes,thai,>60
    the output to the screen (or to a file) should be:
    Code:
    YES
    NO
    YES
    you can confirm this from the cout statements in the classify function...(these are the outputs...just that main is not printing them right...
    thanks a lot in advance,

    Regards,

    Farooq
    Last edited by alvifarooq; 11-07-2004 at 04:58 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    interestingly...if i remove the else {} and i just retain the return statement there i don't get the error...actually it shouldn't make a difference to my code if i remove else since the recursive calls will all complete before it gets to the return statement...

    but even then i can't output the contents to file...i only get the first answer YES and then 2 newlines...

    Farooq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21