prime composite or neither.

Hey, trying to create a program that allows the user to enter a number that is then defined by the program as prime, composite, or neither. For some reason it does not defining all composite numbers. Only odd numbers. Any suggestions?

#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])
{
int number, prime, composite;
char answer;
do
{

cout << "Enter an integer: ";
cin >> number;


if (number < 2)
cout << "The number you entered is neither prime or composite.\n ";
else if ((number == 2) || (number == 3))
{
cout << "The number you entered is prime.\n ";
number = prime;
}
else if (number == 4)
{
cout << "The number you entered is composite.\n ";
number = composite;
}

else for (int i = 2; (i <= sqrt(number)) && (number != composite) && (number != prime); i++)
if (number % i == 0)
{
cout << "The number you entered is composite.\n ";
number = composite;
}
else
{
cout << "The number you entered is prime.\n ";
number = prime;
}
cout << "Would you like to play another? Answer y or n: ";
cin >> answer;
}while (answer == 'y');


system("PAUSE");
return EXIT_SUCCESS;
}


thanks, david
prime and composite, you set them but you never use them. Because you never initialize those variables, when you hit your for loop and use them in a comparison you will get undesirable behavior.
Topic archived. No new replies allowed.