Simple Program w/ Menu

So I was assigned a three-part program a few days ago and I am having problems with my output. The assignment was to do the following:
1. Create a neat menu that will offer three separate options.
2. Option 1: check whether or not an integer is prime or not. This is checking from 1 to the sqrt(n), not including one but including the sqrt(n).
3. Option 2: display all factors for any given number n in some sort of neat table.
4. Exit the program when option 3 is selected.
We must also use function calls for the menu and options 1 and 2. Must use a switch statement, and can't use any libraries like <math.h> to get our results.

So far I have managed to get all of these right and get everything down and even the approximation for primes without the sqrt function, but when I enter 1 in cmd and try to check if a number is prime, I get nothing and re-looped back into the program. Can anyone help me find what's wrong?

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>

using namespace std;
int prime_output(int);
void factors_output(int);
void menu();

int main()
{
	int selection;
	int primes, factors;

	do
	{
		menu();
		cin >> selection;
		while (selection < 1 || selection > 3)
		{
			cout << "Please enter a valid menu choice.\n";
			cin >> selection;
		}
		if (selection != 3)
		{
			switch (selection)
			{
			case 1:
				prime_output(primes=0);
				break;

			case 2:
				factors_output(factors=0);
				break;

			case 3:
				cout << "Thank you for using this program. Goodbye.\n";
				break;

			default:
				cout << "That is not a valid selection.\n";
				cout << "Try again.\n";
			}
		} 
	} while (selection != 3);
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void factors_output(int factors)
{

	cout << "Please enter a positive integer greater than zero to retrieve its factors.\n";
	cin >> factors;

	while (factors <= 1)
	{
		cout << "Error. Please enter a positive integer greater than zero.";
		cin >> factors;
	}
	cout << "These are the factors for " << factors << ":" << endl;
	for (int k = 1; k <= factors; k++)

		if (factors % k == 0)
		{
		cout << "\t" << k;
		cout << endl;
		}

}

/////////////////////////////////////////////////////////////////////////////////////////////////
int prime_output(int primes)
{

	int keep_counting = 0;
	cout << "Please enter a positive integer greater than 1 to begin.\n";
	cin >> primes;


	while (primes <= 1)
	{
		cout << "Error. Please input a positive integer greater than 1.\n";
		cin >> primes;
	}

	int a = primes / 2;
	int b = (1 / 2)*((primes / a) + a);

	for (int k = 1; k <= b; k++)
	{
		if (b%k == 0)
		{
			keep_counting++;
		}
		if (keep_counting == 2)
		{
			cout << primes << " is a prime number!\n";

		}
		else
			cout << "This is not a prime number.\n";
	} return 0;
		
}
/////////////////////////////////////////////////////////////////////////////////////////
void menu()
{

	cout << "Thank you using this Primality and Factor program!\n";
	cout << "Remember to use a positive and non-zero integer!\n";
	cout << "\t Enter 1 to check if your number is prime or not.\n";
	cout << "\t Enter 2 to display all factors for your number.\n";
	cout << "\t Enter 3 to exit the application.\n";
	
}
You may want to review the following two lines.
1
2
	int a = primes / 2;
	int b = (1 / 2)*((primes / a) + a);


Look closely at that second line, remember 1 / 2 will yield zero, no fractions in integer math.

Thank you so very much! Now I have one more problem. When running the code, I get both output that the number I entered is both prime and non prime. At the same time, I'm getting multiple lines. For example, for 11 I get:

"This is not a prime
This is not a prime
11 is a prime number!"

Any way to fix that? Btw, updated 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
int prime_output(int primes)
{

	int keep_counting = 0;
	cout << "Please enter a positive integer greater than 1 to begin.\n";
	cin >> primes;


	while (primes <= 1)
	{
		cout << "Error. Please input a positive integer greater than 1.\n";
		cin >> primes;
	}

	int a = primes / 2;
	int b = ((primes / a) + a) / 2;

	for (int k = 1; k <= b; k++)
	{
		if (b%k == 0)
		{
			keep_counting++;
		}
		if (keep_counting == 2)
		{
			cout << primes << " is a prime number!\n";

		}
		else
			cout << "This is not a prime number.\n";
	} return 0;
		
}
Look closely at your if statements. The only time that else won't execute is when keep_counting is equal to 2, is that really what you want. Also you may want to consider starting your loop with something other than 1, remember 1 and 2 are "known" prime numbers.

Just FYI, 1 is not considered prime.
True but it is "special" and shouldn't be considered, since a prime number is always greater than 1.

I was speaking in relation to your statement that 1 was a "known" prime number, which was incorrect. Considering what you are saying now, though, perhaps I misunderstood you.
I'm still having problems understanding where I'm going wrong. I understand the concept of checking from 1 until sqrt(n), but isn't "keep_counting" keeping count of my factors that have no remainder? If I am misunderstanding a concept, please guide/help me out some.
The problem is that your if statements are incorrect. Look at that last if statement, what happens if keep_counting is greater than 2?
Then it would loop to the else statement and return that the number entered is not a prime number?
I'm not trying to be stubborn or wise, but I'm not catching on here..
But isn't one of the problems you reported that it keeps printing "This is not a prime number."?

That's true. When I run the program, I'll get a list (presumably the amount of the entered number) of "This not prime" and then finally the real outcome. So if I enter 11, which is prime, I'll get 11 times telling me it's not prime, with the final one telling me it is.
So adjusting my if, else loop will fix that?
It should help solve at least one problem, if done properly. By the way that is an if/else statement, not a loop.

After looking at some other examples and reading more up on it, I get this far. Still, I'm getting some wrong answers. Any advice?

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
int prime_output(int primes)
{

	int keep_counting = 0;
	cout << "Please enter a positive integer greater than 1 to begin.\n";
	cin >> primes;


	while (primes <= 1)
	{
		cout << "Error. Please input a positive integer greater than 1.\n";
		cin >> primes;
	}
	if (primes == 2)
	{
		cout << primes << " is a prime number!\n";
	}
	int a = primes / 2;
	int b = ((primes / a) + a) / 2;

	for (int k = 1; k <= b; k++)
	{
		if (primes%k == 0)
		{
			cout << "This is not a prime number!\n";
		}
		else
		{
			cout << "This is a prime number\n";
		}
	} return 0;

}
Do you realize that you're going to print either is a prime or is not a prime every time through the loop. Perhaps you should also print the number that is generating these messages.

You may also want to view some information about finding prime numbers: http://mathforum.org/isaac/problems/prime2.html
Would it be easier to change this to a bool type, or is there a way to adjust the if statement just to print once that n is or is not prime?
Have you studied functions yet?

You really need to study the link I provided, finding if a number is prime involves more than a simple loop and simple if statement.

Are you referring to using square root to find my answer?
No I am referring to this snippet from the link I provided above.

Suppose we want to find all the prime numbers between 1 and 64. We write out a table of these numbers, and proceed as follows. 2 is the first integer greater than 1, so it is obviously prime. We now cross out all multiples of two.


And then this:

The next number that we haven't crossed out is 3. We circle it and cross out all its multiples.


And then go to the next number left and then the next number left until you reach the square root of your number.

Remember the square root is only used to compute the upper limit of the numbers you will need to check.

Topic archived. No new replies allowed.