I think you should break the entire problem up into easy to chew

chunks. Don't be afraid to write lots of functions. It makes writing

the program trivial. All of this retval, intval, counter stuff is

confusing, even for me. Here is how I would approach the problem:

Write a program that prompts the user to input a positive integer.

This tells me I need a function such as:

int PromptUser() {

cin >> n;

if (n <= 0) {

exit(1);

}

return n;

}

It should then output a message indicating whether the number is a prime
number.

This indicates I need the following function:

void OutputMessage(bool bIsPrime) {

if (bIsPrime) {

cout << "the number is prime" << endl;

}

else {

cout << "the number is not prime" << endl;

}

}

(Note: An even number is prime if it is 2.

bool IsNumberEven(int n) {

return n % 2 == 0;

}

bool IsEvenNumberPrime(int n) {

assert(IsNumberEven(n)); // make sure it really is an even number

if (n == 2) {

return true;

}

else {

return false;

}

}

An odd integer is

prime if it is not divisible by an odd integer less than or equal to

the square root of the number.)

bool IsNumberOdd(int n) {

return !IsNumberEven(n);

}

bool IsDivisibleBy(int Dividend, int Divisor) {

return Dividend % Divisor == 0;

}

bool IsOddNumberPrime(int n) {

assert(IsNumberOdd(n)); // make sure it really is an odd number

double dSquareRoot = sqrt(n);

// check divisibility against all odd numbers up to the square root.

for (int i=1; i <= dSquareRoot; ++i) {

if (IsNumberOdd(i)) {

if (IsDivisibleBy(n, i)) {

return false;

}

}

}

return true;

}

I'll leave it to you to construct the rest of program.

I hope this helps.

Christopher Diggins

http://www.cdiggins.com http://www.cpp-cookbook.com