Problems with Pointers in functions.
I've been working on a way to create a random generator for ints and chars, but eventually I gave up and started working on a way to make what was already available (rand() from cstdlib).
I made the functions and put them in a class. I then wanted to make a function which would randomly generate a string, but because I can't return multiple chars, I decided to "point" to a string which was already declared. So far I've been having some problems with calling the function. Every time I do, I get an error saying "Argument of type "Char (*) [255]" is incompatible with parameter of type "char **"". To be honest, I haven't done too much string and pointer manipulation so i'm a bit stumped.
Heres the entire code: (sorry its a bit hard to read)
MainHeader.h
Code:
#include <cstdlib>
#include <sys/timeb.h>
class Rand
{
private:
//system time (__SYSTM) timeb variable for the srand() seed.
struct timeb __SYSTM;
public:
// Rand.Int(Minimum number, Maximum number)
// returns (0 <= __MIN <= NUMBER <= __MAX <= RAND_MAX)
// example call: Rand.Int(500,1500);
int Int(int __MIN, int __MAX)
{
if (__MIN > __MAX || __MIN < 0 || __MAX > RAND_MAX)
return -1;
//gets the system time, and seeds it for rand()
ftime(&__SYSTM);
srand(__SYSTM.millitm);
//returns a number between the min num and the max num starting from the min num
//eg (100, 150) would return the random number between 100 and 150
return ((rand() % (__MAX - __MIN) ) + __MIN);
}
//Rand.Char(Capitals)
//returns (CHAR) or (char) or random depending on caps variable
//example call: Rand.Char(0);
char Char(int __CAPS)
{
//gets the system time , and seeds it for rand()
ftime(&__SYSTM);
srand(__SYSTM.millitm);
//String Buffer is for the randomization of chars, containing lower case at 0-25 and UPPERCASE at 26-52.
char __SBUF[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Caps > 0 means CAPITALS
if (__CAPS > 0)
return __SBUF[(rand() % 26) + 26];
//Caps < 0 means lowercase
else if (__CAPS < 0)
return __SBUF[rand() % 26];
//Caps == 0 means RaNdOm CaPs.
else
return __SBUF[rand() % 52];
}
//Rand.String(Storage Buffer, Chars to be generated, Size of Buffer, Capitals)
//returns (STRING), (string) or random depending on caps variable and length
//example call: Rand.String(&Buffer, 10, sizeof Buffer, 0);
int String(char *__BUFR[], int __CHARS, int __SIZOF, int __CAPS)
{
//count for the loop, and String Buffer for the Characters.
int __COUNT = 0;
char __SBUF[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (__SIZOF <= __CHARS || *__BUFR == NULL || __CHARS < 0 || __SIZOF < 0)
return -1;
//from 0 to CHARS, put a random character into *Buffer
for(__COUNT; __COUNT <= __CHARS; __COUNT++)
{
//get system time, and seed rand()
ftime(&__SYSTM);
srand(__SYSTM.millitm);
//Caps > 0 means UPPERCASE
if(__CAPS > 0)
*__BUFR[__COUNT] = __SBUF[(rand() % 26) + 26];
//Caps < 0 means lowercase
else if (__CAPS < 0)
*__BUFR[__COUNT] = __SBUF[(rand() % 26) + 26];
//Caps == 0 means RaNdOm CaPs.
else
*__BUFR[__COUNT] = __SBUF[rand() % 52];
}
//NULL termination
*__BUFR[__COUNT] = '\0';
return 0;
}
} Rand;
Main.cpp
Code:
#include <iostream>
#include "MainHeader.h"
using namespace std;
char Buffer[255];
int main()
{
Rand.String(&Buffer,10, sizeof Buffer, 0);
cout << Buffer << endl;
return 0;
}