Why won't it show a 3 for factors that have a 3?

Been coding all night so I'm about to sleep but this has been bugging me.

My code shows the factors of numbers but it won't show a 3 when there is a factor of 3 of a number like 6, 12, 24,etc...

I have a feeling its my second for loop but I'm not sure how to change it without breaking the functionality of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Erfesoglou3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace 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.
Why are you changing n inside the loop?

cout<<n++<<", ";

I do nott see any sense in this changing.

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.
Last edited on
OMG guys lol so simple.

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.

Thanks yall.
Topic archived. No new replies allowed.