Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Generating a name of 5 random characters

I got the Database.h & Database.cpp as follow:

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
[3719 byte] By [dickylau] at [2007-11-11 10:26:39]
# 1 Re: Generating a name of 5 random characters
The following code is from Database.cpp

char* Database::genName()
{
char ch;
char* tempName = new char[5];
tempName[0] = '\0';
for (int i=1; i<6; i++)
{
ch = (char) ((rand()%26) + 97);
}
return tempName;
}

How to assign 5 random charcters into temoName??
Thank you!!
Dicky
dickylau at 2007-11-11 20:58:52 >
# 2 Re: Generating a name of 5 random characters
The following code is from Database.cpp

char* Database::genName()
{
char ch;
char* tempName = new char[5];
tempName[0] = '\0';
for (int i=1; i<6; i++)
{
ch = (char) ((rand()%26) + 97);
}
return tempName;
}

How to assign 5 random charcters into temoName??
Thank you!!
Dicky
add tempName[i] = ch ; inside the for loop
hope I have understood your question.
it career at 2007-11-11 20:59:57 >
# 3 Re: Generating a name of 5 random characters
Thank you very much!!! it's working now.......
I got one more question to ask as follow:

When I press 'i' to insert another new record, but this time the user input the characters and store the new record, how do i do it?? also, i want the new record that is inserted before the old record. When i put the following code into my programme, the output displayed some strange characters and after the record.

From Database.cpp
bool Database::insert(int pos, Record *rec)
{
Record *pC;
char *pName = new char;
int i = 0;
pC = pFirst;
if(pos >= numRecord)
{
cout << "Cannot insert!!!\n";
}
else
{
while (i<pos)
{
pC=pC->getNext();
i++;
}
rec = new Record;
rec->setName(pName);
rec->setNext(pC->getNext());
pC->setNext(rec);
}
return true;
}

From main.cpp (the following code is from switch case)
case 'i': case 'I':
{
char name1[6];
int pos1;
Record *r = new Record();
cout << "Name of Record? ";
cin >> name1;
cout << "Insert before Record#? ";
cin >> pos1;
char *NameInHeap = new char[6];
NameInHeap[0] = '\0';
strcpy(NameInHeap, name1);
db.insert(pos1, r);
}
break;

Thank's very much!!!
Dicky
dickylau at 2007-11-11 21:00:51 >
# 4 Re: Generating a name of 5 random characters
I did not see you are setting the name in the record before calling insert.
it career at 2007-11-11 21:01:57 >
# 5 Re: Generating a name of 5 random characters
Is it looks like the following code??

bool Database::insert(int pos, Record *rec)
{
Record *pC;
pC = pFirst;
int i = numRecord;
if(pos>= numRecord)
{
cout << "Cannot insert!!!\n";
}
else
{
while (i<pos)
{
pC = pC->getNext();
i--;
}
rec = new Record;
rec->setNext(pC->getNext());
pC->setNext(rec);
}
return false;
}

case 'i': case 'I':
{
char name1[6];
int pos1;
Record *r = new Record;
cout << "Name of Record? ";
cin >> name1;
cout << "Insert before Record#? ";
cin >> pos1;
char *NameInHeap = new char[6];
NameInHeap[0] = '\0';
strcpy(NameInHeap, name1);
r->setName(NameInHeap);
db.insert(pos1, r);
}
break;
dickylau at 2007-11-11 21:02:50 >