#include #include #include #include typedef unsigned char byte; // objects of this type are going to be transfered directly // to and from a file with fwrite and fread, so they must // not contain pointers, therefore no C++ strings. // and I might have a lot of data, so I want it compressed, // using bytes and short ints whenever they are big enough. struct pet { char name[16], type[16]; // char sex; int SSN; short int length, weight, birth_year; byte birth_month, birth_date; byte num_legs, num_eyes, num_fins; // but that doesn't mean I can't make use of the convenience // of C++ strings in the constructor for setting them up. pet(string NM, string TY, int S, int L, int W, int BY, int BM, int BD, char SX, int NL, int NE, int NF) { for (int i=0; i<16; i+=1) { if (i < NM.length()) name[i] = NM[i]; else // don't want random junk when string < 16 chars name[i] = 0; } for (int i=0; i<16; i+=1) { if (i < TY.length()) type[i] = TY[i]; else type[i] = 0; } SSN = S; length = L; weight = W; birth_year = BY; birth_month = BM; birth_date = BD; // sex = SX; num_legs = NL; num_eyes = NE; num_fins = NF; } pet() { } void print() { cout << name << " the " << type << ", SSN " << SSN << /* ", Sex " << sex << */ ", born " << birth_year << "-" << (int) birth_month << "-" << (int) birth_date << "\n"; cout << " " << length << " inches long, " << weight << " pounds, " << (int) num_legs << " legs, " << (int) num_eyes << " eyes, " << (int) num_fins << " fins\n"; } }; pet * * setup() { pet * * array = new pet * [11]; array[0] = new pet("Tom", "cat", 635287163, 28, 9, 2004, 11, 3, 'M', 4, 2, 0); array[1] = new pet("Stumpy", "dog", 362283673, 24, 12, 2003, 7, 4, 'M', 3, 2, 0); array[2] = new pet("Goldie", "goldfish", 733283721, 2, 1, 2011, 12, 14, 'F', 0, 2, 5); array[3] = new pet("Orangey", "goldfish", 273381721, 2, 1, 2012, 3, 28, 'M', 0, 2, 5); array[4] = new pet("Millie", "dog", 131231912, 45, 67, 2006, 10, 10, 'F', 4, 2, 0); array[5] = new pet("Fluffy", "elephant", 936253176, 110, 3266, 1997, 6, 13, 'F', 4, 2, 0); array[6] = new pet("Jimbo", "snake", 236283931, 175, 41, 2007, 9, 25, 'M', 0, 2, 0); array[7] = new pet("Polly", "parrot", 538901046, 10, 3, 1975, 5, 31, 'F', 2, 2, 0); array[8] = new pet("Pugh", "conch", 065172614, 12, 6, 1999, 4, 17, 'M', 0, 0, 0); array[9] = new pet("Hammy", "hamster", 185782394, 4, 1, 2010, 12, 15, 'F', 4, 2, 0); array[10] = NULL; return array; } void main() { cout << "size of pet is " << sizeof(pet) << "\n"; pet * * pets = setup(); int num; for (int i = 0; true; i += 1) if (pets[i] == NULL) { num = i; break; } for (int i = 0; i < num; i += 1) pets[i]->print(); }