The immediate problem is you didn't reset to zero the value of the variable
i
. Changing from
{
cout<<"\nEnter a number:";
to
{
i = 0;
cout<<"\nEnter a number:";
shoud fix it.
However, there are troubles at the horizon: your code is simply a mess. Moreover it looks you are using an outdated
C++
compiler.
You could write your program this way:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
while ( true)
{
vector <int> binary;
int n;
cout << "please insert a positive number (a negative one to exit)" << endl;
cin >> n;
if ( n < 0) return 0;
do
{
binary.push_back( n % 2);
n >>= 1;
} while (n>0);
for (vector<int>::reverse_iterator rit = binary.rbegin(); rit != binary.rend(); ++rit)
{
cout << *rit;
}
cout << endl;
}
}