/* ********************************** * my first ever c++ game * title: Pet Keeper description:manage a group of pets date start: 16/08/2009 date finish: copywrite: none company: Gazelle gaming programmers: MatthewB; confidential: 0/10 average rating: 0% (0 reviews) type: open source console application genre: content rating: 3+ difficulty rating: ***********************************/ #include //to interact with the console #include //to use the string variable #include //to work with files #include //to use the vector dynamic array #include //to genarate random numbers #include //to use the time as the random number seed class Pet { public: //constructor Pet(std::string Name, unsigned long Age, char Gender, std::string Species) { Name = this->Name; Age = this->Age; Gender = this->Gender; Species = this->Species; } //desructor ~Pet() {} //attributes std::string Name; unsigned long Age; char Gender; std::string Species; void Edit(unsigned short x) { cls(); std::cout<< "\n" << "press 1: to edit the Name: " << Pets[x].Name << "\n" << "press 2: to edit the Age: " << Pets[x].Age << "\n" << "press 3: to edit the Gender: " << Pets[x].Gender << "\n" << "press 4: to edit the species: " << Pets[x].Species << "\n\n" << "or press 5: to edit everythig\n"; char EditSelection; std::cin>> EditSelection; if(!std::cin.good()) { std::cin.clear(); delete &EditSelection; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); EditPet(); } switch(EditSelection) { case '1': cls(); std::cout<< "\n" << "Enter the pets name:"; std::cin >> Pets[x].Name; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } break; case '2': cls(); std::cout<< "Enter the pets age:"; std::cin >> Pets[x].Age; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } break; case '3': cls(); std::cout<< "Enter the pets gender (M/F):"; std::cin >> Pets[x].Gender; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } switch(Pets[x].Gender) { case 'm': Pets[x].Gender = 'M'; break; case 'f': Pets[x].Gender = 'F'; break; case 'M': case 'F': break; default: cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); break; } break; case '4': cls(); std::cout<< "Enter the pets species:"; std::cin >> Pets[x].Species; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } break; case '5': cls(); std::cout<< "\n" << "Enter the pets name:"; std::cin >> Pets[x].Name; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } std::cout<< "Enter the pets age:"; std::cin >> Pets[x].Age; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } std::cout<< "Enter the pets gender (M/F):"; std::cin >> Pets[x].Gender; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } switch(Pets[x].Gender) { case 'm': Pets[x].Gender = 'M'; break; case 'f': Pets[x].Gender = 'F'; break; case 'M': case 'F': break; default: cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); break; } std::cout<< "Enter the pets species:"; std::cin >> Pets[x].Species; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditPet(); } break; default: cls(); std::cout<< "the input is invalid \n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); break; } cls(); std::cout<< "Pet sucsessfully Edited.\n" << "the new data is:\n\n" << "Name: " << Pets[x].Name << "\n" << "Age: " << Pets[x].Age << "\n" << "Gender: " << Pets[x].Gender << "\n" << "Species: " << Pets[x].Species << "\n\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } }; class Activity { public: //constructor Activity(std::string Text, std::string Species, bool ForAll) { Text = this->Text; Species = this->Species; ForAll = this->ForAll } //destructor ~Actvity() {} //attributes std::string Text; std::string Species; bool ForAll; }; /* struct pet{ std::string Name; unsigned long Age; char Gender; std::string Species; }; */ struct Activity{ std::string Text; std::string Species; bool ForAll; }; //declare global variables std::vector Pets; std::vector Activities; unsigned long GameTime; //declare function prototypes void Save(void); void Load(void); void AddPet(void); void AddActivity(void); void EditActivity(void); void ListPets(void); void ListActivities(void); void DisplayMenu(void); void DisplayHelp(void); void DeleteMenu(void); void cls(void); void EmptyBuffer(void); void Sleep(unsigned int mseconds); void RunSimulation(void); int main(void) { DisplayMenu(); return 0; } //functions //pet functions void AddPet(void) { cls(); pet TmpPet; //load all the data into a temporary variable std::cout << "you chose to add a new pet!" << "\n"; EmptyBuffer(); std::cout<< "\n" << "Enter the pets name:"; std::getline(std::cin, TmpPet.Name); if(!std::cin.good()) { std::cin.clear(); delete &TmpPet; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddPet(); } std::cout<< "Enter the pets age:"; std::cin >> TmpPet.Age; if(!std::cin.good()) { std::cin.clear(); delete &TmpPet; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddPet(); } std::cout<< "Enter the pets gender (M/F):"; std::cin >> TmpPet.Gender; if(!std::cin.good()) { std::cin.clear(); delete &TmpPet; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddPet(); } switch(TmpPet.Gender) { case 'm': TmpPet.Gender = 'M'; break; case 'f': TmpPet.Gender = 'F'; break; case 'M': case 'F': break; default: delete &TmpPet; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddPet(); break; } EmptyBuffer(); std::cout<< "Enter the pets species:"; std::getline(std::cin, TmpPet.Species); if(!std::cin.good()) { std::cin.clear(); delete &TmpPet; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddPet(); } Pets.push_back(TmpPet); delete &TmpPet; cls(); std::cout<< Pets[Pets.size()-1].Name << " has been sucsessfully added to the database\n\n"; std::cout<< "press enter to return to the main menu"; std::cin.get(); DisplayMenu(); } void DeleteMenu(void) { cls(); std::cout<< "Delete Menu\n" << "press 1: to delete a pet\n" << "press 2: to delete an activity\n" << "press 3: to delete everything (new game)\n"; char DeleteSelection; std::cin>> DeleteSelection; if(!std::cin.good()) { std::cin.clear(); delete &DeleteSelection; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); } switch(DeleteSelection) { case '1': { cls(); if(Pets.size() > 0) { std::cout<< "your pets are:\n"; for(unsigned short x = 0; x <= Pets.size() - 1; x++) { std::cout<< Pets[x].Name << "\n"; } std::cout<< "\n" <<"enter the name of the pet you wish to delete:"; std::string PetName; std::cin>> PetName; if(!std::cin.good()) { std::cin.clear(); delete &PetName; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); } cls(); for (unsigned short x = 0; Pets.size(); x++) { if(Pets[x].Name == PetName) { std::cout<< "\n" << "Name: " << Pets[x].Name << "\n" << "Age: " << Pets[x].Age << "\n" << "Gender: " << Pets[x].Gender << "\n" << "species: " << Pets[x].Species << "\n\n" << "is this the pet you want to delete?\n" << "[Y/N]"; char DeleteVerify; std::cin>> DeleteVerify; if(!std::cin.good()) { std::cin.clear(); delete &DeleteVerify; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); } switch(DeleteVerify) { case 'y': case 'Y': //do nothing, just continue break; case 'n': case 'N': cls(); continue; default: std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); break; } Pets.erase(Pets.begin() + x); cls(); std::cout<< "Pet sucsessfully deleted.\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } //if the name is not found cls(); std::cout<< "there is no pet named \'" << PetName << "\' in the database!\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there are no pets in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } break; case '2': { cls(); if(Activities.size() > 0) { for(unsigned short x = 0; x <= Activities.size() - 1; x++) { std::cout<< "[NAME] " << Activities[x].Text << "\n"; } std::cout<< "what activity do you want to delete?\n"; std::string ActivityName; EmptyBuffer(); std::getline(std::cin, ActivityName); if(!std::cin.good()) { std::cin.clear(); delete &ActivityName; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); } for(unsigned short x = 0; x <= Activities.size() - 1; x++) { if(ActivityName == Activities[x].Text) { Activities.erase(Activities.begin() + x); cls(); std::cout<< "Activity sucsessfully deleted.\n" << "press enter to return to the main menu"; std::cin.get(); DisplayMenu(); } } cls(); std::cout<< "there is no activity with that text in the database!\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there are no Activities in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } }break; case '3': { cls(); if(Pets.size() > 0 or Activities.size() > 0) { std::cout<< "are you sure you want to continue\n" << "all your pets and activities will be deleted\n" << "[Y/N]"; char VerifyDelete; std::cin>> VerifyDelete; if(!std::cin.good()) { std::cin.clear(); delete &VerifyDelete; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); } switch(VerifyDelete) { case 'y': case 'Y': cls(); break; case 'n': case 'N': DisplayMenu(); break; default: cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); break; } if(Pets.size() > 0) { Pets.erase (Pets.begin(), Pets.end()); std::cout<< "all pets deleted.\n"; } else { std::cout<< "there are no pets in the database\n"; } if(Activities.size() > 0) { Activities.erase (Activities.begin(), Activities.end()); std::cout<< "all activities deleted\n"; } else { std::cout<< "there are no activities in the database\n"; } std::cout<< "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there is nothing in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } break; default: cls(); std::cout<< "the input is invalid \n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DeleteMenu(); break; } } void ListPets(void) { cls(); if(Pets.size() > 0) { std::cout<< "your pets are:\n\n"; for(unsigned short x = 0; x <= Pets.size() - 1; x++) { std::cout << "Name: " << Pets[x].Name << "\n" << "Age: " << Pets[x].Age << "\n" << "Gender: " << Pets[x].Gender << "\n" << "Species: " << Pets[x].Species << "\n\n"; } std::cout<< "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there are no pets in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } //activity functions void AddActivity(void) { cls(); std::cout<< "add an activity\n" << "the name of the pet will be at the begining of the text\n" << "(e.g. [NAME] digs a hole)\n\n" << "enter the text of this activity\n" << "[NAME] "; Activity TmpActivity; EmptyBuffer(); std::getline(std::cin, TmpActivity.Text); if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddActivity(); } std::cout<<"can all species do this?\n" << "[Y/N]"; char Answer; std::cin>> Answer; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddActivity(); } switch(Answer) { case 'y': case 'Y': TmpActivity.ForAll = true; break; case 'n': case 'N': TmpActivity.ForAll = false; std::cout<< "and what Species can do this\n"; std::cin>> TmpActivity.Species; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddActivity(); } break; default: cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); AddActivity(); break; } Activities.push_back(TmpActivity); cls(); std::cout<< "Activity saved\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } void EditActivity(void) { cls(); if(Activities.size() > 0) { for(unsigned short x = 0; x < Activities.size(); x++) { std::cout<< "[NAME] " << Activities[x].Text << "\n"; } std::cout<< "\n" << "enter the text of the activity you want to edit:"; std::string TmpText; EmptyBuffer(); std::getline(std::cin, TmpText); if(!std::cin.good()) { std::cin.clear(); delete &TmpText; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); EditActivity(); } for(unsigned short x = 0; x < Activities.size(); x++) { if(Activities[x].Text == TmpText) { cls(); std::cout<< "\n" << "press 0: if this is not the activity you want to edit\n" << "press 1: to edit the Text: " << Activities[x].Text << "\n" << "press 2: to edit what species can do it: "; if(Activities[x].ForAll == true) { std::cout<< "all species can do this\n"; } else if(Activities[x].ForAll == false){ std::cout<< "only " << Activities[x].Species << "s can do this\n"; } std::cout<< "or press 3: to edit everythig\n"; char EditSelection; std::cin>> EditSelection; if(!std::cin.good()) { std::cin.clear(); delete &EditSelection; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); EditActivity(); } switch(EditSelection) { case '0': continue; case '1': cls(); std::cout<< "\n" << "Enter the activities text:"; std::getline(std::cin, Activities[x].Text); if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } break; case '2': cls(); std::cout<< "can all species do this activity:[Y/N]"; char Tmp; std::cin >> Tmp; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } switch(Tmp) { case 'y': case 'Y': Activities[x].ForAll = true; break; case 'n': case 'N': Activities[x].ForAll = false; cls(); std::cout<< "what species can do this: "; std::cin>> Activities[x].Species; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } break; default: cls(); std::cout<< "the input is invalid \n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); break; } break; case '3': cls(); std::cout<< "\n" << "Enter the activities text:"; EmptyBuffer(); std::getline(std::cin, Activities[x].Text); if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } std::cout<< "can all species do this activity:[Y/N]"; char Tmp2; std::cin >> Tmp2; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } switch(Tmp2) { case 'y': case 'Y': Activities[x].ForAll = true; break; case 'n': case 'N': Activities[x].ForAll = false; cls(); std::cout<< "what species can do this: "; std::cin>> Activities[x].Species; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } break; default: cls(); std::cout<< "the input is invalid \n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); cls(); std::cout<< "\n" << "Enter the activities text:"; std::cin >> Activities[x].Text; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } std::cout<< "can all species do this activity:[Y/N]"; char Tmp2; std::cin >> Tmp2; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } switch(Tmp2) { case 'y': case 'Y': Activities[x].ForAll = true; break; case 'n': case 'N': Activities[x].ForAll = false; cls(); std::cout<< "what species can do this: "; std::cin>> Activities[x].Species; if(!std::cin.good()) { std::cin.clear(); cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); cls(); EditActivity(); } break; default: cls(); std::cout<< "the input is invalid \n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); break; } break; } break; } cls(); std::cout<< "activity sucsessfully Edited.\n" << "the new data is:\n\n" << "[NAME] " << Activities[x].Text << "\n"; if(Activities[x].ForAll == true) { std::cout<< "all species can do this\n"; } else if(Activities[x].ForAll == false) { std::cout<< "only " << Activities[x].Species << "s can do this\n"; } std::cout<< "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } //if the name is not found cls(); for(unsigned short x = 0; x < Activities.size(); x++) { if(Activities[x].Text == TmpText) { std::cout<< "there are no more activities with the text \"" << TmpText << "\" in the database!\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } std::cout<< "there is no activity with the text \"" << TmpText << "\" in the database!\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there are no activities in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } void ListActivities(void) { cls(); if(Activities.size() > 0) { std::cout<< "your activities are:\n\n"; for(unsigned short x = 0; x <= Activities.size() - 1; x++) { std::cout<< "[NAME] " << Activities[x].Text << "\n"; if(Activities[x].ForAll == true) { std::cout<< "all pets can do this\n\n\n"; } else if(Activities[x].ForAll == false){ std::cout<< "only " << Activities[x].Species << "s can do this\n\n"; } } std::cout<< "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there are no Activities in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } //other functions void EmptyBuffer(void) { std::cin.ignore(100, '\n'); } void DisplayMenu(void) { cls(); std::cout<< "- pet keeper - a programming demo -\n"; std::cout<< "what do you want to do?\n\n"; std::cout << "press E: to exit\n" << "press H: for help\n\n" << "press S: to save\n" << "press L: to load\n\n" << "press 1: to add a pet\n" << "press 2: to edit a pets details\n" << "press 3: to list all your pets\n\n" << "press 4: to add an activity for a pet to do\n" << "press 5: to edit an activity\n" << "press 6: to list all your activities\n\n" << "press R: to run the simulation\n\n" << "press D: to go to the delete menu\n\n" << "Input:"; char TaskChoose; std::cin>> TaskChoose; if(!std::cin.good()) { std::cin.clear(); delete &TaskChoose; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } switch(TaskChoose){ case 'e': case 'E': exit(0); break; case 'h': case 'H': DisplayHelp(); break; case 's': case 'S': Save(); break; case 'l': case 'L': Load(); break; case '1': AddPet(); break; case '2': //edit pet cls(); if(Pets.size() > 0) { for(unsigned short x = 0; x < Pets.size(); x++) { std::cout<< x << ": " << Pets[x].Name << " " << Pets[x].Age << " " << Pets[x].Gender << " " << Pets[x].Species; } std::cout<< "enter the number of the pet you want to edit:"; std::string PetNumber; std::cin>> PetNumber; if(!std::cin.good() or PetNumber > Pets.size()) { std::cin.clear(); delete &PetNumber; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); EditPet(); } Pets[PetNumber].Edit(PetNumber); } else { std::cout<< "there are no pets in the database\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } break; case '3': ListPets(); break; case '4': AddActivity(); break; case '5': EditActivity(); break; case '6': ListActivities(); break; case 'r': case 'R': RunSimulation(); break; case 'd': case 'D': DeleteMenu(); break; default: //say that an error occured cls(); std::cout<< "the input is invalid \n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); break; } } void DisplayHelp(void) { cls(); std::cout << " HELP:\n\n" << "on the main menu choose what you want to do\n" << "and enter the number corrisponding to that activity.\n" << "then press enter to submit your choice.\n" << "(input is not case sensitive)\n\n"; std::cout << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } void cls(void) { std::system("CLS"); } void RunSimulation(void) { cls(); if(Pets.size() > 0) { std::cout<< "- Simulation -\n\n"; //define variables srand (time(NULL)); std::vector ChosenPets; std::vector ChosenActivities; std::vector PossibleActivities; unsigned short Runs = rand() % 4 + 1; for(unsigned short x = 0; x < Runs; x++) { //choose a random pet ChosenPets.push_back(rand() % Pets.size()); //filter out un-do-able activities for (unsigned short x = 0; x < Activities.size(); x++) { if(Activities[x].ForAll == true or Activities[x].Species == Pets[ChosenPets[x]].Species) { PossibleActivities.push_back(x); } } //choose a random do-able activity for the pet ChosenActivities.push_back(PossibleActivities[rand() % PossibleActivities.size()]); } std::cout<< GameTime << " O\'clock\n"; for(unsigned short x = 0; x < ChosenPets.size(); x++) { std::cout<< Pets[ChosenPets[x]].Name << " " << Activities[ChosenActivities[x]].Text << "\n\n"; } GameTime++; } else { std::cout<< "there are no pets in the database!\n"; } std::cout<< "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } //file functions void Save(void) { cls(); if(Pets.size() > 0 or Activities.size() > 0) { char VerifySave; std::cout<< "are you sure you want to save (saving overwrites old data)\n" << "[Y/N]"; std::cin>> VerifySave; if(!std::cin.good()) { std::cin.clear(); delete &VerifySave; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); Save(); } switch(VerifySave) { case 'y': case 'Y': cls(); break; case 'n': case 'N': DisplayMenu(); break; default: cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); Save(); break; } if(Pets.size() > 0) { std::ofstream PetsOutput("Pets.ggsav", std::ios::trunc); if(PetsOutput.is_open()) { for(unsigned short x = 0; x < Pets.size(); x++) { PetsOutput << Pets[x].Name << "\n" << Pets[x].Age << "\n" << Pets[x].Gender << "\n" << Pets[x].Species << "\n\n"; } PetsOutput.close(); std::cout<< "pets Saved...\n"; } else { std::cout<< "the pets save file could not be written!\n"; } } else { std::cout<< "There ane no pets in the database to save\n"; } if(Activities.size() > 0) { std::ofstream ActivitiesOutput("Activities.ggsav", std::ios::trunc); if(ActivitiesOutput.is_open()) { for(unsigned short x = 0; x < Activities.size(); x++) { ActivitiesOutput << Activities[x].Text << "\n"; if(Activities[x].ForAll == true) { ActivitiesOutput<< "true\n" << "\n"; } else { ActivitiesOutput<< "false\n" << Activities[x].Species << "\n\n"; } } ActivitiesOutput.close(); std::cout<< "Activities Saved...\n"; } else { } } else { std::cout<< "there are no activities in the database\n"; } std::cout<< "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } else { std::cout<< "there is nothing in the database!\n" << "press enter to return to the main menu"; EmptyBuffer(); std::cin.get(); DisplayMenu(); } } void Load(void) { cls(); std::cout<< "are you sure you want to continue\n" << "(any unsaved data will be lost)\n" << "[Y/N]"; char VerifyLoad; std::cin>> VerifyLoad; if(!std::cin.good()) { std::cin.clear(); delete &VerifyLoad; cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); Save(); } switch(VerifyLoad) { case 'y': case 'Y': cls(); break; case 'n': case 'N': DisplayMenu(); break; default: cls(); std::cout<< "the input is invalid\n" << "press enter to start again"; EmptyBuffer(); std::cin.get(); Load(); break; } Pets.erase (Pets.begin(),Pets.end()); std::ifstream PetsInput("Pets.ggsav"); if(PetsInput.is_open()) { pet TmpPet; std::string Line; unsigned short Cycle = 1; while(! PetsInput.eof() ) { switch (Cycle) { case 1: std::getline (PetsInput,Line); TmpPet.Name = Line; break; case 2: std::getline (PetsInput,Line); TmpPet.Age = atoi(Line.c_str()); break; case 3: std::getline (PetsInput,Line); char LineChars[10]; strcpy(LineChars, Line.c_str()); TmpPet.Gender = LineChars[0]; break; case 4: std::getline (PetsInput,Line); TmpPet.Species = Line; break; case 5: std::getline (PetsInput,Line); Pets.push_back(TmpPet); Cycle = 0; break; } Cycle++; } std::cout<< "pets loaded...\n"; } else { std::cout<< "The pets save file could not be opened\n"; } Activities.erase (Activities.begin(),Activities.end()); std::ifstream ActivitiesInput("Activities.ggsav"); if(ActivitiesInput.is_open()) { Activity TmpActivity; std::string Line; unsigned short Cycle = 1; EmptyBuffer(); while(! ActivitiesInput.eof() ) { std::getline (ActivitiesInput,Line); switch (Cycle) { case 1: TmpActivity.Text = Line; break; case 2: if(Line == "true") { TmpActivity.ForAll = true; } else if (Line == "false") { TmpActivity.ForAll = false; } break; case 3: if(TmpActivity.ForAll == false) { TmpActivity.Species = Line; Activities.push_back(TmpActivity); } else if(TmpActivity.ForAll == true) { Activities.push_back(TmpActivity); Cycle = 0; } break; case 4: Cycle = 0; break; } Cycle++; } std::cout<< "Activities loaded...\n"; } else { std::cout<< "The activities save file could not be opened\n"; } std::cout<< "press enter to return to the main menu"; //EmptyBuffer(); std::cin.get(); DisplayMenu(); }