help

hi I'm writing an program that can look if the number is prime or not.
but it only says its prime why??

The Code:
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
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

using namespace std;
#include <string>
string a = "Enter A Number Between 1 to 1000: ";
void basic_check (int );
void ad_check (int);
int main()
{
    int x;

    cout << a;
    a ="Enter Another A Number Between 1 to 1000: ";
    cin>>x;
    cout << endl;
    basic_check(x);



    return 0;
}
void basic_check (int num){
switch (num){

case 1:
    {
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        main();
    }
    case 2:
    {
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        main();
    }
    case 3:
    {
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        main();
    }
    default:
    {
    ad_check (num);
    cout << endl << endl;
    main();}}}

void ad_check(int num){
    int k = num-3;
    bool checking = false;
int *p;
p= new int[1000];
int arr=0;
for (int i=2 ;i<num;i++)
{
    int re = num%i;
    p[arr]=re;
    arr++;
};
        for (int i=0 ;i<k;i++){

            if (p[i]=0){
                cout << "The Number U Entered Is Not A Prime Number";
                cout <<endl << endl;
                checking=true;
                i=k;
                main();}
        }
        cout<<"The Number U Entered Is A Prime Number";
};


Last edited on
closed account (E0p9LyTq)
NEVER call the main() function in your program, use return.

You are missing a number of closing brackets.

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
64
65
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void basic_check (int);
void ad_check (int);

int main()
{
   std::cout << "Enter A Number Between 1 to 1000: ";
   int x;
   std::cin>>x;
   std::cout << std::endl;
   basic_check(x);
}

void basic_check (int num)
{
   switch (num)
   {
   case 1:
      std::cout << "The number u entered is a prime number.\n";
      return;

   case 2:
      std::cout << "The number u entered is a prime number.\n" ;
      return;

   case 3:
      std::cout << "The number u entered is a prime number.\n" ;
      return;

   default:
      ad_check(num);
   }
}

void ad_check(int num)
{
   int k = num - 3;
   int* p;
   p= new int[1000];
   int arr = 0;

   for (int i = 2 ; i < num; i++)
   {
      int re = num % i;
      p[arr] = re;
      arr++;
   }

   for (int i = 0 ; i < k; i++)
   {
      if (p[i] == 0)
      {
         std::cout << "The Number U Entered Is Not A Prime Number\n";
         std::cout <<std::endl << std::endl;
         i = k;
         return;
      }
   }
   std::cout<<"The Number U Entered Is A Prime Number\n";
}
guys I edited it like u said and made upgrades but now it crashes when I enter number above 3 why :(

The New Code
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
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>

using namespace std;
#include <string>
string a = "Enter A Number Between 1 to 1000: ";
void basic_check (int );
void ad_check (int);
int main()
{
    int x;

    cout << a;
    a ="Enter Another A Number Between 1 to 1000: ";
    cin>>x;
    cout << endl;
    basic_check(x);



    return 0;
}
void basic_check (int num){
switch (num){

case 1:
    {
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        return;
    }
    case 2:
    {
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        return;
    }
    case 3:
    {
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        return;
    }
    default:
    {
    ad_check (num);
    cout << endl << endl;
    return;}}}

void ad_check(int num){
    int k = num-2;
    bool checking = false;
int *p;
p= new int[1000];
int arr=0;

for (int i=2 ;i<num;i++)
{
    int re = num%i;
    p[arr]=re;
    arr++;
};
      int i = 0;
        while (!checking || i!=k)

{
if (p[i]=0){
                cout << "The Number U Entered Is Not A Prime Number";
                cout <<endl << endl;
                checking=true;
                i=k;
                return;}
                i++;}
                  cout<<"The Number U Entered Is A Prime Number";
        }
Last edited on
While, what gentleguy has pointed out is a problem, the loop condition on line 63 can never be false, given the logic in the function (even after the other issue is sorted,) so p[i] will eventually be accessing unintialized values and memory that isn't valid to access, both of which result in undefined behavior.
Hi ehsan687. There are some notes that I hope to be useful.

1) Never call main() function. Choose another way to do what you want.
2) It is not required to use ; after {} of loops or function implementations.
3) Be aware of using = and ==. The first is assignment operator and the second is for checking equally.
4) Always think before you begin to write codes. This will reduce your mistakes and will improve your code.
5) In programming world, when you have to do a most repetitive job, often there is an easier way. For example look at your code(lines 25-42). The three case statements are doing same work and just the conditions are different. Think about it 20 seconds...
We can replace those codes with just one of them and use OR operator in the condition.
for this purpose, we should write case statements without break; and { }
I mean:
1
2
3
4
5
6
    case 1:
    case 2:
    case 3:
        cout << "The number u entered is a prime number." ;
        cout <<endl<<endl;
        return;

for information about Switch-case go to this link:
http://www.cplusplus.com/doc/tutorial/control/
6) Always before start coding, try to get more and more information about your problem. A programmer should study/get information/ask somebody/etc before he/she begins to coding.(See comments in codes below. Those are information I have get from math)
For example in your problem(specifying that input number is prime or not) there are some algorithms that make coding easier. (look at this code for example)
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
#include <iostream>

using namespace std;


bool basic_check (int);  /** This function takes a number and checks whether it's prime or not.
                            if the number is prime, function will return true.
                            if the number is not prime, function will return false.
                        **/


int main()
{
    int input;
    bool result;

    while(1){                 /// An infinite loop for repeat program. It will stop when user enters 0
        cout<<"Enter a number: ";
        cin>>input;
        if(input==0)
            break;
        else if(input<0)
            cout<<"Invalid input."<<endl<<endl;
        else{
            result=basic_check(input);
            if(result)
                cout<<"The number is prime."<<endl<<endl;
            else
                cout<<"The number is not prime."<<endl<<endl;
        }
    }
    return 0;
}

bool basic_check(int input){
    /** Mathematical note:
                            1) A number is prime if divided only by 2 numbers.
                            2) The dividers of a number are not greater than half of it.
                                e.g. the dividers of 200 are not greater than 100.
    **/
    int i,dividers=0;   ///dividers is a variable that we store sum of dividers in it.

    for(i=1;i<=input/2;i++)
        if(input%i == 0)
            dividers++;
    if(dividers==2)
        return true;
    else    ///we can erase this line. It is just for more readability
        return false;
}


Or use the code below for checking primness:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool basic_check(int input){
    /** Mathematical note:
                            1) A number is not prime if there is at least one divider except 1 and itself.
                            2) The dividers of a number are not greater than half of it.
                                e.g. the dividers of 200 are not greater than 100.
    **/
    int i;

    for(i=2; i<=input/2; i++)
        if(input%i==0)
            return false;

    return true;
}


After all these advises(or maybe boring advises!) look at this short code:
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
#include <iostream>

using namespace std;

bool isprime(int);
int main(){
    int input;
    while(1){
        cout<<"Enter a number: ";
        cin>>input;
        if(input==0)
            break;
        else if(input<0)
            cout<<"Invalid input."<<endl<<endl;
        else if(isprime(input))
            cout<<"The number is prime."<<endl<<endl;
        else
            cout<<"The number is not prime."<<endl<<endl;
    }
    return 0;
}

bool isprime(int input){
    for(int i=2; i<=input/2; i++)
        if(input%i==0)
            return false;
    return true;
}


Note: the manner you choose for your program depends on the problem you have faced with.
Last edited on
Oh, I forgot to answer your question about your code.
you should replace these codes:

in line 63:
 
while (!checking && i<=k)


in line 66: (As gentleguy said)
 
p[i]==0


Note:
Because you write return; in line 71, there is no need to write lines 69 & 70.
When program meets the return; command, it will exit from the current function.
Last edited on
You can save yourself a whole lot of trouble you know! Just divide the number by repeatedly larger numbers (say increase variable n) starting from 0 and stopping when it reaches original number. If the number divided by n is a whole number (modulo) then increase say variable count by 1 (count++). Then if count is 2 it's prime if it's greater than 2 then it's composite.

Like this:

1
2
3
4
5
6
7
for (int n {1}; n <= original_number; n++)
{
    if (original_number % n == 0)
    {
        count++;
    }
}
Last edited on
@gentleguy
Thanks.
(But the forum is not a place to say greetings)
Last edited on
@gentleguy
Sorry for that.
I didn't see your post and I was busy last two days.

Are you here all the time?!
Last edited on
What is "OP" ?
Topic archived. No new replies allowed.