只要输入用户输入,C程序就会崩溃

时间:2016-12-10 14:02:03

标签: c crash

所以我有一个任务来制作一个程序来检查一个数字是否完美。我没有写任何问题或理解它,但每当输入一个数字时程序崩溃。我错过了什么吗?

#include <stdio.h>
#include <stdlib.h>

#define ZERO 0
/*
    This function gets input of type int and checks if that number is perfect.
*/
int main(void)
{
    int num=ZERO, dividersTotal=ZERO, i=ZERO;
    printf("Please enter a number:");
    scanf("%d", &num);
    for(i=ZERO; i<num; i++) // Looks through all the numbers that are smaller than num and bigger than 0, if a number divides num, it adds it to dividersTotal
    {
        if(ZERO==(num%i))
        {
            dividersTotal+=i;
        }
    }
    if(dividersTotal==num) // If the total of all the dividers of a number equals to that number, then it is perfect.
    {
        printf("The number is perfect");
    }
    else
    {
        printf("The number is not perfect.");
    }
    system("PAUSE>NUL");
    return 0;
}

谢谢! (请记住我使用gcc编译,所以事情可能很奇怪)

1 个答案:

答案 0 :(得分:0)

if(ZERO==(num%i))除以零。                     –EOF

i=ZERO;-> i = 1;                     –́BLUEPIXY