C++ question, converting a list of string to record object

Discussion in 'OT Technology' started by MP, Jun 20, 2004.

  1. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    This is the first time I've dived into record objects so I was wondering what the best way to convert a list of strings to a list of record objects would be.

    here's some sample code I'm working with:

    Code:
    
    const string nullObject = ""; //This is a special value to indicate an empty location
    
    class simpleList  {
      int maxSize, currSize;
      string* List;
      
    public:
      simpleList();          
      simpleList(int);
      
      void addValue(string);
      void delValue(string);
      string searchValue(string);
      void Compact();
      void showAll();
    };
    
    //The default constructor...
    
    simpleList::simpleList()
    {
      currSize = 0;
      maxSize  = 25;
      List = new string[maxSize];
    }
    
    //The initializing constructor...
    
    simpleList::simpleList(int Size)
    {
      currSize = 0;
      maxSize  = Size;
      List = new string[maxSize];
    }
    
    //This add a single value to the end of the list...
    
    void simpleList::addValue(string V)
    {
      if( currSize < maxSize) {
        List[currSize] = V;
        currSize++;
        return;
       }
      cout << "List Is Full" << endl;
    }
    
    //This removes a single value from the list...
    
    void simpleList::delValue(string V)
    {
       for(int K = 0 ; K < currSize ; K++)
          if( (List[K] != nullObject) && (List[K] == V) ) {
             List[K] = nullObject;
             return;
           }
       cout << V << " Not Found" << endl;
    }
    
    //This allows the user to search for a single value..
    
    string simpleList::searchValue(string V)
    {
       for(int K = 0 ; K < currSize ; K++)
         if( (List[K] != nullObject) && (List[K] == V) )
           return List[K];
           
       return V + " Not Found";
    }
    
    //This push empty locations to the end of the list and ajusts
    //     the currSize variable accordingly...
    
    void simpleList::Compact()
    {
      string* tempList;
      int J = 0;
      
      tempList = new string[maxSize];
      
      for(int K = 0 ; K < maxSize ; K++) 
        if(List[K] != nullObject) {
          tempList[J] = List[K];
          J++;
         }
       
       delete[] List;
       List = tempList;
       currSize = J;
    }
    
    //This displays the entire list...
    
    void simpleList::showAll()
    {
       for(int K = 0 ; K < currSize ; K++)
         cout << "List[" << K << "] = " << List[K] << endl;
    }
    
    //This class defines the user interface...
    
    class Final {
      simpleList Collection;
      
    public:
      Final() { RunIT(); }  //All we need to do is jump start the program...
      
      void RunIT();
      
      void Menu();
      void AddValues();
      void RemoveValues();
      void SearchForValues();
      void CompactList()      { Collection.Compact(); }
      void DisplayAllValues() { Collection.showAll(); }
    };
    
    //This is the command processor...
    
    void Final::RunIT()
    {
       string Cmd;
       
       for( ; ; ) {
    
          Menu();
          cout << "Command: ";
          getline(cin, Cmd);
          
          if(Cmd == "Quit")
            break;
          else if(Cmd == "Add")
            AddValues();
          else if(Cmd == "Delete")
            RemoveValues();
          else if(Cmd == "Search")
            SearchForValues();
          else if(Cmd == "Compact")
            CompactList();
          else if(Cmd == "Display")
            DisplayAllValues();
          else
            cout << "Not A Known Command" << endl;
        }
       
       cout << "Session is shutting down..." << endl;
    }
    
    //...obvious...
    
    void Final::Menu()
    {
       cout << "Choices---------------------" << endl;
       cout << "\tAdd Values" << endl;
       cout << "\tDelete Values" << endl;
       cout << "\tSearch For Values" << endl;
       cout << "\tCompact The List" << endl;
       cout << "\tDisplay The List" << endl;
       cout << "\tQuit" << endl;
       cout << "----------------------------" << endl;
    }
    
    //This is the add interface...
    
    void Final::AddValues()
    {
       string newValue;
       
       cout << "Entering Add Values..." << endl;
       cout << "Enter Values Below, \"Stop\" To Quit" << endl;
       
       for(;;) {
         cout << "\tNew Value: ";
         getline(cin, newValue);
        
         if(newValue == "Stop")
           break;
           
         Collection.addValue(newValue);
        }
       cout << "Exiting Add Values...." << endl;
    }
      
    //...the delete interface...
     
    void Final::RemoveValues()
    {
       string newValue;
       
       cout << "Entering Remove Values..." << endl;
       cout << "Enter Values Below, \"Stop\" To Quit" << endl;
       
       for(;;) {
         cout << "\t Value: ";
         getline(cin, newValue);
             
         if(newValue == "Stop")
           break;
           
         Collection.delValue(newValue);
        }
       cout << "Exiting Remove Values...." << endl;
    }
      
    //...the search interface...
      
    void Final::SearchForValues()
    {
       string newValue;
       
       cout << "Entering Search For Values..." << endl;
       cout << "Enter Values Below, \"Stop\" To Quit" << endl;
       
       for(;;) {
         cout << "\tSearch For: ";
         getline(cin, newValue);
             
         if(newValue == "Stop")
           break;
           
         cout << "Result: " << Collection.searchValue(newValue) << endl;
        }
       cout << "Exiting Searc For Values...." << endl;
    }
    
    
    int main()
    {
      Final X;
    }       
    
    

    I want to leave some of the strings in for usability, but for the most part I'm trying to convert the list of strings to a list of records objects. I'm blind when it comes to record objects unfortunatly. Any help would be great.
     
  2. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    bump for a slight chance I'll get a little help
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Perhaps you should explain what a "record object" is?
     
  4. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    database
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    :dunno: you still haven't explained what is is. Do you have your own record object built?
     
  6. MP

    MP New Member

    Joined:
    Sep 10, 2002
    Messages:
    34,377
    Likes Received:
    0
    Location:
    Silicon Valley
    n/m I can't explain it well enough.
     
  7. irichie

    irichie New Member

    Joined:
    Jun 5, 2002
    Messages:
    1,297
    Likes Received:
    0
    Location:
    Los Angeles

Share This Page