Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的密码在哪里?_C++_Access Violation_Primes - Fatal编程技术网

C++ 我的密码在哪里?

C++ 我的密码在哪里?,c++,access-violation,primes,C++,Access Violation,Primes,当我试图运行这段在边界中查找素数的代码时,遇到访问冲突 int main () { cout << "Program initialized successfully. Please wait for the next message to appear." << endl << endl ; int Primes[51] ; int runner = 0 ; int chaser = 0 ; int bound =

当我试图运行这段在边界中查找素数的代码时,遇到访问冲突

int main () {
    cout << "Program initialized successfully. Please wait for the next message to appear." << endl << endl ;

    int Primes[51] ;
    int runner = 0 ;
    int chaser = 0 ;
    int bound = 0 ;
    int count = 0 ;

    cout << "Please enter the maximum boundary of the calculation : " ;

    cin >> bound ;

    cout << endl << "The number you've entered, " << bound << ", has been accepted. Please wait for the calculations." << endl ;

    if (runner <= bound ) {
        Primes[0] = 2;
        Primes[1] = 3;
        Primes[2] = 5;
        Primes[3] = 7;
        Primes[4] = 11;
        count = 4;

        for ( runner = 11 ; runner <= bound ; runner ++ ) {
            while ( runner % Primes[chaser] != 0 ) {
                for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++ ) {
                    if ( runner % Primes[chaser] == 0 ) {
                        count ++ ;
                        Primes[count] = runner;
                    }
                }
            }
        }

        int chaser_count;
        cout << "Here's the primes computer discovered : " << endl ;
        for ( chaser_count = 0 ; chaser_count <= count ; chaser_count ++ ) {
            cout << Primes[chaser_count] << endl ;
        }
        cout << "There is " << count << " primes discovered." << endl ;
    }
    return 0;
}
int main(){
cout此处:

for(chaser=0;Primes[chaser]!=0;chaser++){


您没有用0初始化
素数
数组,因此循环可以反复循环,chaser可以大于51(素数数组的大小),然后
素数[大于50的值]
将引发访问冲突。

调试器会告诉您崩溃发生的位置。您使用的是哪个IDE/编译器/调试器?在您的帐户中阅读了您对自己的描述后,我真的鼓励您为自己设置一个挑战,即如何在任何软件开发环境中使用调试器这样,当你遇到这样的崩溃时,你可以逐行逐行地检查代码,找出发生了什么。我想你会发现它是一个非常有用的技能,你会学到很多。你使用的C++环境是什么?@ SBI出于兴趣,为什么你从原始代码中删除所有的注释,但不能解释你为什么要这么做?@ ClareMacrae:commit注释的内容是“删除了不必要的注释”。他们对代码的混乱程度超过了他们帮助理解它的程度。用“this defines a variable”注释变量定义有什么好处?也许我本可以保留一些,但我太懒了,我想最好从这个简单的代码中删除一些,而不是全部保留。Shane,你可能想学习使用调试器单步执行代码。非常感谢。我在大脑中设置了一个限制,试图防止这种情况发生,即使绑定r当我测试它的时候,它确实很小。但是我似乎在我的代码中犯了一个错误,导致了代码中无限的if循环。