c/c++语言开发共享使用strtolvalidationANSI C中的整数输入

我是编程和C的新手,我目前正在大学学习。 这是一个任务,所以我想避免直接的答案,但更多的提示或提示/推动正确的方向。

我试图使用strtol来validation我的键盘输入,更具体地说,测试输入是否为数字。 我已经查看过这里和其他网站上的其他问题,我已经按照给其他用户的说明,但它没有帮助我。

从我读取/理解的strtol (long int strtol(const char * str,char ** endptr,int base);)如果endptr不是空指针,该函数将endptr的值设置为第一个字符之后的号码。

因此,如果我要输入84948ldfk,则endptr将指向’l’,告诉我输入中除了数字之外还有其他字符会使其无效。

然而在我的情况下,正在发生的事情是,无论我输入什么,我的程序返回一个无效的输入。 这是我的代码:

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 '') = 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 Squaren"); 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 1000000n"); 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 1000000n"); run_perf_square(option_stats); } else if(ptr != NULL) { printf("Invalid Input! PLease enter a positive integer between 1 and 1000000n"); run_perf_square(option_stats); } else { perfect_squares(option_stats, num); } } } 

谁能帮助我朝正确的方向发展? 显然错误是我的if(ptr!= NULL)条件,但据我所知它似乎是正确的。 正如我所说,我已经查看过类似的问题,并在答案中提出了建议,但似乎对我没有用。 因此,我认为最好根据自己的情况寻求帮助。

提前致谢!

编辑:好的,我让它工作,经过几次测试,它似乎正常工作。 这是我的if语句的问题。 到目前为止,下面的代码对我有用:

 if((input[0] != 'n' && (*ptr == '' || *ptr == 'n')) && num  0) { perfect_squares(option_stats, num); } else { printf("Invalid Input! PLease enter a positive integer between 1 and 1000000n"); if(num > 1000001) { read_rest_of_line(); } run_perf_square(option_stats); } 

我可能会清理if语句,使其更容易混淆,但对目前的结果感到满意。

    你正在以错误的顺序检查strtol的结果,首先检查ptr ,也不要检查ptr对NULL ,对它进行修改并检查它是否指向NUL'' )字符串终止符。

     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 < 0也需要为num < 1

    您还可以通过一些重组和逻辑调整将if语句序列折叠为仅一个无效分支和一个okay分支。

    OP希望避免直接回答….

    validation整数输入

    Qapla”

    函数strtol返回long int,这是一个带符号的值。 我建议你使用另一个变量(entry_num),你可以测试<0,从而检测负数。

    我还建议正则表达式可以测试数字和有效输入的字符串输入,或者你可以使用strtok和除数字之外的任何东西作为分隔符;-)或者你可以使用validation扫描输入字符串,如:

     int validate_input ( char* input ) { char *p = input; if( !input ) return 0; for( p=input; *p && (isdigit(*p) || iswhite(*p)); ++p ) { } if( *p ) return 0; return 1; } 

      以上就是c/c++开发分享使用strtolvalidationANSI C中的整数输入相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/550148.html

      (0)
      上一篇 2021年1月14日
      下一篇 2021年1月14日

      精彩推荐