I made a program where the user inputs a number and all prime numbers up to and including it are displayed. My algorithm involves starting at 2 and deleting all multiples of it except itself, then 3 and so on. This code erases all multiples of 2 just fine but for some reason will not pass numbers any higher. It's pretty much ignoring the z++ in my for loop. What am I doing wrong?
You are not entering the while loop all of the time.
1. z = 2
2. find z+1 = 3
3. you find if and remove all of the multiples of 2
4. Now, z = 3
5. find z+1 = 4
6. you don't find 4 because it is a multiple of 2 and it was removed.
The same thing happens for 5, 7, 11 because +1 is a even number that you have removed on the first iteration of the for loop.
You need to change the logic on how you iterate thru the set.
If you change line 20 to cnt=s.find(z); and add an extra check on line 23 it will work.