I am writing a program that will take input and find whether or not the number you put in is prime. The program works fine, save for one small problem:
#include <iostream>
using namespace std;
int main(){
int num; //the chosen number
int i; //potential factors
do{
cout<<"Input a positive integer: \n";
cin>>num;
cin.ignore();
//these if statements take care of all "abnormal" situations:
if (num == 1){
cout<<"1 is composite, not prime. Try again.\n";
}
if (num < 1){
cout <<"Not valid input. Try again.\n";
}
if (num == 2){
cout <<"2 is prime.\n";
cin.get();
}
} while (num < 2);
//factor iteration:
for (i = 2; i < num; i++){
if (num % i == 0){
cout<< num <<" is not prime. It is divisible by " << i << "\n";
cin.get();
}
if (num % i != 0){
cout<< num <<" is prime!\n";
cin.get();
}
else {
cout<<"An error occured.\n";
cin.get();
}
}
}
The problem is in the if statements embedded in the for loop that iterates through the possible factors of "int num". As you may have guessed, I have to press enter as many times as the input minus one. So, for every time the "i" variable increments. I tried to use a break statement for each one, but this declares every odd number as prime, like 15 or 39. Any help as to how to fix this problem would be greatly appreciated.
On a side note, just for future reference on DaniWeb, do I have to include all the code in the code tags, or just the part that has the problem? If it's a really long program, then of course it would be stupid to include all of it...Also, is there a way to enable syntax highlighting? Anyway, thanks.