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

about generating random characters & linked list

I have two classes which are contained in two header files - one is called Record.h & the other is called Database.h. The follwoing codes are shown:

For Record.h
class Record
{
private:
char name[6];
Record *pNext;
public:
Record()
{
pNext = 0;
}
void setName(char *nm)
{
strncpy (name,nm,5);
name[5] = '\0';
}
char * getName()
{
return name;
}
void setNext(Record *pC)
{
pNext = pC;
}
Record * getNext()
{
return pNext;
}
};

For Database.h
#include "Record.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);
};

For Database.cpp (implementation of member functions in Database.h)
#include "Database.h"
#include <iostream>
using namespace std;
Database::Database()
{
numRecord = 0;
pFirst = 0;
}
Database::~Database()
{//destroy the Records//}
void Database::create(int n)
{ //create n records, each contains a name of 5 random characters//
Record *pH, *pT, *pL;
int i = 0;
pH = new Record;
pL = pH;
pL->setName(0);
for (; i<n; i++)
{
pT = new Record;
pL->setNext(pT);
pL = pT;
pL->setName();
}
numRecord = n;
}
void Database::display()
{
//show all the Records in the list//
}
bool Database::remove(char *recName)
{
//remove from the list a record whose name is recName; return true if removal is successful, otherwise, return false//
}
bool Database::insert(int pos, Record *rec)
{
//insert the Record rec before the pos-th Record in the list; return true if insertion is successful, otherwise, return false//
}

Question:
(1) How to implement the member functions from Database.h??
(2) How to generate a name of 5 random characters?
Plese Help!!!
Dicky
[2298 byte] By [dickylau] at [2007-11-11 10:23:13]
# 1 Re: about generating random characters & linked list
char ch[6] = {0};
int i;
for(i = 0; i < 5; i++)
ch = rand()%26 + 65; //random letters from 'A' to 'Z' -- 65 is 'A' so add 0-25 to 'A'

youll have to code the functions yourself, it looks like homework.
jonnin at 2007-11-11 20:58:56 >
# 2 Re: about generating random characters & linked list
Well since your going to be linking objects together you'd implement your Database functions via the . operator. Of course you still need to write them first.

EDIT: Oh never mind, I just saw the last class..so no . operator needed. You will just need to write the bodies to the functions and include the .h file.
Dark Rain at 2007-11-11 20:59:56 >
# 3 Re: about generating random characters & linked list
i have two codes which show two error messages & one warning message after I compile.

1st code: (one of the member functions in the class)
char Database::genName()
{
int ch;
for(int i = 0; i < 5; i++)
{
ch = (rand()%26) + 97;
cout << ch;
}
cout << endl;
}

1st error: 'char Database::genName(void)' : overloaded function differs only by return type from 'char *Database::genName(void)'
2nd error: 'Database::genName' : 'char (void)' differs in levels of indirection from 'char *(void)'

2nd code: (one of the member functions in other class)
void setName(char *nm)
{
strncpy (name,nm,5);
name[5] = '\0';
}
warning message: 'strncpy' was declared deprecated

Can someone explain more in detail, please??
Thanks!!
Dicky
dickylau at 2007-11-11 21:01:00 >
# 4 Re: about generating random characters & linked list
I have implemented the functions in the following class:
#include <iostream>
#include "Database.h"
using namespace std;
Database::Database()
{
numRecord = 0;
pFirst = 0;
}
Database::~Database()
{;}
void Database::create(int n)
{
Record *pH, *pT, *pL;
char *pInput;
pH = new Record;
pL = pH;
pL->setName(0);
for (int i = 0; i<n; i++)
{
pT = new Record;
pL->setNext(pT);
pL = pT;
pL->setName(pInput);
}
numRecord = n;
}
char Database::genName()
{
int ch;
for(int i = 0; i < 5; i++)
{
ch = (rand()%26) + 97;
cout << ch;
}
cout << endl;
}
void Database::display()
{
if (pFirst != 0)
{
Record *pTemp = pFirst;
do
{
cout << pTemp->getName() << endl;
pTemp = pTemp->getNext();
}
while (pTemp!=0);
}
else
{
cout << "list is empty" << endl;
}
}
bool Database::remove(char *recName)
{
}
bool Database::insert(int pos, Record *rec)
{
Record *pCurr;
int i=0;
char *pName;
pCurr = pFirst;
while (i<pos)
{
pCurr = pCurr->getNext();
i++;
}
rec = new Record;
rec->setName(pName);
rec->setNext(pCurr->getNext());
pCurr->setNext(rec);
if (rec!=0)
{
return true;
}
else
{
return false;
}
}

The main file is the follwoing code:
#include <iostream>
#include "Database.h"
using namespace std;
int main()
{
char choice;
do
{
cout << "(i) insert\n";
cout << "(r) remove\n";
cout << "(d) display\n";
cout << "(q) quit\n";
cout << ".........Input?";
cin >> choice;
cout << endl;

switch (choice)
{
char name1;
int pos1;
case 'i': case 'I':
{
cout << "Name of Record? ";
cin >> name1;
cout << "Insert before Record#? ";
cin >> pos1;
break;
}
case 'r': case 'R':
break;
case 'd': case 'D':
RecordList.display();
break;
}
}
while (!((choice == 'q')||(choice=='Q')));
return 0;
}

After I compile it, it has some errors as the previous reply. I saw the strange output as follow:
phjkk //some random charchers//
(i) insert
(r) remove
(d) display
(q) quit
..........Input? i //when i type i for instance

(i) insert
(r) remove
(d) display
(q) quit
..........Input?

The menu comes up again. what's wrong with my code?? Can someone explain in detail, please? I really want to know in depth since i am the newbee for C++ programming.

Thanks,
Dicky
dickylau at 2007-11-11 21:02:01 >