gpt4 book ai didi

C - 我的函数适得其反

转载 作者:行者123 更新时间:2023-11-30 15:49:59 24 4
gpt4 key购买 nike

当我调用这个函数时,它总是返回 false 并停止程序。但我不明白为什么。

// checks to see if the second argument is a positive interger, 
// and that there is only one arg
if (ArgCheck (argc, shift) == false)
{
printf("You may only input one non-negative argument.\n");
return 1;
}
else
return true;

函数ArgCheck是:

int ArgCheck (int a, int b)
{
if (a > 2)
{
return false;
}
else if (b < 0)
{
return false;
}
else
return 0;
}

即使我在命令行中输入正确的参数,它仍然返回 false。我知道这是一个菜鸟问题,但我感谢您的帮助。

谢谢。

最佳答案

int ArgCheck (int a, int b)
{
if (a > 2)
{
return false;
}
else if (b < 0)
{
return false;
}
else
return 0;
}

该函数始终返回 false。 0false 相同。您的要求是:

return true if the second argument is a positive integer and that there is only one arg

所以...

bool ArgCheck(int argCount, int value)
{
return argCount == 1 && value >= 0;
}

请注意,我不允许参数计数为 0,因为您说需要一个参数。您当前的代码不允许使用参数。

关于C - 我的函数适得其反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16004652/

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