Another Prime numbers program question.

Well, after trolling around the site for the past few months I've managed to learn more than I ever expected about programming. However, I now find myself stumped. I am writing a prime numbers program that accepts a user's value and tests it. The value is tested through a separate function, which returns a bool value. I can look at other versions of programs that perform similar actions to make it work, but I'm still curoius about what's going on in this one. The program says any number that is odd is prime, while every even number is not prime. From this I believe the loop is only running one iteration. My question is why? I am thinking the value of the modulus is being assigned to the orginal value, but I don't know how to check it. Thanks for 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
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
115
116
/*****************************************
This program asks the user for a
number. The program will then test 
the number to see if it is a prime
number. A bool value is then returned
to the main function.
******************************************/
#include <iostream>
using namespace std;

// This is the primeTEST prototype.
bool primeTEST(int value);

// This is the start of the main function.
int main()
{
	int userNumber; // This variable holds the user's value.
	char choice; // This variable holds the user's choice for "run again".

	// This loop runs the program until the user get tired of entering numbers.
	do 
	{
		// Clear the screen before each iteration.
		system ("cls");
		// The section asks the user for the value to test.
		cout << "Please enter an integer value greater than\n";
		cout << "one, and I will tell you if it is prime or not.\n";
		cout << "\nEnter your value and press [ENTER]: ";
		cin >> userNumber;

		while (userNumber <= 0)
		{
			// This section clears the screen and asks for a valid input.
			system ("cls");
			cout << "\nThe number you entered is invalid.\n";
			cout << "\nPlease enter a number greater than 1: ";
			cin >> userNumber;
		}

		// Call the primeTEST function, and send the users value.
		// If the number is not prime this message will display.
		if (primeTEST(userNumber))
		{
			cout << "\nThe number " << userNumber << endl;
			cout << "is a not prime number!\n";
		}

		// If the number is prime display.
		else
		{
			cout << "\nThe number " << userNumber << endl;
			cout << "is a prime number.\n";
		}

		// Ask the user if the program should run again.
		cout << "\nWould you like to test another value?\n";
		cout << "Please Enter Y or N: ";
		cin >> choice;

			// Validate the choice is Y or N.
			while ((choice != 'Y')&&(choice != 'N'))
			{
				system ("cls");
				cout << "\nThe choices are Y for yes, N for no.\n";
				cout << "\nWould you like to test another value?\n";
				cout << "Please Enter Y or N: ";
				cin >> choice;
			}
	}
	while (choice == 'Y');
	
	// This line clears the screen.
	system ("cls");

	// Tell the user the program has completed.
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout << "\n\tThis program has completed its mission.\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	cout <<"\n*****************************************************\n";
	return 0;
	
}

/*************************************************************
This function recievies the user's value and tests to see if
it is a prime number. A bool value is then returned to main.
**************************************************************/
bool primeTEST(int value)
{
	// This is the bool variable to return.
	bool prime = false;

	// This loop tests to see if the number is prime.
	for (int i = 2; i <= (value - 1); i--)
	{
		// This formula divides the user's value by every number 
		// from 2 to its original value minus one.
		value %= i;

		// If there is no decimal value the number is not prime.
		if (value == 0)
		{
			return prime = true;
		}
	}

	// If the function is not stopped by the loop it is not prime. 
	return prime;
}
During the first iteration of the for loop, value becomes either 0 or 1 on line 105.

I think the rest is pretty clear.
Topic archived. No new replies allowed.