【发布时间】:2015-05-09 14:31:33
【问题描述】:
我对 C 编程很陌生。我在编译我的程序时遇到了这个错误。我试图在互联网上寻找解决方案,但无济于事。报错就行了num = rand()%20;
char* getParameter(char *name)
{
char *num;
char *buffer;
if(strcmp(name,"Trainingsprogramm")){
srand (time(NULL));
num = rand()%20;;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"Personnenummer")){
srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"Tretzustand")){
srand (time(NULL));
srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"Tretleistung")){
srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"Drehzahl")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"Geschwindigkeit")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"GefahreneDistanz")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"RealeKJoule")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"AktuellerPuls")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"MomentanerGang")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"RelaxBetrieb")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}else if(strcmp(name,"VerbrauchteKJoule")){srand (time(NULL));
num = rand()%20;
sprintf(buffer,"%d",num);
}
return buffer;
}
【问题讨论】:
-
将
char *num;更改为int num;,为buffer分配内存,在main开头调用一次srand。 -
为什么要连续调用srand?
标签: c