问题描述
我是编程和C语言的新手,目前正在大学学习.这是一项作业,因此我想避免直接回答,但会更多地遵循正确方向的提示或提示/推动.
I am new to programming and to C in general and am currently studying it at university. This is for an assignment so I would like to avoid direct answers but are more after tips or hints/pushes in the right direction.
我正在尝试使用strtol验证键盘输入,更具体地说,测试输入是否为数字.我在这里和其他网站上查看了其他问题,并且按照其他用户的指示进行操作,但这并没有帮助我.
I am trying to use strtol to validate my keyboard input, more specifically, test whether the input is numeric. I have looked over other questions on here and other sites and I have followed instructions given to other users but it hasn't helped me.
据我所读/理解的 strtol (长整数strtol(const char * str,char ** endptr,int base);)如果 endptr 是不是空指针,该函数会将 endptr 的值设置为数字后的第一个字符.
From what I have read/ understand of strtol (long int strtol (const char* str, char** endptr, int base);) if the endptr is not a null pointer the function will set the value of the endptr to the first character after the number.
因此,如果我要输入84948ldfk,则 endptr 将指向'l',告诉我输入中除了数字以外还有其他字符,这将使其无效.
So if I was to enter 84948ldfk, the endptr would point to 'l', telling me there is characters other than numbers in the input and which would make it invalid.
但是,对于我来说,发生的事情是,无论我输入什么内容,我的程序都将返回无效输入.这是我的代码:
However in my case, what is happening, is that no matter what I enter, my program is returning an Invalid input. Here is my code:
void run_perf_square(int *option_stats)
{
char input[MAX_NUM_INPUT + EXTRA_SPACES]; /*MAX_NUM_INPUT + EXTRA_SPACES are defined
*in header file. MAX_NUM_INPUT = 7
*and EXTRA_SPACES
*(for '\n' and '\0') = 2. */
char *ptr;
unsigned num=0; /*num is unsigned as it was specified in the start up code for the
*assignment. I am not allow to change it*/
printf("Perfect Square\n");
printf("--------------\n");
printf("Enter a positive integer (1 - 1000000):\n");
if(fgets(input, sizeof input, stdin) != NULL)
{
num=strtol(input, &ptr, 10);
if( num > 1000001)
{
printf("Invalid Input! PLease enter a positive integer between 1
and 1000000\n");
read_rest_of_line(); /*clears buffer to avoid overflows*/
run_perf_square(option_stats);
}
else if (num <= 0)
{
printf("Invalid Input! PLease enter a positive integer between 1
and 1000000\n");
run_perf_square(option_stats);
}
else if(ptr != NULL)
{
printf("Invalid Input! PLease enter a positive integer between 1
and 1000000\n");
run_perf_square(option_stats);
}
else
{
perfect_squares(option_stats, num);
}
}
}
任何人都可以在正确的方向帮助我吗?显然,该错误与我的 if(ptr!= NULL)条件有关,但据我所知,这似乎是正确的.正如我所说,我已经看过以前与此类似的问题,并在答案中采纳了建议,但它似乎对我不起作用.因此,我认为最好根据自己的情况寻求帮助.
Can anyone help me in the right direction? Obviously the error is with my if(ptr != NULL) condition, but as I understand it seems right. As I said, I have looked at previous questions similar to this and took the advice in the answers but it doesn't seem to work for me. Hence, I thought it best to ask for my help tailored to my own situation.
提前谢谢!
推荐答案
您正在以错误的顺序检查strtol
的结果,首先检查ptr
,也不要针对NULL
检查ptr,推论并检查它是否指向NUL
('\0'
)字符串终止符.
You're checking the outcome of strtol
in the wrong order, check ptr
first, also don't check ptr against NULL
, derference it and check that it points to the NUL
('\0'
) string terminator.
if (*ptr == '\0') {
// this means all characters were parsed and converted to `long`
}
else {
// this means either no characters were parsed correctly in which
// case the return value is completely invalid
// or
// there was a partial parsing of a number to `long` which is returned
// and ptr points to the remaining string
}
num > 1000001
也必须是num > 1000000
num < 0
也必须是num < 1
您还可以通过一些重组和逻辑调整将if语句的序列折叠为仅一个无效的分支和一个好的分支.
You can also with some reorganising and logic tweaks collapse your sequence of if statements down to onlya single invalid branch and a okay branch.
这篇关于使用strtol验证ANSI C中的整数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!