问题描述
我是编程和 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 的阅读/理解(long int 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 '
' and ' ') = 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
");
printf("--------------
");
printf("Enter a positive integer (1 - 1000000):
");
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
");
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
");
run_perf_square(option_stats);
}
else if(ptr != NULL)
{
printf("Invalid Input! PLease enter a positive integer between 1
and 1000000
");
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
(' '
) 字符串终止符.
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
(' '
) string terminator.
if (*ptr == ' ') {
// 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 也需要
num
您还可以通过一些重组和逻辑调整将您的 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 中的整数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!