I am C++ beginner, the following program is very simple, yet i don’t know why when “EXIT” is entered, the program terminates, though it’s supposed to print out the names entered before !
here’s the code:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main()
{
set <string> myset;
set <string> :: const_iterator it;
it = myset.begin();
string In;
int i=1;
string exit("EXIT");
cout << "Enter EXIT to print names." << endl;
while(1)
{
cout << "Enter name " << i << ": " ;
cin >> In;
if( In == exit)
break;
myset.insert(In);
In.clear();
i++;
}
while( it != myset.end())
{
cout << *it << " " ;
it ++ ;
}
cout << endl;
}
thanks in advance.
Move this line to just before the loop that displays the names. The problem is that with it at the top, where there are no elements in the set, it gets the value of the end iterator, so the display loop ends immediately.