Re-running program! displayed unreadable charactes..
hi all guru,
here, my problem is re-running the program, the vector attribute was retrieved and displayed unreadable characters.. WHY??
Below is my following code :
Service.h code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
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:
1 2 3 4 5 6 7 8
|
#define REGULAR_CHARGE 50;
class Regular : public Service{
public:
Regular() { strcpy(servType,"Regular"); }
double getServiceCharge() { return REGULAR_CHARGE; }
};
|
serviceDAO.h code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
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..
Topic archived. No new replies allowed.