#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];
    }
}
     
by

C++ Online Compiler

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!

Read inputs from stdin

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;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

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.

2. Switch:

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;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

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 
}  

5. Do-While:

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

Functions

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.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}