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.
/*****************************************
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>
usingnamespace 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;
}