找到一个数的除数

时间:2016-11-07 01:20:52

标签: c++ c++11

我试图创建一个程序来查找用户给出的数字的除数。我的程序正在编译,但它没有正常工作。

#include <iostream>

using namespace std ;

int main () {
    int a,x,y,z,p ;
    int d [z] ;
    y=0 ;
    cout << "Please give me the number you want me to check for dividors" << endl ;
    cin >> a ;
    for (x=2;x<a;x++){
        if(a%x==0)
            d[y]=x ;
            y++ ;
    }
    if (y==0){
        cout << a <<" has no dividors except itself and 1 and therefore, " << a <<" is a prime" << endl ;
    }
    else 
        cout << a <<" has " << y <<" dividors except itself and 1 and this dividors are :" << endl ;
        for (p=0;p<=y;p++){
            cout << d[p] <<endl ;
        }

    return 0 ;
} 

1 个答案:

答案 0 :(得分:0)

我会将此添加为评论,但我还没有足够的声誉。

无论如何,我认为问题出在你最后的for循环中。当p <= y是数组y中的除数数时,您将迭代到d[]d[y]超出范围,循环只应迭代p < y