Prime # Program, strange error

I need to write a program where the user enters a number and the program finds all the prime numbers from 1 - user input number

However, I am trying to compile my current code, but I keep on getting an error stating:

error C2059: syntax error: 'return'

Why am I getting this error? return 0; is in the correct position right before the program executes, I do not understand this at all

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

using namespace std;

int main()
{
	string name;

	

	cout << "Please enter your username: " << endl;
	cin >> name;

	do
	{
		char choice;
		cout << "Welcome to the Prime Number Program" << endl;
		cout << "-----------------------------------" << endl;
		cout << "Please enter the following options: " << endl;
		cout << "A) Would you like to print all of the prime numbers from N (desired integer)?" << endl;
		cout <<" B) Quit the program" << endl;
		cin >> choice; 

		bool prime;

		if (choice == 'a' || choice == 'A')
		{
			double number;
			number = 0;
			cout <<"Please enter the desired value to print all of the prime numbers: " << endl;
			cin >> number;

			for (int i = 3; i <= number; i++)
			{
				prime = true; 
				for (int n = 2; n <= i; n++)
				{
					if (i%n==0)
					{
						prime = false;
					}

				}
				if(prime)
				{
				cout << i << " is prime" << endl;
				}
			}
		}
		else 
		{
			cout << "You have entered an invalid option" << endl;
		}
		while (choice != 'a' || choice != 'A')
		{
			cout <<"Exiting program" << endl;
		}

		
	}
	
	
	return 0;

}
You forgot about the while at the end of the do
You have forgotten the 'while' on line 62 (you started it as a do-while loop).

EDIT: Ninja'd by ccsdude...
Last edited on
Thank you, I have compiled the program. However, I have another problem:


When the program executes, and I input a number to find the prime, nothing happens and the menu re loops
Your prime generator is extremely slow. Here is an semi-optimized prime sieve but still much faster than what you are doing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const int largestPrime = 10000;
std::vector<bool> sieve(largestPrime+1, true);
sieve.at(0) = false;
sieve.at(1) = false;

const int sqrt = 101;
for(int i = 2; i < sqrt; ++i) //this can be optimized
//if you create the sieve for odds only and remember 2 is a prime
{
    if(sieve.at(i)
    {
        for(int j = i*i; j <= largestPrime; j += i)
        {
            sieve.at(j) = false;
        }
    }
}

//now if it is true it is prime. 



http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Last edited on
Line 35 change to for (int i = 2; i <= number; i++)

Line 38 change to for (int n = 2; n < i; n++)

The second loop can't be equal to the number of the first
I have finished my code with a improved prime number finder from a max range.

One last problem, I cannot compile as I am receiving two errors:
error C2181: illegal else without matching if
error c1075: end of file found before the left brace
{
at 'd:\visual studio 2010/projects/project3/project3/primenumb.cpp(16)' was matched

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>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string name;
	char choice;
	int number, i, count, n;
	cout << "Please enter your username: " << endl;
	cin >> name;

	do
	{
		
		cout << "Welcome to the Prime Number Program" << endl;
		cout << "-----------------------------------" << endl;
		cout << "Please enter the following options: " << endl;
		cout << "A) Would you like to print all of the prime numbers from N (desired integer)?" << endl;
		cout <<" B) Quit the program" << endl;
		cin >> choice; 

		if (choice == 'a' || choice == 'A')
		{

			
			cout <<"Find all the primes below which number:" << endl;
			cin >> n;
			cout << name << " has chosen to select all of the prime numbers below" << number << endl;

			for (number=1; number <= n; number++)
			{
				count =0;
				for (i=2; i<= number/2; i++)
				{
					if (number%i==0)
					{
					count ++;
					break;
					}

				}
				if(count == 0 && number!= 1)
				{
				cout << number << setw(3);
				}
			
		}
		else 
			cout << "You have entered an invalid option" << endl;
	
	while (choice == 'b'|| choice == 'B');
	{
			cout <<"Exiting program" << endl;
	}

		
	
	
	system("PAUSE");
	return 0;

}
You are missing a brace for your for loop (line 49) should been fairly noticeable.
Ah! i cannot believe I missed such a simple error, thank you!


One last question I promise, thank you guys for pointing out all the logical errors!


The program runs as intended, however, when I press B which the program should exit, the else statement "you have entered an invalid option" executes and re loops the menu, but when I press B or b the program should exit
while (choice == 'b'|| choice == 'B'); this is the condition you use inside your do/while loop so it only executes if choice is b or B maybe you meant != b && != 'B' or == 'A' || == 'a'
I see, so I managed to fix the while loop where I enter B and the program executes.

HOWEVER haha another issue has come up.

When I enter a digit over 100, the prime numbers output correctly up until the program has to deal with high numbers (over 100) and the program starts executing random numbers

Also, the do while loop does not execute correctly, after inputting a digit to find the prime number, the program will execute the else statement and terminate.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string name;
	char choice;
	int number, i, count, n;
	cout << "Please enter your username: " << endl;
	cin >> name;

	do
	{
		
		cout << "Welcome to the Prime Number Program" << endl;
		cout << "-----------------------------------" << endl;
		cout << "Please enter the following options: " << endl;
		cout << "A) Would you like to print all of the prime numbers from N (desired integer)?" << endl;
		cout <<" B) Quit the program" << endl;
		cin >> choice; 

		if (choice == 'a' || choice == 'A')
		{

			
			cout <<"Find all the primes below which number:" << endl;
			cin >> n;
			cout << name << " has chosen to select all of the prime numbers below " << n << endl;

			for (number=1; number <= n; number++)
			{
				count =0;
				for (i=2; i<= number/2; i++)
				{
					if (number%i==0)
					{
					count ++;
					break;
					}

				}
				if(count == 0 && number!= 1)
				{
				cout << number << setw(3);
				}
			}
		}
	
		else 
		{
			
		}cout << "You have entered an invalid option" << endl;
	}while ((choice !='A')&&(choice !='B')&&(choice !='a')&&(choice !='b'));
	{
			cout <<"Exiting program" << endl;
	}

		
	
	
	system("PAUSE");
	return 0;

}
while ((choice !='A')&&(choice !='B')&&(choice !='a')&&(choice !='b')); again this isn't right. You either want to loop 1) when it is 'a' or 'A', 2)when it is not 'b' && not 'B', 3) when it is case 1 or case 2. (I would suggest case 1 and output an error message for case 3). As far as your prime finder again I would suggest a sieve you can find all the primes less than 5 billion in the matter of a few minutes(given enough ram and decent computer). As for your error maybe setw(3) has something to do with it. I would honestly just put a space (' ') or a newline ('\n') instead of using std::setw unless you want it to look like a table then pick a largest width.
I see.


What about the else statement? It is not executing properly


When I press B to quit the program, the else statement executes along with the proper statement, why?

I want it to execute properly where it only shows the statement "you have entered an invalid option" when the user enters anything other than A or B or a or b
Look at it closely. I didn't notice earlier but the output statement is outisde of the braces. Anyways it shouldn't be an else for that. It should be an else-if something like:
1
2
3
4
else if(choice != 'b' && choice != 'B') //the first if will have already checked if it is a or A so you don't need to check again
{
    //error message
}
Last edited on
Do you mean outside the if brackets?

Isn't that where else is supposed to be placed?

if
{
statement
}
else
statement

??
I mean your output is outside of the else.
1
2
3
4
		else 
		{
			
		}cout << "You have entered an invalid option" << endl;
Ah I see.


However, now when I press B to simply quit, the invalid statement STILL executes

1
2
3
4
5
6
7
8
9
		else if(choice != 'b' || choice != 'B')
		{
		
		cout << "You have entered an invalid option" << endl;
		}

	}while (choice == 'A' || choice == 'a');
	
			cout <<"Exiting program" << endl;
Sorry it should be && not or pretty tired it's 2 am here.
Thank you so much gibilt! you have been an immense help, and thank you to everyone else for helping! finally got the program running smoothly
Topic archived. No new replies allowed.