What are these error codes on my prime test??

Hello all. I am currently learning C++ in college, and I need help with an assignment. I've been stuck on it for about 7 hours now :(


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
primetest.cpp: In function 'int main()':
primetest.cpp:35:6: error: 'n' was not declared in this scope
primetest.cpp:41:18: error: expected ')' before ';' token
primetest.cpp:41:20: error: 'i' was not declared in this scope
primetest.cpp:41:37: error: expected ';' before ')' token
primetest.cpp:57:1: error: expected '}' at end of input
primetest.cpp:57:1: error: expected '}' at end of input
void ifPrime(int n)

int main()
{
    bool primen = true;

    if( n <= 1 || n == 2)
    {
        cout << "0" << endl;
    }
    else
    {
        while(int i = 2; i <= sqrt(n); i++)
        {
            if(n % i == 0)
            {
                primen = false;
            }
        }
        if(primen == true)
        {
            cout << "1" << endl;
        }
        else
        {
            cout << "0" << endl;
        }
    }
}

The reason it's printing out 0's and 1's is because he said he wants it to print 0 if its not a prime, and 1 if its prime

This was my original program (I think, as I've just retyped it, it won't work :/)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
primetestoriginal.cpp: In function 'int main()':
primetestoriginal.cpp:37:3: error: 'else' without a previous 'if'
primetestoriginal.cpp:40:3: error: 'else' without a previous 'if'
[csc103@103Arch Desktop]$ 

int main()
{
	unsigned long n;
	while(cin >> n) {
		if(n <= 1);
			cout << "0" << endl;
	
		else if (n == 2);{
		cout << "1" << endl;
		}
		else if (n % 2 == 0);{
			cout << "0" << endl;
		}
}


Last edited on
Okay so first off, maths...

Lines 14-16, you state that if n (the number) equals two, then it is not a prime. Two (2) though is a prime number. A prime number is divisible only by two numbers, itself and one. Two fits this mold and is therefore a prime number.

So far we have dealt with many numbers you could run across. All negative numbers, one and 2 have been dealt with in your code. This is good. You're on the right track.

Now what is another thing we know about prime numbers? Well above two prime numbers cannot be even. Right? In proofs, a generic (i.e. non-specified/could be any) even number is represented as "2*k" where k is some value. So we know right off the bat, any even number larger than 2 can be, and is, expressed as a multiple of 2.

This means we can get rid of ALL even numbers larger than two because we already know that they cannot be prime.

How would we do this? (Hint hint... use '%').

Okay so negatives, zero, 1, 2, and all even numbers are dealt with, no need to worry about them, your code will handle them now.

Part (I don't know, let's say 3) PART THREE!
All we need to do after we have done what I have previously said is add in a small while loop and and if statement.

You will have a while loop, which checks whether your divisor is greater than your number.
Inside of this you will use the modulus (%) operator again to check for whether the number modulus of your divisor equals 0, if it does. That means that your number is NOT a prime.

1
2
3
4
5
6
7
8
9
10
11
bool prime = true;    //Assume it is true and have the code dictate if it is or is not true
int divisor = 3;  //This is because negatives, 0, 1, and 2 are accounted for.
double num_d = static_cast<double>(num);
int upperLimit = static_cast<int>(sqrt(num_d) +1);
        
while (divisor <= upperLimit)
{
    if (num % divisor == 0)  //If this is 0, that number divided with no remainder.
        prime = false;
    divisor +=2;    //+2 because we excluded all even numbers.
}


Code credit goes to Grey Wolf for actual code:
http://www.cplusplus.com/forum/general/1125/
Hay thanks for the reply!!!
So I've tried to comprehend what you told me, I've made this now
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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::endl;
#include <cmath>


/* a prime number is a number greater then 1 that has no other integer divisors
 * other then 1 and itself.
 * a composite number is a number that can be divided by numbers other then 1
 * and itself 
 * If the program finds a number to be a prime number, it will return the 
 * value 1
 * If the program finds a number to be a composite number, it will return
 * the value 0 */


int main()
{
	bool primen = true;
	int n;
	int divisor = 3;

	if( n <= 1)
	{
		cout << "0" << endl;
	}
	else if (n == 2)
	{
		cout << "1" << endl;
	}
	else if (n % 2 == 0)
	{
		cout << "0" << endl;
	}
	else
	{
		while(int i = 3 && i <= sqrt(n); n++)
		{
			if (n % i == 0)
			{
				cout << "0" << endl;
			}
			else
			{
				cout << "1" << endl;
			}
		}
      }
}

Now I'm getting this
[csc103@103Arch p2]$ g++ primetest.cpp
primetest.cpp: In function 'int main()':
primetest.cpp:51:34: error: expected ')' before ';' token
primetest.cpp:51:39: error: expected ';' before ')' token
primetest.cpp:62:2: error: expected '}' at end of input
primetest.cpp:62:2: error: expected '}' at end of input
I know what the error code is saying but I'm sure I've done what it says I haven't. I have ; before ), I have } at the end, what's wrong??
Last edited on
I made this function a while ago to my mathematics framework, I believe it to be correct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool IsPrime(unsigned int n)
{
	// if number is even, definitely not prime
	if(n % 2 == 0) return 0;
	// 0 or 1 is not prime
	if(n == 0 || n == 1) return 0;
	// 2 and 3 are
	if(n == 2 || n == 3) return 1;
	// if number is greater than 3 and not even
	// test numbers against it
	for(unsigned int i = 3; i < n/2+1; i+=2)
	{
		if(n%i == 0) return 0;
	}

	// if execution gets here, it is a prime number
	return 1;
}
Last edited on
This is what I had, I squashed out the compiling errors
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
    cout << "Please enter a numerical value";
    while(cin >> n)
    {
    if( n <= 1)
    {
        cout << "0" << endl;
    }
    else if (n == 2)
    {
        cout << "1" << endl;
    }
    else if (n % 2 == 0)
    {
        cout << "0" << endl;
    }
    else
    {
        for(int i = 3 && i <= sqrt(n); i++;)
        {
            if (n % i == 0)
            {
                cout << "0" << endl;
            }
            else
            {
                cout << "1" << endl;
            }    
    }
}
}
}

Changed the while to for, and added a while to the top.
The following inputs return correct values, anything less then 1, and 2
Any other number, the console gets spammed with 1111111111111111111111111111111111111111111. I've been told I need breaks somewhere. Unfortunately I have to get off the PC right now, but just giving an update what I've done
megatron thanks! I'll take a better look tomorrow.
Unfortunately I've submitted the assignment 40 mins late trying to squash things out, so I won't have a program thats correct, submitted, but atleast some works
Take a look at this.

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
76
77
#include <iostream>
#include <cmath>


using namespace std; //Although it is frowned upon sometimes this is okay.

/*
using std::cin;    //Do not use these!
using std::cout;
using std::endl;
using std::endl;
*/

/* a prime number is a number greater then 1 that has no other integer divisors
* other then 1 and itself.
* a composite number is a number that can be divided by numbers other then 1
* and itself
* If the program finds a number to be a prime number, it will return the
* value 1
* If the program finds a number to be a composite number, it will return
* the value 0 */



int ifPrime(int n)
{
	int count = 3;
	
	//0 = false, 1 = true.
	if (n <= 1)
	{
		return 0;
	}
	else if (n == 2)
	{
		return 1;
	}
	else if (n % 2 == 0)
	{
		return 0;
	}
	else
	{
		while( count <= (sqrt(n) + 1))
		{
			if (n % count == 0)	//Divided with no remainder means even division, i.e. not prime.
			{
				return 0;
			}
			count += 2; //Plus two to skip over even number;
		}
	return 1;
	}
}

int main(void)
{
	int input;
	cout << "Enter number: ";
	cin >> input;
	
	int val = ifPrime(input);
	
	if (val == 0)
	{
		cout << "Your number is not prime.\n";
	}
	else if (val == 1)
	{
		cout << "Your number is prime.\n";
	}
	else
	{
		return 1;
	}
	return 0;
}
Wow thanks for the explanations and everything! =D. Why shouldn't I use the std:cin, cout, etc, why is namespace frowned upon? Also in int main(void), isn't void for a fuction that isn't returning something?
Last edited on
Topic archived. No new replies allowed.