I am teaching myself C++, so this is not homework.
I am reading to C++ books, and research my code on the web.
I do not know any C++ programmers, so I would appreciate your help.
I would like someone to review this code and tell me my error.
The output of this code is...
2 3 5 7
However, it should have listed the first 100 prime numbers.
I think the problem is with the outer for loop (w/ var k),
but I am not sure.
On line 22, I am using *(primes + i) to loop through the primes found so far in order to test whether trial is prime or not.
The outer loop (w/ var k) loads the next trial number to be tested.
Your usage of i instead of k is another problem, because most of the loops will be accessing zeroes (you didn't initialize the rest of the array, so by default it is all zeroed out). Look carefully at the way your loops are set up and what they intend to do.
I'm not sure how you can check one number against many prime numbers to determine if it is prime...? Could you explain how you expect this to work, as where I thought I understood before, I am now lost.
LB: There are several ways to find a prime number. If a number can be divided evenly by any of the prime numbers smaller than it then the trial number is not prime. The even numbers do not need to be tested, because they can all be divided by 2.
I'm a little afraid this may be homework, so I'm not going to tell you directly. Also, if it is your own code you will be more pleased if you figure it out.
I suggest you concentrate on this thought - if the numbers are not being added to the array then that means lines 30-34 aren't executing.
kev82: Thanks for the tip. I added "isprime = 0;" to line 19. This fixed the problem. I have a hard time finding that kind of error. I appreciate the help, and to re-assure you I am not in a class, I am however, working through problems in the book. I do not know any other way to learn except to practice. I do get stuck sometimes, and books can't answer questions, or review code and tell you where the mistake is. Thanks again for your help.