我正在编写一个随机数生成器,我希望在程序内外输入。我在主要论点上遇到了麻烦。这是我的代码(对不起,它有点草率,但我只有15) 我的问题在于主要功能......
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int min;
int max;
int howMany;
int in;
int rnd();
void menu();
void getMin();
void getMax();
void getHowMany();
void getNumbers();
void quickRandTut();
void quickRand();
int main(int argc, char **argv[]){
int i;
int x;
srand(time(NULL));
if(argc > 1){
for(i = 0; i != argc; i++){
x = i + 1;
if(argv[i] == "-min") min = argv[x];
else if(argv[i] == "-max") max = argv[x];
else if(argv[i] == "-count") howMany = argv[x];
}
quickRand();
}
else if(argc == 1) menu();
return 0;
}
void menu(){
while(1){
system("clear");
printf("[1]Set Min.\n[2]Set Max.\n[3]Set How Many\n[4]Get Numbers\n[5]Quick Rand\n[6]Exit\n\nWhat would you like to do?\n>>>");
scanf("%d", &in);
if(in == 1) getMin();
else if(in == 2) getMax();
else if(in == 3) getHowMany();
else if(in == 4) getNumbers();
else if(in == 5) quickRandTut();
else if(in == 6) exit(0);
}
}
void getMin(){
int cont = 0;
while(cont == 0){
system("clear");
printf("What is the smallest number you want?\n>>>");
scanf("%d", &min);
if(min < 1){
system("clear");
printf("Your minimum must be at least 1...\n");
sleep(3);
}
else if(min > 0) cont = 1;
}
}
void getMax(){
int cont = 0;
while(cont == 0){
system("clear");
printf("What is the largest number you want?\n>>>");
scanf("%d", &max);
if(max < 1){
system("clear");
printf("Your maximum must be at least 1...\n");
sleep(3);
}
else if(max > 0) cont = 1;
}
}
void getHowMany(){
int cont = 0;
while(cont == 0){
system("clear");
printf("How many numbers would you like?\n>>>");
scanf("%d", &howMany);
if(howMany < 1){
system("clear");
printf("You must get at least 1 number...\n");
sleep(3);
}
else if(howMany > 0) cont = 1;
}
}
void getNumbers(){
int i;
int col = 0;
int num;
system("clear");
if(max < min){
system("clear");
printf("Your maximum must be larger than you minimum...\n");
sleep(3);
menu();
}
else if(max > min){
for(i = 0; i != howMany; i++){
col++;
num = rnd(min, max);
printf("%d. %d\n", col, num);
}
printf("\n\n[1]Continue\n>>>");
scanf("%d", &in);
menu();
}
}
int rnd(int min, int max){
return ((rand() % (max - min)) + min);
}
void quickRandTut(){
system("clear");
printf("You can use DSP's Random Number Generator by giving it external arguments.\n");
printf("You need to give it 3 arguments:\n\t-min #\n\t-max #\n\t-count #\n");
printf("\nHere is an example input and output:\n\nInput:\n\t/RAND -min 1 -max 10 -count 3\nOutput\n\t1. 3\n\t2. 9\n\t3. 4\n");
printf("\n-min #, -max #, and -count # arguments must be given in that order.\n\n[1]Menu\n>>>");
scanf("%d", &in);
menu();
}
void quickRand(){
int col;
int i;
int num;
system("clear");
for(i = 0; i != howMany; i++){
num = rnd(min, max);
printf("%d. %d\n", col, num);
}
}
当我编译它时,它告诉我:
rand.c: In function ‘main’:
rand.c:23:15: warning: comparison of distinct pointer types lacks a cast [enabled by default]
rand.c:23:30: warning: assignment makes integer from pointer without a cast [enabled by default]
rand.c:24:20: warning: comparison of distinct pointer types lacks a cast [enabled by default]
rand.c:24:35: warning: assignment makes integer from pointer without a cast [enabled by default]
rand.c:25:20: warning: comparison of distinct pointer types lacks a cast [enabled by default]
rand.c:25:41: warning: assignment makes integer from pointer without a cast [enabled by default]
当我运行它时,我给它以下参数:
/RAND -min 1 -max 100 -count 10
但它没有做任何事情。请以任何方式帮助。
答案 0 :(得分:3)
如其他答案中所述,将char **argv
更改为字符*argv[]
,这可以解决一件事。
其他错误存在,因为您平等对待字符串和整数。您无法使用>, <, >=, <=, ==
运算符在C中比较它们,而是使用字符串函数(在本例中为strncmp()
)。对于赋值也是如此,你不能将字符串赋给int,你需要转换它,尝试sscanf()
,atoi()
或任何其他类似的函数。
答案 1 :(得分:2)
尝试将char **argv[]
更改为字符*argv[]
在您收到错误的任何地方,将argv[x];
更改为*argv[x];
。
那是
if(argv[i] == "-min") min = *argv[x];
else if(argv[i] == "-max") max = *argv[x];
else if(argv[i] == "-count") howMany = *argv[x];
这是为了摆脱错误
答案 2 :(得分:0)
包括sleep
所需的标头:
#include <unistd.h>
修复不完整的原型:
int rnd(int, int); /* was an incomplete prototype */
main() is a mess
您需要利用atoi
或strtol
将参数值转换为数值,并且您的循环在编写时不安全。以下是它似乎需要的更改:
int main(int argc, char **argv){
int i;
// int x;
srand(time(NULL));
if(argc > 2){
for(i = 2; i < argc; i++){
// x = i + 1;
if ( strcmp (argv[i - 1], "-min") == 0 ) min = atoi (argv[i]);
else if (strcmp (argv[i - 1], "-max") == 0 ) max = atoi (argv[i]);
else if (strcmp (argv[i - 1], "-count") == 0 ) howMany = atoi (argv[i]);
}
quickRand();
}
else if(argc == 1) menu();
return 0;
}
我不清楚col
中quickRand()
应该是什么,但您至少需要在尝试打印之前将其初始化为某些内容。
通过这些更改,您的代码无需进一步警告即可编译。两个rules
会有所帮助。 (1) 始终初始化变量,(2) 始终在启用警告的情况下进行编译,({{1 }}) 最低限度。当你遇到其他障碍时发表评论。