我正在构建一个程序,该程序使用值的ascii tabe随机生成密码,并且只能包含每个字符中的一个。它生成一个8字符长的密码。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define SIZE 10
char Charactor(char x);
void Check(char* y);
int main()
{
char string[SIZE];//defines varriables
char* point;
point=&string[SIZE];
srand(time(NULL));
for (int i=0;i<SIZE-1;i++)//empties out the string
{
string[i]='\0';
}
for (int i=0;i<SIZE-2;i++)//randomizes a char for each space in the string
{
string[i]=Charactor(*point);
}
Check(point);/checks the string for duplicated values
printf("%s\n",string);//prints string on screen
}
char Charactor(char x)
{
int rnd=0;
rnd=rand()%2;//chooses char or number using ascii
if (rnd==0)
{
rnd=rand()%10+48;
}
else
{
rnd=rand()%26+65;
}
x=(char)rnd;
return x;
}
void Check(char* y)
{
int run=0;
for (int i=0; i<SIZE-2;i++)
{
for (int x=0; x<SIZE-2; x++)
{
if (y[i]==y[x] && run=0)
{
run++;
continue;
}
else
{
y[i]='\0';
y[i]=Charactor(*y);
}
}
}
return;
}通过这些更改,代码现在正在运行,我只需弄清楚如何更改正确的值,这样就不会有任何重复。
发布于 2013-09-06 17:51:17
修正:
char* point =&string[0]; //Make it to point to first element
由于您的Charactor(*point);实际上没有执行任何基于*point的操作,所以您可能会使用Check(point);从字符串开始启动扫描。
和
if (y[i]==y[x] && run==0)
^^Use Equality check不能将boolean的y[i]==y[x] && run结果修改为零。
注意:但是,if (y[i]==y[x] && (run=0) )不会抛出此错误。
发布于 2013-09-06 17:53:56
您的错误似乎是错误地将run=0设置为
if (y[i]==y[x] && run=0)这是最有可能混淆编译器的部分。不需要对Y做任何事。
修正为:
if (y[i]==y[x] && run==0)https://stackoverflow.com/questions/18663581
复制相似问题