Asg 0: List of data_t objects

Objectives

To implement a data_t object, read a file, and add data_t objects to a list (std::vector).

Assignment

  1. Write C++ code to define a data_t class (object) that will hold the following two data members:
  2. Create two files: data.h that holds the object interface and data.cpp that holds the object implemantation.
  3. The main.cpp test driver program (see below) reads a data file (redirected from std::cin), creates a data_t object from each pair of string and numbers, and adds them to a list. It then prints them out and deletes the list. Once your program successfully compiles (using the Makefile provided below), you should be able to test the program by executing this command on the command line:

Details

  1. Your data_t object should provide the following:

Requirements

  1. Your code should be such that the following main() routine works unaltered:
    #include <iostream>
    #include <string>
    #include <vector>
    
    #include "data.h"
    
    int main(int argc, char *argv[]);
    
    int main(int argc, char *argv[])
    {
            data_t                         data;
            std::vector<data_t>            records;
            std::vector<data_t>::iterator  itr;
            std::string                    name;
            int                            num;
    
      // read input file consisting of names and id codes adding entities to list
      while(!std::cin.eof()) {
        std::cin >> name >> num;
        if(std::cin.good()) {
          data.setName(name);
          data.setId(num);
    //    std::cout << data;
          records.push_back(data);
        }
      }
    
      // now play it back
      std::cout << "*** printing records" << std::endl;
    
      for(int i=0; i<records.size(); i++) {
    //  std::cout << records[i] << std::endl;
        std::cout << records[i].getName() << " " << records[i].getId() << std::endl;
    
      }
    
      // try it again...
      std::cout << "*** printing records again" << std::endl;
    
      for(itr = records.begin(); itr != records.end(); ++itr) {
    //  data = *itr;
    //  std::cout << data.getName() << " " << data.getId() << std::endl;
        std::cout << itr->getName() << " " << itr->getId() << std::endl;
      }
    
      // clear list via STL clear() function (memory leak if data_t used new!!)
      std::cout << "*** deleting records" << std::endl;
      records.clear();
    
      // nope ... see if we can delete an empty list
      std::cout << "*** deleting records again" << std::endl;
      records.clear();
    
      // prove we survived
      std::cout << "*** done" << std::endl;
    }
    	

Input

  1. The listdata.txt file used to test the above program is:
    Mike    1234
    Bill    3212
    Sarah   1321
    John    1021
    Carol   3223
    Debbie  4231
    Gary    9321
    Ann     1231
    Dale    7231
    Lynn    8133
    Patty   9999
    	

Output

  1. Expected output with the above input file is:
    Mike 1234
    Bill 3212
    Sarah 1321
    John 1021
    Carol 3223
    Debbie 4231
    Gary 9321
    Ann 1231
    Dale 7231
    Lynn 8133
    Patty 9999
    *** printing records
    Mike 1234
    Bill 3212
    Sarah 1321
    John 1021
    Carol 3223
    Debbie 4231
    Gary 9321
    Ann 1231
    Dale 7231
    Lynn 8133
    Patty 9999
    *** printing records again
    Mike 1234
    Bill 3212
    Sarah 1321
    John 1021
    Carol 3223
    Debbie 4231
    Gary 9321
    Ann 1231
    Dale 7231
    Lynn 8133
    Patty 9999
    *** deleting records
    *** initializing new records
    *** deleting records again
    *** done
    	

Supplemental

  1. Here is a Makefile that you can use to compile the project:
    .SUFFIXES: .c .o .cpp .cc .cxx .C
    
    UNAME = $(shell uname)
    PLATFORM = $(shell uname -p)
    
    CC = g++
    COPTS = -g -Wall
    
    INCDIR =
    LIBDIR =
    
    ifeq ("$(shell uname)", "Linux")
    LIBS = -lm
    else
    ifeq ("$(shell uname)", "Darwin")
    LIBS = -framework Foundation -lstdc++
    endif
    endif
    
    .c.o:
    	$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
    
    .cpp.o:
    	$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
    
    all : main
    
    OBJECTS = \
    data.o
    
    main : $(OBJECTS) main.o
    	$(CC) -o $@ $(INCDIR) $(COPTS) $(OBJECTS) $@.o $(LIBDIR) $(LIBS)
    
    data.o: data.h data.cpp
    
    clean :
    	rm -f *.o
    	rm -rf main main.app
    	

Turn in

Turn in all of your code, in one tar.gz archive of your asg##/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    4. Lessons learned, identified interesting features of your program
    5. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .cpp source)
  4. object code (do a make clean before tar)

How to hand in

See handin notes

Grading scheme