繁体   English   中英

我的C程序陷入了for循环

[英]My C program is stuck in a for loop

我正在尝试编写一个程序来计算用户提供的三角数。 当我运行它时,它陷入了for循环中,但仍然会打印出正确的答案,因为我的print语句不在循环中。 任何帮助是极大的赞赏。 我的代码如下:

#include <stdio.h>

int main (void)
{

    //Declare your variables

    int triangle, triNumber, i;


    //Assign values for known variables

    triNumber = 0;


    //Get the user to input the triangular number that they want

    printf("Please enter the triangular number you would like: ");
    scanf("%i\n", &triangle);


    //Execute for loop that will calculate the triangular number

    for (i = 1; i <= triangle; i = i + 1)
    {
        triNumber = triNumber + 1;
    }


    //Display the user's triangular number

    printf("The triangular number is %i\n", triNumber);

    return 0;

}

它不会停留在for循环中。 您必须从此更改scanf()语句:

scanf("%i\n", &triangle);

对此:

scanf("%i", &triangle);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM