gpt4 book ai didi

c - 接受主要论点

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:25 25 4
gpt4 key购买 nike

我正在编写一个随机数生成器,我希望在程序内外获取输入。我在处理主要论点时遇到了麻烦。这是我的代码(抱歉,有点马虎,但我只有 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

虽然它没有做任何事情。请尽您所能提供帮助。

最佳答案

如其他答案所述,更改 char **argv到炭化 *argv[] ,这解决了一件事。

其他错误的存在是因为您对字符串和整数一视同仁。您不能使用 >, <, >=, <=, == 在 C 中比较它们运算符,请改用字符串函数(在本例中为 strncmp() )。赋值也是一样,不能把字符串赋值给int,需要转换,试试sscanf() , atoi()或任何其他类似功能。

关于c - 接受主要论点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27336589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com