求大神帮忙c++随机生成字符串的有关问题

求大神帮忙c++随机生成字符串的问题
楼主正在学习数据结果的串这一章,要生成一个随机的串,于是。。。



void MySqString::RandCreat()
{
srand((unsigned)time(NULL));
const int n=62;  // 10+26+26
char SS[n]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    length=rand()%15;
int j=0;
for(int i=0;i<length;i++)
{    

ch=new char[length];
assert(ch!=0);
j=rand()%n;
ch[i]=SS[j];
}

}  //随机生成串   这个是随机生成的函数

void SqString::display(ostream& out) const
{
for(int i=0;i<length;++i)
{
  out<<ch[i];
}
   
cout<<endl;
}   //这个是输出   
然后,然后结果生成的是这个 ,,,,
求大神帮忙c++随机生成字符串的有关问题

随机了好几次都是屯屯屯加一个字母,请问为什么啊,我是菜鸟。。什么都不知道




------解决方案--------------------
ch=new char[length];
这段代码放到外边,因为不是每次都需要分配的
------解决方案--------------------
1.for(int i=0;i<length;++i)
{
   out<<ch[i];
}
这个改为
.for(int i=0;i<length;i++)
{
   out<<ch[i];
}
------解决方案--------------------
#include <iostream>
#include <assert.h>
#include <cstdlib>
class SqString
{
private:
        char *ch;
        int length;
public:
        void RandCreat();
        void display() const;
};
void SqString::RandCreat()
{
        srand((unsigned)time(NULL));
        const int n=62;  // 10+26+26
        char SS[n]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        length=rand()%15;
        int j=0;
        ch = new char[length];
        assert(ch!=0);
        for(int i=0;i<length;i++)
        {
                j=rand()%n;
                ch[i]=SS[j];
        }

}  //随机生成串   这个是随机生成的函数

void SqString::display() const
{
        for(int i=0;i<length;++i)
        {
                std::cout<<ch[i];
        }

        std::cout<<std::endl;
}
按照你的样子修改了一下,但是这个程序还有很多地方有问题,比如ch没有delete
------解决方案--------------------
仅供参考
//产生一个包含字母、数字、以及其他字符的字符串
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CN  8 //大写英文字母个数
#define LN  8 //小写英文字母个数
#define NN  8 //数字个数
#define SN  8 //符号个数
char t[CN+LN+NN+SN+1];
char S[33]="!\"#$%&'()*+,-./:;<=>?@[\\]^_`{
------解决方案--------------------
}~";
char *myrandstr() {
    int i,n,a,b;
    char tc;

    for (i=0;i<CN;i++) sprintf(t         +i,"%c",'A'+rand()%('Z'-'A'+1));
    for (i=0;i<LN;i++) sprintf(t+CN      +i,"%c",'a'+rand()%('z'-'a'+1));
    for (i=0;i<NN;i++) sprintf(t+CN+LN   +i,"%c",'0'+rand()%('9'-'0'+1));
    for (i=0;i<SN;i++) sprintf(t+CN+LN+NN+i,"%c",S[rand()%32]          );

    n=CN+LN+NN+SN-1;
    for (i=n;i>0;i--) {
        a=i-1;b=rand()%i;
        if (a!=b) {tc=t[a];t[a]=t[b];t[b]=tc;}
    }
    return t;
}
int main() {
    int i;

    srand(time(NULL));
    for (i=0;i<10;i++) printf("%s\n",myrandstr());
    return 0;
}
//iWQ*RG7r2)u'9lAJ9t2Vl03d)Ht"5-@/
//^dQPe_R>00Z6H6x3+klGS2=Ewqf/86^)
//u>2oR=55?aaj4OXG0:9rX#6+Dv6DDt-=