c/c++语言开发共享如何限制用户无法在c中输入0

我有以下函数来过滤整数值并重新提示用户。

int checkInput0(void){ int option0,check0; char c; do{ printf("Enter the amount of triangles you want to check: n"); if(scanf("%d%c",&option0,&c) == 0 || c != 'n'){ while((check0 = getchar()) != 0 && check0 != 'n' && check0 != EOF); printf("[ERR] Invalid number of triangles.n"); }else{ break; } }while(1); // printf("returning the value of option, which is %f", option); return option0; 

但是,我想将此函数扩展为过滤0。

我似乎错过了一些东西。 非常感谢所有帮助。

提前致谢!

    发布的代码没有正确检查’scanf()’返回的值,并且没有完全执行所需的function。

    以下提议的代码:

    现在建议的代码:

     do { printf("Enter the amount of triangles you want to check: n"); if( scanf( "%d", &option0 ) == 1 ) { if( 0 >= option0 ) { // 0 or negative value entered printf("[ERR] Invalid number of triangles.n"); continue; } // empty stdin while( (check0 = getchar()) != EOF && check0 != 'n' ); // exit while() loop break; } else { // scanf failed perror( "scanf for number of triangles failed" ); exit( EXIT_FAILURE ); } } while(1); 

      以上就是c/c++开发分享如何限制用户无法在c中输入0相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

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

      ctvol管理联系方式QQ:251552304

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

      (0)
      上一篇 2020年12月9日
      下一篇 2020年12月9日

      精彩推荐