### Program crashes unless I run it with the debugger

I wrote a prime factorization program and it crashes in the while loop. I just get a "this program is not responding" message. The part in the while loop runs because it writes the messages to the screen, but nothing after it. The primes.txt file contains a list of all the prime numbers from 1 to 10000.
Here's the code:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include #include using namespace std; int main() { int *primes; int *array1; ifstream inf ("primes.txt"); int c=1230; primes = new int [c]; int iii=0; for (int iii=0;iii<=c;iii++) { std::string Text; getline(inf,Text); stringstream convert(Text); convert >> primes[iii]; } int n=0; array1 = new int [10000]; for (int iii=0;iii<=10000;iii++) array1[iii]=0; cout<<"Type in a whole number between 1 and 10 000"<>n; int remainder=n; iii=0; while (iii<=c) { if (remainder%primes[iii]==0) { cout<<"Found match:"<
In the following code you are trying to access your array out of bounds:
 ``12`` ``````array1 = new int [10000]; for (int iii=0;iii<=10000;iii++) array1[iii]=0;``````

Arrays in C/C++ start at zero and stop at size - 1. You are not stopping until size. You have this problem throughout your code. Normally when dealing with arrays in for loops you use the operator< not the operator<= and you start the loop at zero not one.
I changed it, but I'm still having the same problem:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include #include using namespace std; int main() { int *primes; int *array1; ifstream inf ("primes.txt"); int c=1230; primes = new int [c]; int iii=0; for (int iii=0;iii> primes[iii]; } int n=0; array1 = new int [10000]; for (int iii=0;iii<10000;iii++) array1[iii]=0; cout<<"Type in a whole number between 1 and 10 000"<>n; int remainder=n; iii=0; while (iii
Topic archived. No new replies allowed.