// Erfesoglou3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int startValue,constStart,endValue,primeCounter=0;
bool prime;
cout<<"Enter a starting value (2 or higher): ";
cin>>startValue;
constStart=startValue;
cout<<"Enter an ending value: ";
cin>>endValue;
cout<<endl;
for(startValue; startValue <= endValue; startValue++){
prime = true;
cout<<endl<<"Factors of "<<startValue<<" : ";
cout<<1<<", ";
for(int n = 2; n <= startValue-1 ; n++){
if(startValue % n == 0 ){
cout<<n++<<", ";
prime = false;
}
}
cout<<startValue<<endl;
if(prime){
cout <<endl<<" "<<startValue << " is a prime!" << endl;
primeCounter++;
}
}
if(primeCounter == 1)
cout<<endl<<endl<<"There is only 1 prime number in the range of ["<<constStart<<", " <<endValue<<"] ."<<endl;
else
cout<<endl<<endl<<"There are "<<primeCounter<<" prime numbers in the range of ["<<constStart<<", " <<endValue<<"] ."<<endl;
return 0;
}
Line 30: cout<<n++<<", ";
That is the problem. If 2 is the factor, n willbe incremented here and then incremented again at the end of for loop so 3 will never be tested. Get rid of increment here.
So when for example n is equal to 2 then after this statement it becomes equal to 3 and again within the for statement it is increased one more and becomes equal to 4.
I guess it slipped by me. While I was trying to get it to work at some point I had resorted to doing that. Once I got it working close I left it there and overlooked it.