高手请看--莫名其妙的bug求解
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define passwordLength 9 //including the last char '\0'
//generate 8-bit password
char* GeneratePassword()//unnessesary to change, so return the point
{
char* password = new char[passwordLength];//password vector
for(int i = 0; i < passwordLength; i++)
{
password[i] = '\0';
}
char passwordElements[62] = {'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'};
srand((unsigned int)time(NULL));
int index = 0;
for(i = 0; i < passwordLength-1; i++)
{
index = rand() % 61;//generate an int from 0 to 61
password[i] = passwordElements[index];
}
return password;
}
//test process
int main(void)
{
char* test = NULL;
//test = GeneratePassword();
for(int j = 0; j < 10; j++)
{
test = GeneratePassword();
printf("%s\n", test);
}
//printf("%s\n", test);
delete[] test;
return 0;
}
本程序产生10个不同的随机8位密码
debug的时候发现产生的不一样
但是打印的都一样
请高手帮忙改错
本程序在vc++ 6.0下可以运行