the program has stopped working

Hi everyone! I have really big problem. I wrote myself a program which checks if number that you input is prime number. At first you have to write down how many numbers You would like to check. After compilation program runs. I input:

5
1
11
10
997
88

and at the end of the program there appears a message box with message:
the program has stopped working. I really dont know why :( plz help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include<iostream>
#include<cmath>
using namespace std;

void function()
{
	int n=0;
	int *Tab = new int(n);//number which user gives
	int *TabPierw = new int(n);//sqrt for each number

	//write down, how many numbers would u like to check
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> Tab[i];
	}

	for (int i = 0; i < n; i++)
	{
				
			TabPierw[i] = (int) pow((double) Tab[i],0.5);

			for (int j = 2; j <= sizeof(TabPierw[i]); j++)
			{
				if (Tab[i] == 1)
				{
					cout << "NIE" << endl;
					break;
				}
				if ((Tab[i] == 2) || (Tab[i] == 3) || (Tab[i] == 5))
				{
					cout << "TAK" << endl;
					break;

				}
				if (Tab[i] % j == 0)
				{
					cout << "NIE" << endl;
					break;

				}
				if (Tab[i] % j != 0)
				{
					cout << "TAK" << endl;
					break;
				}

			}	
	}

	delete [] Tab;
	delete [] TabPierw;
}


int main()
{
	function();
	system("pause");
	return 0;
}
new int(n) allocates one integer that has the value of n.
new int[n] allocates n integers that have undefined values.
Ok i changed some things but now i have another problem.. Something with algorithm is wrong.. I tested input this:

7
88
88
88
88
999
1
777

and output looks like this:

NO
NO
NO
NO
YES
NO
YES

I don't know how is it possible... I'm fighting with this code so many hours ;(

#include<iostream>
#include<cmath>
using namespace std;

void function()
{
int n=0;
//write down, how many numbers would u like to check
cin >> n;
int *Tab = new int[n];//number which user gives
int *TabPierw = new int[n];//sqrt for each number



for (int i = 0; i < n; i++)
{
cin >> Tab[i];
}

for (int i = 0; i < n; i++)
{

TabPierw[i] = (int) pow((double) Tab[i],0.5);

for (int j = 2; j <= sizeof(TabPierw[i]); j++)
{
if (Tab[i] == 1)
{
cout << "NO" << endl;
break;
}
if ((Tab[i] == 2) || (Tab[i] == 3) || (Tab[i] == 5))
{
cout << "YES" << endl;
break;

}
if (Tab[i] % j == 0)
{
cout << "NO" << endl;
break;

}
else
{
cout << "YES" << endl;
break;
}

}
}

delete [] Tab;
delete [] TabPierw;
}


int main()
{
function();
system("pause");
return 0;
}
sizeof(TabPierw[i]) gives the size of TabPierw[i] in bytes.

TabPierw[i] is an int so it will give you the size of an int (usually 4).
Ok thx, You have right.. i changed that but still have problems

i used the same input:

7
88
88
88
88
999
1
777

And now i get:

NO
NO
NO
NO
YES
YES

It's crazy!! :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<cmath>
using namespace std;

void function()
{
	int n=0;
	//write down, how many numbers would u like to check
	cin >> n;
	int *Tab = new int[n];//number which user gives
	int *TabPierw = new int[n];//sqrt for each number



	for (int i = 0; i < n; i++)
	{
		cin >> Tab[i];
	}

	for (int i = 0; i < n; i++)
	{
				
			TabPierw[i] = (int)round( pow((double) Tab[i],0.5));

			for (int j = 2; j <= TabPierw[i]; j++)
			{
				if (Tab[i] == 1)
				{
					cout << "NO" << endl;
					break;
				}
				if ((Tab[i] == 2) || (Tab[i] == 3) || (Tab[i] == 5))
				{
					cout << "YES" << endl;
					break;

				}
				if (Tab[i] % j == 0)
				{
					cout << "NO" << endl;
					break;

				}
				else 
				{
					cout << "YES" << endl;
					break;
				}

			}	
	}

	delete [] Tab;
	delete [] TabPierw;
}


int main()
{
	function();
	system("pause");
	return 0;
}
Last edited on
Please could you use code tags when posting code, to make it readable? You did it in your OP, and it would help if you could do it in your other posts.
ok i will use code tags, sry for that.. help plz
If you input 1 through 3, then j <= TabPierw[i] on line 25 will not be true and there will be no output for those numbers.

Your inner for loop isn't really a loop. There is no path through the loop without hitting a break which ends the loop.
Last edited on
Topic archived. No new replies allowed.