Question

In: Computer Science

C++ Write a menu based program for the pet rescue. There should be 2 menu options...

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.

Solutions

Expert Solution

#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)


Related Solutions

Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:Covert a binary string to corresponding positive integersConvert a positive integer to its binary representationAdd two binary numbers, both numbers are represented as a string of 0s and 1sTo reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:int binary_to_decimal(string...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER TEMPERATURE – JAMES SMITH Convert Fahrenheit temperature to Celsius Convert Celsius temperature to Fahrenheit Exit CASE 1: Convert Fahrenheit temperature to Celsius -Display the message to ask to enter Fahrenheit degree from the keyboard -Use the following formula to convert to Celsius degree         Celsius Temperature = (Fahrenheit Temperature – 32) * 5/9 ; -Display the output as below: TemperatureConverter_Smith.cpp TEMPERATURE CONVERTER – JAMES...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT