Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;




class Medicine{
private:
	string productName;
	double productPrice;

public:
	Medicine(string productName, double productPrice){
		this->productName = productName;
		this->productPrice = productPrice;
	}

	void printMedicineDetails(){
		cout << "Product Name: " << this->productName << "\n"
			<< "Product Price: " << this->productPrice << "\n";

	}

	// Getter and setter
	string getProductName(){
		return productName;
	}

	void setProductName(string productName){
		productName = productName;
	}
};


//Function Prototype
void addMedicine(vector<Medicine>& medicineCollection);
void printAllMedicine(vector<Medicine>& medicineCollection);
void deleteMedicine(vector<Medicine>& medicineCollection);
void searchMedicine(vector<Medicine>& medicineCollection);

vector<Medicine> readMedicineRecordFromFile(string filename);
void writeMedicineRecordToFile(vector<Medicine>& medicineCollection, string filename);








int main()
{
	vector <Medicine> medicineCollection;
	string MedicineRecordFilename = "MedicineRecord.txt";
	int menuOption;


	do
	{
		system("CLS");
		cout << "1. Add Medicine" << "\n";
		cout << "2. Delete Medicine" << "\n";
		cout << "3. Modify Medicine" << "\n";
		cout << "4. Search Medicine" << "\n";
		cout << "5. Medicine Expiry Date" << "\n";
		cout << "6. Return to Login menu " << "\n";
		cout << "7. Exit " << "\n\n";



		cout << "Enter option: ";
		cin >> menuOption;

		switch (menuOption) {

		case 1:
			addMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 2:
			deleteMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 3:
			printAllMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 4:
			searchMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 7:
			cout << "System Terminal succesfully" << "\n";
			system("PAUSE");

			system(0);


		default:
			cout << "Invalid Input, Please try again." << "\n";
			break;
		}

	} while (menuOption != 7);

	getchar();
	return 0;
}

void addMedicine(vector<Medicine>& medicineCollection) {
	string productName;
	double productPrice;

	cout << "Enter Product Name: ";
	cin >> productName;

	cout << "Enter Product Price: ";
	cin >> productPrice;

	Medicine newMedicine(productName, productPrice);
	medicineCollection.push_back(newMedicine);

	cout << "Medicine succesfully recorded.";


}

void printAllMedicine(vector<Medicine>& medicineCollection) {
	if (medicineCollection.empty()){
		cout << "No, Medicine record yet." << "\n";

		return;
	}

	for (Medicine medicine : medicineCollection) {
		cout << medicine.getProductName() << "\n";
	}
}

void deleteMedicine(vector<Medicine>& medicineCollection) {

	string productName;

	if (medicineCollection.empty()) {
		cout << "No, Medicine record yet." << "\n";

		return;
	}

	cout << "Enter Product Name: ";
	cin >> productName;

	for (unsigned int i = 0; i < medicineCollection.size(); i++) {

		if (productName.compare(medicineCollection[i].getProductName()) == 0) {

			medicineCollection.erase(medicineCollection.begin() + i);
			cout << "Medicine record has succesfully deleted.";
			return;

		}

		cout << "Sorry Medicine record not found.";
	}


}

void searchMedicine(vector<Medicine>& medicineCollection) {

	string productName;

	if (medicineCollection.empty()) {
		cout << "No, Medicine record yet." << "\n";

		return;
	}

	cout << "Enter Product Name: ";
	cin >> productName;

	for (unsigned int i = 0; i < medicineCollection.size(); i++) {

		if (productName.compare(medicineCollection[i].getProductName()) == 0) {

			medicineCollection[i].printMedicineDetails();
			return;

		}

		cout << "Sorry Medicine record not found.";
	}
}


What I have tried:

I try to save the medicine vector to file but it doesn't not work, please help me out ?!

std::ofstream outFile;
outFile.open(fileName, ios::out);
for(int i=0;i<obj.size();i++)
    {
        outFile.write((const char *)(obj.data()),sizeof(vector<vector<Medicine> >)*obj.size());
    }
outFile.close();
Posted
Updated 31-Jul-17 20:31pm
Comments
Patrice T 1-Aug-17 0:48am    
'but it doesn't not work' is not informative, please define.

1 solution

Have a look at Serialization[^]. I suggest you to add serialization support to your Medicine class (e.g. load/save methods) and then iterate over the vector items in order to serialize the whole container.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900