I'm new to C++ and was trying to write a program that would ask for a number then return its binary form. However I couldn't get it to compile while trying to use integer powers, and had to use a double power function...however got a warning saying that there could be possible loss of data. After running the program a few times it seems to be fine, but how can I avoid the warning? Here is my code:
Code:
#include "base10tobase2.h"
using namespace std;
int main()
{
int i,x=-1;
double a;
while ((x<0)||(x>255))
{
cout<<"Please enter a number less than 256:\n";
cin>>x;
}
cout<<"The number you entered was "<<x<<"\n";
cout<<"Its binary form is:\n";
for (i=7;i>-1;i--)
{
a=pow(2.0,i);
if ((x-a)>-1)
{
x-=a;
cout<<"1";
}
else
cout<<"0";
}
cout<<"\n\nPress enter to continue";
fflush (stdin);
cin.get();
return 0;
}
Also, as I am new to C++, I would appreciate any tips/advice you have?
Thanks!