Generating a name of 5 random characters
Database.h
class Database
{
private:
Record *pFirst;
int numRecord;
public:
Database();
~Database();
void create(int n);
char* genName();
void display();
bool remove(char * recName);
bool insert(int pos, Record *rec);
};
Database.cpp
#include <iostream>
#include "Database.h"
#include <time.h>
using namespace std;
Database::Database()
{
srand((unsigned int) time(NULL));
numRecord = 0;
pFirst = 0;
}
Database::~Database()
{;}
void Database::create(int n)
{
numRecord = n;
Database *re = new Database;
Record *pH, *pT, *pL;
int i;
pH = new Record;
pL = pH;
pL->setName(re->genName());
for (i=1; i<n; i++)
{
pT = new Record;
pL->setNext(pT);
pL = pT;
pL->setName(re->genName());
}
pFirst = pH;
}
char* Database::genName()
{
char ch;
char *tempName = new char;
for (int i=1; i<6; i++)
{
ch = ((rand()%26) + 97);
cout << (char) ch;
if (i%5 == 0)
{
cout << endl;
}
}
return tempName;
}
void Database::display()
{
if (pFirst!=0)
{
Record *pTemp = pFirst;
int i=0;
do
{
cout << "Record #" << i << ": " << pTemp->getName() << endl;
pTemp = pTemp->getNext();
i++;
}
while (pTemp!=0);
}
}
bool Database::remove(char *recName)
{
Record *pCurr, *pPrev, *pRecord;
pCurr = pFirst;
bool found = false;
do
{
if (pCurr->getName() == recName)
found = true;
else
{
pPrev = pCurr;
pCurr = pPrev->getNext();
}
}
while ((!found) && (pCurr!=0));
if (found)
{
if(pCurr == pFirst)
{
pPrev = pFirst;
pFirst = pFirst->getNext();
delete pPrev;
}
else
{
pRecord=pCurr->getNext();
pPrev->setNext(pRecord);
delete pCurr;
}
display();
}
else
{
cout << recName << " not found!!\n";
}
return false;
}
bool Database::insert(int pos, Record *rec)
{
Record *pC;
pC = pFirst;
bool found2 = false;
do
{
if (pos==numRecord)
found2 = true;
else
{
pC->getNext();
}
}
while ((!found2) && (pC != 0));
if (found2)
{
if (pC == pFirst)
{
cout << "Cannot insert before Record#0!!!\n";
}
else
{
rec = new Record;
rec->setNext(pC->getNext());
pC->setNext(rec);
display();
}
}
else
{
cout << "Cannot insert!!!\n";
}
return false;
}
I got a question from "char* genName()" function. I know how to generate 5 random characters, but i don't know how to generate a name of those 5 random characters. I mean how to store these 5 characters into a name.
Please help!!!
Thank you!!!
Dicky

