In: Computer Science
C++
Write a menu based program for the pet rescue. There should be 2 menu options
-Add a pet
-View pets
-If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate.
-If the user chooses option 2, you will open your data file for reading and print the information all the pets to the screen.
-Once you have completed the actions of the chosen option, return to the main menu and ask if the user would like to quit or perform another task.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
class Pet
{
private:
string name, species, breed, color;
public:
Pet()
{
this->name = "";
this->species = "";
this->breed = "";
this->color = "";
}
Pet(string name, string species, string breed, string color)
{
this->name = name;
this->species = species;
this->breed = breed;
this->color = color;
}
string getName(){ return this->name; }
string getSpecies(){ return this->species; }
string getBreed(){ return this->breed; }
string getColor(){ return this->color; }
string toString()
{
stringstream ss;
ss << "Name: " << this->name << ", Species: " << this->species << ", Breed: " << this->breed << ", Color: " << this->color;
return ss.str();
}
};
// function prototypes
void addAPet();
vector<Pet> readData();
void printMenu();
void viewPets(vector<Pet> pets);
int main()
{
vector<Pet> pets = readData();
int choice;
char yesNo;
do
{
printMenu();
cin >> choice;
switch(choice)
{
case 1:
{
addAPet();
break;
}
case 2:
{
viewPets(readData());
break;
}
default:
cout << "Invalid choice" << endl;
}
cout << "Continue? [y/n]: ";
cin >> yesNo;
cout << endl;
if(yesNo == 'N' || yesNo == 'n')
{
cout << "Goodbye!" << endl;
exit(0);
}
}while(yesNo != 'N' || yesNo != 'n');
}
void printMenu()
{
cout << "1. Add a pet\n2. View pets\nEnter your choice: ";
}
void addAPet()
{
string name, species, breed, color;
cin.ignore();
cout << "\nEnter name of the pet: ";
getline(cin, name);
cout << "Enter species of the pet: ";
getline(cin, species);
cout << "Enter breed of the pet: ";
getline(cin, breed);
cout << "Enter color of the pet: ";
getline(cin, color);
ofstream outFile;
outFile.open ("pets.txt", std::fstream::in | std::fstream::out | std::fstream::app);
if(!outFile.is_open())
{
cout << "Cannot open pets.txt" << endl;
exit(1);
}
outFile << endl << name << "," << species << "," << breed << "," << color;
cout << name << " is added to the pet list." << endl;
}
vector<Pet> readData()
{
ifstream inFile;
inFile.open ("pets.txt", std::fstream::in | std::fstream::out | std::fstream::app);
if(!inFile.is_open())
{
cout << "Cannot open pets.txt" << endl;
exit(1);
}
vector<Pet> pets;
string line;
while(getline(inFile, line))
{
stringstream ss(line);
vector<string> tokens;
string token;
while(getline(ss, token, ','))
{
tokens.push_back(token);
}
string name = tokens[0];
string species = tokens[1];
string breed = tokens[2];
string color = tokens[3];
pets.push_back(Pet(name, species, breed, color));
}
return pets;
}
void viewPets(vector<Pet> pets)
{
cout << endl << "ALL PETS:\n---------\n";
for(Pet pet : pets)
{
cout << pet.toString() << endl;
}
cout << endl;
}
**************************************************************** SCREENSHOT ***********************************************************
Console Output:
pets.txt (input/output file)