int main()
{	int num=2;
	int i=0;
	int user_num,x;
	cout<<"enter a number(number>4)"<<endl;
	cin>>user_num;
	int a[100];
	for (i=0;i<100;i++)
	{
		a[i]=0;
	}
	while(num<=user_num)
	{
		x=2;
		while(x<=num)
		{
			if(num%x==0)
			{
				break;
			}
			x++;
		}
				
			if(x==num)
			{
			   a[i]=num;
			   i++;
			}
			
	num++;
	}

	int b[100];
	for (i=0;i<100;i++)
	{
		b[i]=a[i];
	}
	for (i=0;i<100;i++)
	{
		for(int j=1;j<100;j++)
		{
			if(a[i]+b[j]==user_num)
			{
				cout<<a[i]<<","<<b[j];
			}
		}
	}

        char f;
	cin>>f;
	return 0;
}

after entering the number no output appears & a dialog box appears mentioning that " Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted

Recommended Answers

All 2 Replies

you need to reset your i variable back to zero after your for loops that start on lines 8 and 34. By the way what exactly are you trying to accomplish here?

Well, you need to reset i to 0 before line 12. Your code will be easier to read if you create a few simple (and well-named) functions that do specific things like:

bool isPrime(int num)
{
    bool is_prime = True
    // loop to determine if num is a prime
    // if NOT prime, set is_prime = False

    return is_prime;
}

You definitely aren't checking your array-bounds ... you're limiting 'a' to contain up to 100 primes, but because you don't reset 'i' then you're writing past that point in the array at line 26.

Once you fix that, you have a logic problem at line 24, so if you specify a sufficiently-large user_num, you'll still have problems....

And ultimately, you need to check the value of 'i' and print an error msg to the user if you've already found 100 primes and aren't all the way up to user_num yet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.