This program asks the user for a number, then stores all numbers up to and including it into a set container. The problem is that I to erase specific numbers, but I can't because the program freezes whenever I try to erase an odd number (signified by NUMBER below). It works fine when I erase even numbers though. However, I noticed that if I change the initial value of y to an even number, I become unable to erase even numbers. Here I have it set to an odd number. What did I do wrong?
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int>s;
set<int>::iterator cnt;
int n,x,y=1;
cout<<"Number: ";
cin>>n;
for(x=0;x<n-1;x++)
{
s.insert(y);
y++;
}
for(cnt=s.begin();cnt!=s.end();cnt++)
{
if(*cnt==NUMBER)
s.erase(cnt);
}
for(cnt=s.begin();cnt!=s.end();cnt++)
cout<<*cnt<<"\n";
return 0;
}