#include <iostream> #include <cstring> #include <cctype> using namespace std; struct Pet { char* name; char* type; int age; float price; }; const int MAX_PETS = 5; Pet* pets[MAX_PETS]; int numPets = 0; void displayMenu(); void addPet(); void displayPets(); void buyPet(); void searchPet(); void cleanupMemory(); int main() { char choice; do { displayMenu(); cout << "----------------------------------------\n"; cout << "β’Enter your choice: "; cin >> choice; cout << "----------------------------------------\n"; switch(tolower(choice)) { case 'a': addPet(); break; case 'b': displayPets(); break; case 'c': buyPet(); break; case 'd': searchPet(); break; case 'e': cout << "Thankyou comeback again!!.\n"; cout << "________________________________________\n"; cleanupMemory(); break; default: cout << "Invalid choice. Please try again.\n"; cout << "________________________________________\n"; } } while(tolower(choice) != 'e'); return 0; } void displayMenu() { cout << "βοΈ: [email protected]\n"; cout << "π: 09918268831\n"; cout << "----------------------------------------\n"; cout << "|πΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎ|\n"; cout << "|πΎπΎπΎπ¦Mac's Friendly Pet Shop πΎπΎπΎ|\n"; cout << "|πΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎ|\n"; cout << "----------------------------------------\n"; cout << "|A. Add a pet | β |\n"; cout << "----------------------------------------\n"; cout << "|B. Show available pets | π |\n"; cout << "----------------------------------------\n"; cout << "|C. Buy a pet | π² |\n"; cout << "----------------------------------------\n"; cout << "|D. Find details of pet | π |\n"; cout << "----------------------------------------\n"; cout << "|E. Quit | β |\n"; cout << "----------------------------------------\n"; cout << "________________________________________\n"; } void addPet() { if(numPets >= MAX_PETS) { cout << "Sorry, the pet shop is full.\n"; cout << "________________________________________\n"; return; } pets[numPets] = new Pet; pets[numPets]->name = new char[50]; cout << "Pet name: "; cin >> pets[numPets]->name; pets[numPets]->type = new char[50]; cout << "Pet type: "; cin >> pets[numPets]->type; cout << "Pet age: "; cin >> pets[numPets]->age; cout << "Pet price: "; cin >> pets[numPets]->price; numPets++; cout <<"========================================\n"; cout << "Pet has been added.\n"; cout <<"========================================\n"; } void displayPets() { if(numPets == 0) { cout <<"########################################\n"; cout << "No pets available.\n"; cout <<"########################################\n"; return; } cout <<"ΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓ\n"; cout << "Available Pets:\n"; cout <<"ΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓΓ\n"; for(int i = 0; i < numPets; i++) { cout<<"πΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎ\n"; cout<< "Name: " << pets[i]->name <<endl; cout<< "Type: " << pets[i]->type << endl; cout<< "Age: " << pets[i]->age <<endl; cout<< "Price: β±" << pets[i]->price << endl; cout<<"πΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎπΎ\n"; } } void buyPet() { if(numPets == 0) { cout <<"########################################\n"; cout << "No pets available to buy.\n"; cout <<"########################################\n"; return; } displayPets(); int choice; cout <<"########################################\n"; cout << "Enter the number of the pet you want to buy: \n"; cout <<"########################################\n"; cin >> choice; if(choice < 1 || choice > numPets) { cout << "Invalid choice.\n"; return; } cout << "You bought " << pets[choice - 1]->name << ". Thank you!\n"; delete[] pets[choice - 1]->name; delete[] pets[choice - 1]->type; delete pets[choice - 1]; // Free memory for the pet // Remove the bought pet by shifting remaining pets for(int i = choice - 1; i < numPets - 1; i++) { pets[i] = pets[i + 1]; } numPets--; } void searchPet() { if(numPets == 0) { cout <<"########################################\n"; cout << "No pets available to search.\n"; cout <<"########################################\n"; return; } char searchName[50]; cout << "Enter the name of the pet you want to search: "; cin >> searchName; for(int i = 0; i < numPets; i++) { if(strcmp(pets[i]->name, searchName) == 0) { cout << "Pet found:\n"; cout << "Name: " << pets[i]->name <<endl; cout << ", Type: " << pets[i]->type <<endl; cout<< ", Age: " << pets[i]->age <<endl; cout<< ", Price: β±" << pets[i]->price << endl; return; } } cout << "Pet not found.\n"; } void cleanupMemory() { for(int i = 0; i < numPets; i++) { delete[] pets[i]->name; delete[] pets[i]->type; delete pets[i]; } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}