Several issues with your code:
cout < charStr;
should be:
cout << charStr;
If you compile with the g++ -Wall argument (warning all), that error becomes easily apparent.
Also, you never set the value of stringLength! This is one example of why you generally should not use global variables--it can be hard to keep track of them. The unset value of stringLength could do weird things depending on your compiler--many compilers will simply initialize the value to 0, but some will set it to a random value. This undefined behavior can cause major headaches, so be very careful about that, and try to always initialize your variables when appropriate (this is generally a bigger issue with pointers, but the problem can still remain for other variables).
A fixed program is below:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
char genRandom()
{
return alphanum[rand() % stringLength];
}
int main()
{
while(true)
{
cout << genRandom();
}
return 0;
}
Still using global variables, but I think this is a bit more of an appropriate use of them. I'm not sure what you were trying to accomplish by having the global char* string, just a headache waiting to happen and not really giving any advantages in your code. Generally in C++ it's better to use the C++ standard library string when you can--though in this case, your code really didn't need strings.