Originally posted by: RedArmy
Originally posted by: dinkumthinkum
You seriously need to learn how to properly indent and format your code if you expect anyone else to read it. Plus it will help you spot errors. Automatically indenting editors are freely available. You have no excuse.
P.S. You probably didn't intend to put "n=(3*n)+1;" in the prime-checking loop. But, who knows ...
I don't need an excuse. Every professor I've ever had in college has told us to do indenting and commenting different ways, so I just do what makes sense to me, because in the end I'm the one that has to deal with it. If this was some group collaboration then yeah, I would try and format it to a certain standard. However, I'm sure as hell not gonna go reading through a book of ISO standards of how to properly indent.
I'm just throwin' this out there, I'm not trying to please anyone. Also, I do need that n=(3*n)+1 in the loop since it needs to be implemented regardless if it's prime or not.
Indentation is important when you are asking for help on the internet. at very least be consistant. crap like
if(blah)
{ Do stuff; }
is very ugly and can be hard to read as the curvy braces look pretty simular to parentheses. at very least that line would look better like this.
if(blah)
Do stuff;
as those braces aren't needed.
as was stated, this is part of your problem
for(int i=2; i<=sqrt(n);i++)
if (n%i==0){
isPrime=false;
cout<<n<<endl;
n=(3*n)+1;}
your changing the value of n when checking to see if n is prime. That doesn't work. since your prime detection is based on incrementing i and dividing n by it, eventually you will hit a point where your new n is divisible by some i that you entered, or n changes enough that it won't be divisible by i. If you have to do this for whatever reason (I honestly don't see why) then do it in a separate loop.
Also, is there any particular reason that you have the iostream header included, you're using cout, but then for input you are using fgetc? Why? you already have that overhead of iostream, you might as well use cin.