//******************************************************************************
//
// CS 240C : Spring 2003 Ohio University Travis Dillon
// Project 3 : Animal Homes
// file : prog2.cc
// started : 05-07-03
//
//******************************************************************************
#include "prog3.h"
void print_out(vector <Animal*> homes); //output all homes to screen
void get_data(vector <Animal*>& homes); //read in from file all the homes
void print_data(vector <Animal*> homes); //output all homes to file
int main()
{
vector <Animal*> homes; //dynamic array of pointers to animals
Animal* temp; //temperary pointer for the new animal
char ans; //y or Y to continue the do while loop
char choice; //choice in the menu of animal or print out
get_data(homes); //read in from file all the homes ordered
do
{
cout << "\nChoose an animal to buy a home for, or view homes ordered\n"
<< "1. Bat\n"
<< "2. Bear\n"
<< "3. Bobcat\n"
<< "4. Groundhog\n"
<< "5. Ring-Tailed Lemur\n"
<< "6. Penguin\n"
<< "7. View all homes ordered\n"
<< " : ";
cin >> choice;
switch(choice)
{ //1 - 6 make a new pointer to an Animal, 7 prints out all homes
case '1': temp = new Bat; break;
case '2': temp = new Bear; break;
case '3': temp = new Bobcat; break;
case '4': temp = new Groundhog; break;
case '5': temp = new Lemur; break;
case '6': temp = new Penguin; break;
case '7': print_out(homes); break;
default: cout << "Invalid choice for animal home."; break;
}
if((choice >= '1') && (choice <= '6'))
{ //1 - 6 calls the input funtion that gets the data from the user
(*temp).input();
homes.push_back(temp); //insert into dynamic array
}
cout << endl <<"\nDo you want to continue? (y/Y)es or (n/N)o: ";
cin >> ans; //only continue if 'y' or 'Y', exit on anything else
}while((ans == 'y') || (ans == 'Y'));
print_data(homes); //output all homes in array to a file
return EXIT_SUCCESS;
}
void print_out(vector <Animal*> homes)
{
vector <Animal*>::iterator iter; //iterator that runs the vector
for(iter = homes.begin(); iter != homes.end(); ++iter)
{
(*iter) -> output(); //calls the output function for each child
}
}
void get_data(vector <Animal*>& homes)
{
Animal* temp;
ifstream fin;
fin.open("homes.txt"); //input file with all homes ordered
int id_num, a, b, c, d; //ints for data of each child
while(!fin.eof())
{
fin >> id_num >> a >> b >> c >> d; //each child has same # of variables
switch(id_num)
{ //call constructor to create child
case 1: temp = new Bat(a,b,c,d); break;
case 2: temp = new Bear(a, b, c, d); break;
case 3: temp = new Bobcat(a, b, c, d); break;
case 4: temp = new Groundhog(a, b, c, d); break;
case 5: temp = new Lemur(a, b, c, d); break;
case 6: temp = new Penguin(a, b, c, d); break;
default:cout<<"homes.txt is empty"; break;
}
homes.push_back(temp); //insert temp into vector
}
homes.pop_back(); //must do this for my prog to work
fin.close();
}
void print_data(vector <Animal*> homes)
{
ofstream fout; //output file
fout.open("homes.txt"); //output all data which will be read in next run
vector <Animal*>::iterator iter;
for(iter = homes.begin(); iter != homes.end(); ++iter)
{
fout << (*iter) -> get_id() << endl; //i guess i didn't need this
(*iter) -> print(fout); //output to file all child variables
}
fout.close();
}