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

quick question about generating 5 random charcters and displaying 5 lines

I thought all the posible ways for generating 5 random charcters and displaying 5 lines (each line contains 5 characters). But none of them are working........Please help!!!

I know the following way for generating 5 random characters, but not displaying 5 lines (each line contains 5 characters). PLease help!!!

int ch;
for (int i=0; i<25; i++)
{
ch = ((rand()%26) + 97);
cout << ch;
}

Could someone tell me how to display in 5 lines and each line contains 5 random charatcers, please??

Thank you,
Dicky
[578 byte] By [dickylau] at [2007-11-11 10:25:07]
# 1 Re: quick question about generating 5 random charcters and displaying 5 lines
make your i in the forloop go from 1 to less than 26

Insert

if(i%5==0) // % is the modulo operator
cout <<endl;

after your cout << ch; statement, it will output a new line after every five chars
nspils at 2007-11-11 20:58:50 >
# 2 Re: quick question about generating 5 random charcters and displaying 5 lines
thanks a lots!! I can generate random names
dickylau at 2007-11-11 20:59:50 >
# 3 Re: quick question about generating 5 random charcters and displaying 5 lines
i got a function which is called "void create (int n)" from the class as follow:

void Database::create(int n)
{
numRecord = n;
Record *pH, *pT, *pL;
int i;
pH = new Record;
pL = pH;
pL->setName('\0');
for (i=1; i<n; i++)
{
pT = new Record;
pL->setNext(pT);
pL = pT;
pL->setName();
}
pFirst = pH;
}

I want to create n records and each record contains a name of 5 random characters. How can i do that?? Please help
Thank you!!
Dicky
dickylau at 2007-11-11 21:00:53 >
# 4 Re: quick question about generating 5 random charcters and displaying 5 lines
How about something like this?

I take it that your Record class has a data member which holds the randomly generated name and that this name is created by your setName() method which sets a name field.

void Database::create(int n)
{
numRecord = n;
Record *pH, //pointer at head node
*pT, // pointer to temporary node
*pL; // tail pointer, construction site pointer
int i;
pH = new Record;
pL = pH //initialize your building site pointer to the new head node
pH->setName( '\0' );

// either start this at 0 or iterate up to and including n to have
// n elements. If you start at 1 you'll have n-1 elements if you count
// up to but not including n
for ( i = 1; i <=n; i++ )
{
pT = new Record; // create new temporary
pL->setNext( pT ); // link the new node into the list at the tail
pL = pT; // move the construction site pointer to the new node
// which also happens to be the tail of the list
pT->setName(); // execute your method to create a name
}
pFirst = pH; // list is built, point your pFirst pointer to the empty
// head node
}

It looks to me that your intent was to build a list at the tail with an empty head node and then five records which contain lists of five names( or one name ). pT is the temporary node being added to the list, pL is your reference pointer that your are walking down your list, keeping track of the "construction site" (tail) of the list.
nspils at 2007-11-11 21:01:54 >
# 5 Re: quick question about generating 5 random charcters and displaying 5 lines
i have re-write the programme again, the code is shown as follow:
The first class as following:
class Record
{
private:
char name[6];
Record *pNext;
public:
Record()
{
pNext = 0;
}
void setName(char *nm)
{
strncpy_s (name,nm,5);
name[5] = '\0';
}
char * getName()
{
return name;
}
void setNext(Record *pC)
{
pNext = pC;
}
Record * getNext()
{
return pNext;
}
};
The second class as follow:
#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);
};
The implementation of the above class as following:
#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()
{
int ch;
char *pf = new char;
for (int i=1; i<6; i++)
{
ch = ((rand()%26) + 97);
cout << (char) ch;
if (i%5 == 0)
{
cout << endl;
}
}
return pf;
}
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)
{
return true;
}
bool Database::insert(int pos, Record *rec)
{
return true;
}
The main is shown as below:
#include <iostream>
#include "Database.h"
using namespace std;
int main()
{
Database db;
db.create(5);
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)
{
int pos1;
case 'i': case 'I':
{
char *name1 = new char;
Record *r = new Record();
cout << "Name of Record? ";
cin >> name1;
cout << "Insert before Record#? ";
cin >> pos1;
r->setName(name1);
db.insert(pos1, r);
}
break;
case 'r': case 'R':
break;
case 'd': case 'D':
{
db.display();
}
break;
}
}
while (!((choice == 'q')||(choice=='Q')));
return 0;
}

The whole programme can be compiled and when i press d for display the records, it doesn't show any random characters which are generated by rand function. The output is shown as below:
Record #0: (random chinese characters)
Record #1: (random chinese characters)
Record #2: (random chinese characters)
Record #3: (random chinese characters)
Record #4: (random chinese characters)

Please help me!!!
Thank you!!
Dicky
dickylau at 2007-11-11 21:02:58 >
# 6 Re: quick question about generating 5 random charcters and displaying 5 lines
why aren't you using strings?

anyway, you have the py pointer (which is returned by your genName method) but you don't assign anything to it, so it is pointing to who knows what.
nspils at 2007-11-11 21:04:04 >
# 7 Re: quick question about generating 5 random charcters and displaying 5 lines
But, how do i assign it?? Please give me a hand, please??
Thank you!!
Dicky
dickylau at 2007-11-11 21:05:02 >
# 8 Re: quick question about generating 5 random charcters and displaying 5 lines
you can use strcat but you need to have two char*

for( i = 0; i < 5; ++i )
{
char* newChar((rand()%26) + 97);
strcat( py, newChar, 1);
}

return py;

but I would use the string class, construct the string by building a char array

char newChars[5];
for( i = 0; i < 5; ++i )
{
newChars[i] = ((rand()%26) + 97);
}
string py(newChars);
return py;
nspils at 2007-11-11 21:06:05 >
# 9 Re: quick question about generating 5 random charcters and displaying 5 lines
its probably my fault, I tend to use raw char arrays for simple tasks and posted the howto make random letters code in that format.
jonnin at 2007-11-11 21:06:58 >
# 10 Re: quick question about generating 5 random charcters and displaying 5 lines
When assign these two functions into my "char *Database::genName()", it shows the error message which states that it can't be converted string into char*.

Does the following code have something wrong?
char * Database::genName()
{
char newChars [5];
for(int i = 0; i < 5; ++i )
{
newChars[i] = ((rand()%26) + 97);
}
string py(newChars);
return py;
}
dickylau at 2007-11-11 21:08:04 >
# 11 Re: quick question about generating 5 random charcters and displaying 5 lines
py will need to be a string rather than a char* for the method, as now written, to work. You would have to change the return type AND the declaration of py AND how it is used whereever you used it in your code - a "refactoring" of your code.

Otherwise, use the char* code that I suggested
nspils at 2007-11-11 21:09:04 >
# 12 Re: quick question about generating 5 random charcters and displaying 5 lines
I used your suggested char* solution, i still got 2 errors:
char * Database::genName()
{
for(int i = 0; i < 5; ++i )
{
char* newChar((rand()%26) + 97);
strcat(py, newChar, 1);
}
return py;
}

1st error: error C2440: 'initializing' : cannot convert from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

2nd error: error C2065: 'py' : undeclared identifier

For 2nd error: is it defined 'py' as string py;??

THank you!
Dicky
dickylau at 2007-11-11 21:10:02 >
# 13 Re: quick question about generating 5 random charcters and displaying 5 lines
Your name data field in the record class is a char array. Just keep it that way. So avoid the char* altogether in the

char[] Database::genName()
{
char tempName[5];
for(int i = 0; i < 5; ++i )
{
tempName[i] = (char)((rand()%26) + 97);

}
return tempName;
}

make sure to adjust the datatype in the calling method to expect the char array
nspils at 2007-11-11 21:11:03 >