hi all guru,

here, my problem is re-running the program, the vector attribute of Service Class was listed unreadable characters.. WHY??

Below is my following code :

Service.h code:

class Service{
protected:
	char	servType[100];
	vector<Medicine> vecMedicine; // the purpose that i created a vector attribute is a service can have many medicine
	vector<vector<char> > vecDosage; 
public:
	static int servNo;
public:
	Service() 
	{
		servNo++;
	}
	
	//set
	void setServType(string newServType) { strcpy(servType, newServType.c_str()); }
	void setMedicine(Medicine newMedicine) { vecMedicine.push_back(newMedicine); }
	void setDosage(vector<char> newVecDosage) { vecDosage.push_back(newVecDosage); }

	//get
	string getServType() { return servType; }
	vector<Medicine> getMedicine() { return vecMedicine; }
	vector<vector<char> > getDosage() { return vecDosage; }

	virtual double getServiceCharge() { return 0; }

Regular.h code:

#define REGULAR_CHARGE 50;

class Regular : public Service{
public:
	Regular() { strcpy(servType,"Regular"); }

	double getServiceCharge() { return REGULAR_CHARGE; }
};

serviceDAO.h code:

#define SERVICE_FILE "db/service.txt"

class ServiceDAO{
public:
	ServiceDAO() { }	
	void saveFile(vector<Service*>& newVecService)
	{
		int i = 0;
		fstream file;

		file.open(SERVICE_FILE,ios::out|ios::trunc|ios::binary);
		
		for(i=0; i < newVecService.size(); i++)
		{
			if(newVecService[i] != NULL)
			{
				file.write((char*)newVecService[i], sizeof(*newVecService[i]));
			}
		}
	}

	vector<Service*>& readFile(vector<Service*>& newVecService, Service *newServ)
	{
		int i = 1;
		newServ = new Service; // allocate memory for the read function  
		fstream file;

		--Service::servNo;
		file.open(SERVICE_FILE,ios::in|ios::binary);

		if(file.is_open())
		{
			while(file.read((char*)newServ, sizeof(*newServ)))
			{
				newVecService[i] = newServ;
				newServ = new Service; // allocate memory for the read function  
				if(newVecService[i] != NULL)
				{
					Service::servNo = i;
				}
				++i;
			}
			delete newServ;
			newServ = 0;
		}
		return newVecService;
	}
};

Main.cpp code:

static const int MAXIMUM_RECORDS = 300;
int Service::servNo = 0;

vector<Medicine> vecMedicine;

Service *newServ = 0;
ServiceDAO servDAO;
vector<Service*> vecService;

void main()
{
	vecService.assign(MAXIMUM_RECORDS, newServ);
	vecService = servDAO.readFile(vecService, newServ);
	do
	{
		// at here display the main menu
		// switch(choice)
		// case 1 goto addService();break;
		// case 2 goto searchService();break;
		// case 3 goto listService();break;
	}
	servDAO.saveFile(vecService);
}

void addService()
{
	newServ = new Regular();
	vecService[Service::servNo] = newServ;
	// back to main menu
}

void searchService()
{
	newServ = new Regular();
	
	// enter a valid medicine input
	vecService[x]->setMedicine(vecMedicine[iPosMed]);
	
	// enter a valid dosage input
	// pusback the value into tempVec
	vector<char> tempVec;
	vecService[pos]->setDosage(tempVec);

	// back to main menu
}

void listService()
{
	system("cls");
	int x;
	int z = 0;
	int iMed = 0;
	string temp;
	gotoxy(0,8);cout << "No.";
	gotoxy(5,8);cout << "Type ";
	gotoxy(55,8);cout << "Medicine(s)";
	gotoxy(0,9);cout << "-------------------------------------------------------------------------------";
	for (x=1;x<vecService.size();x++)
	{
		if(vecService[x] != NULL)
		{
			gotoxy(0,9+x+z);cout << x;
			gotoxy(5,9+x+z);cout << vecService[x]->getServType().c_str();
			if(vecService[x]->getMedicine().size() != 0)
			{
				for(iMed = 0; iMed < vecService[x]->getMedicine().size(); iMed++)
				{
					if(iMed == 0)
					{
						temp = vecService[x]->getMedicine()[iMed].getMedName();
					}
					else
					{
						temp += ", " + vecService[x]->getMedicine()[iMed].getMedName();
					}
					gotoxy(55,9+x+z);cout << temp.c_str(); 
				}
			}
			else
			{
				gotoxy(55,9+x+z);cout << "<<empty>>";
			}
			
		}
		else
		{
			--z;
		}
	}
}

hope all guru can help me solve my problem..

Thanks in advance..

when posting that much code you need to post all of it so that it will compile cleanly (unless that's the problem). main.cpp does not contain the include files and class Medicane is undefined.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.