c/c++语言开发共享为什么我的程序循环次数太多?

我是C的初学者,试图创建一个程序并且我的主要function有问题。

问题:

码:

 #include  #include #include "a3defs.h" int main() { StackType stk; StackType *stkPtr = &stk; //Will be used to check whether to use recursive or iterative int method = 0; int sum; int *sumPnt = ∑ //Will be used to create array for amount of ints: int numOfIntegers; //Array of ints: int *userInts; printf("How many integers would you like to enter? "); scanf("%d", &numOfIntegers); userInts = (int*)calloc(numOfIntegers, sizeof(int)); //Create the array printf("Please enter %d numbers: n", numOfIntegers); int i; for (i = 0; i < numOfIntegers; i++) { scanf("%dn", &userInts[i]); printf("Next:"); } while(1) { printf("Would you like to used iterative or recursive to sum?n"); printf("Enter 1 for iterative or 2 for recursive: "); scanf("%dn", &method); if (method == 1) { //found in loop.c sumIterative(stkPtr, numOfIntegers, userInts, sumPnt); break; } else if (method == 2) { //Found in loop.c sumRecursive(stkPtr, numOfIntegers, userInts, sumPnt); break; } else { printf("Invalid choice. Repeating... n"); continue; } } printf("Your sum is: %d", *sumPnt); return 0; } 

    替换scanf("%dn", &userInts[i]); with scanf("%d", &userInts[i]);

    请参阅有关在scanf中以格式说明符输入非空白字符的信息。

    它说:

    任何不是空格字符(空格,换行符或制表符)或格式说明符的一部分(以%字符开头)的字符都会导致该函数从流中读取下一个字符,将其与此非空白字符进行比较如果它匹配,则将其丢弃,并且该函数将继续使用格式的下一个字符。 如果字符不匹配,则函数失败,返回并保留流的后续字符未读。

    问题1:

    只需更换:

     scanf("%dn", &userInts[i]); 

    by scanf("%d", &userInts[i]);

    并在格式字符串不以n结尾的所有printf语句之后添加fflush(stdout) 。 否则,只有在输出下一个n后才会显示输出。

      以上就是c/c++开发分享为什么我的程序循环次数太多?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

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

      ctvol管理联系方式QQ:251552304

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

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

      精彩推荐