#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// Define the structure for a T-shirt
struct TShirt {
    int id;
    string name;
    string size;
    double price;
};

class TShirtStore {
private:
    vector<TShirt> tshirts;
    int nextId = 1; // Counter for generating unique IDs

public:
    // Display the main menu
    void displayMenu() {
        cout << "\n===== T-Shirt Store Management System =====\n";
        cout << "1. Add T-Shirt\n";
        cout << "2. Update T-Shirt Details\n";
        cout << "3. Delete T-Shirt\n";
        cout << "4. View All T-Shirts\n";
        cout << "5. Save Data to File\n";
        cout << "6. Load Data from File\n";
        cout << "7. Exit\n";
    }

    // Add a new T-shirt to the system
    void addTShirt() {
    TShirt tshirt;
    tshirt.id = nextId++;
    
    cout << "Enter T-shirt name: ";
    getline(cin, tshirt.name);

    // Input validation: Ensure the name is not an empty string
    while (tshirt.name.empty()) {
        cout << "Name cannot be empty. Enter T-shirt name: ";
        getline(cin, tshirt.name);
    }

    cout << "Enter T-shirt size: ";
    getline(cin, tshirt.size);

    // Input validation: Ensure the size is not an empty string
    while (tshirt.size.empty()) {
        cout << "Size cannot be empty. Enter T-shirt size: ";
        getline(cin, tshirt.size);
    }

    // Input validation for price
    string priceInput;
    while (true) {
        cout << "Enter T-shirt price: ";
        getline(cin, priceInput);

        try {
            tshirt.price = stod(priceInput); // Convert string to double
            if (tshirt.price < 0) {
                cout << "Price cannot be negative. Please enter a non-negative price.\n";
            } else {
                break; // Exit the loop if the input is a valid double
            }
        } catch (const invalid_argument& e) {
            cout << "Invalid input. Please enter a valid numeric price.\n";
        } catch (const out_of_range& e) {
            cout << "Input out of range. Please enter a valid numeric price.\n";
        }
    }

    cin.ignore();  // Clear the input buffer
    tshirts.push_back(tshirt);
    cout << "T-shirt added successfully! ID: " << tshirt.id << '\n';
}

// Update the details of an existing T-shirt by ID
void updateTShirtDetails() {
    int id;
    cout << "Enter T-shirt ID: ";
    cin >> id;

    auto it = find_if(tshirts.begin(), tshirts.end(), [id](const TShirt& t) {
        return t.id == id;
    });

    if (it != tshirts.end()) {
        cout << "Enter new name for T-shirt (ID: " << id << "): ";
        getline(cin, it->name);

        // Input validation: Ensure the name is not an empty string
        while (it->name.empty()) {
            cout << "Name cannot be empty. Enter new name for T-shirt (ID: " << id << "): ";
            getline(cin, it->name);
        }

        cout << "Enter new size for T-shirt (ID: " << id << "): ";
        getline(cin, it->size);

        // Input validation: Ensure the size is not an empty string
        while (it->size.empty()) {
            cout << "Size cannot be empty. Enter new size for T-shirt (ID: " << id << "): ";
            getline(cin, it->size);
        }

        // Input validation for price
        string priceInput;
        while (true) {
            cout << "Enter new price for T-shirt (ID: " << id << "): ";
            getline(cin, priceInput);

            try {
                it->price = stod(priceInput); // Convert string to double
                if (it->price < 0) {
                    cout << "Price cannot be negative. Please enter a non-negative price.\n";
                } else {
                    break; // Exit the loop if the input is a valid double
                }
            } catch (const invalid_argument& e) {
                cout << "Invalid input. Please enter a valid numeric price.\n";
            } catch (const out_of_range& e) {
                cout << "Input out of range. Please enter a valid numeric price.\n";
            }
        }

        cin.ignore();  // Clear the input buffer
        cout << "T-shirt details updated successfully!\n";
    } else {
        cout << "T-shirt not found!\n";
    }
}

    // Delete an existing T-shirt from the system by ID
    void deleteTShirt() {
        int id;
        cout << "Enter T-shirt ID: ";
        cin >> id;

        auto it = find_if(tshirts.begin(), tshirts.end(), [id](const TShirt& t) {
            return t.id == id;
        });

        if (it != tshirts.end()) {
            tshirts.erase(it);
            cout << "T-shirt deleted successfully!\n";
        } else {
            cout << "T-shirt not found!\n";
        }
    }

    // View all T-shirts in the system
    void viewAllTShirts() {
        cout << "\n===== All T-Shirts =====\n";
        for (const auto& tshirt : tshirts) {
            cout << "ID: " << tshirt.id << ", Name: " << tshirt.name << ", Size: " << tshirt.size << ", Price: " << tshirt.price << '\n';
        }
    }

    // Save the current state of the data set to a file
    void saveToFile() {
        ofstream outFile("tshirt_data.txt");
        if (outFile.is_open()) {
            for (const auto& tshirt : tshirts) {
                outFile << tshirt.id << ' ' << tshirt.name << ' ' << tshirt.size << ' ' << tshirt.price << '\n';
            }
            outFile.close();
            cout << "Data saved to file successfully!\n";
        } else {
            cout << "Error: Unable to open file for writing.\n";
        }
    }

    // Load data from a file
    void loadFromFile() {
        tshirts.clear(); // Clear existing data

        ifstream inFile("tshirt_data.txt");
        if (inFile.is_open()) {
            TShirt tshirt;
            while (inFile >> tshirt.id >> tshirt.name >> tshirt.size >> tshirt.price) {
                tshirts.push_back(tshirt);
            }
            inFile.close();
            cout << "Data loaded from file successfully!\n";
        } else {
            cout << "No previous data found.\n";
        }
    }

    // Run the T-shirt store management system
    void run() {
        string choice;
        while (true) {
            displayMenu();
            cout << "Enter your choice (1-7): ";
            getline(cin, choice);

            if (choice == "1") {
                addTShirt();
            } else if (choice == "2") {
                updateTShirtDetails();
            } else if (choice == "3") {
                deleteTShirt();
            } else if (choice == "4") {
                viewAllTShirts();
            } else if (choice == "5") {
                saveToFile();
            } else if (choice == "6") {
                loadFromFile();
            } else if (choice == "7") {
                cout << "Exiting T-Shirt Store Management System. Goodbye!\n";
                break;
            } else {
                cout << "Invalid choice. Please enter a number between 1 and 7.\n";
            }
        }
    }
};

int main() {
    TShirtStore tshirtStore;
    tshirtStore.run();

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