I have written this program for my class. I have found it compiles and runs just fine with the GNU g++ compiler. My professor auto-grades our programs from his website, which uses the Microsoft Visual Studio compiler and it throws an error. I have also tried this program in the BSD clang compiler and I am getting a completely different error.
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
double dec2Bin(int value, char binaryString[])
{
int x = 1;
string hold = "";
while(x <= value){
x *= 2;
}
x /= 2;
while(x >= 1){
//cout << x << " ";
if(value > x){
hold += "1";
value -= x;
}
else if(value < x){
hold += "0";
}
else if(value == x){
hold += "1";
value = 0;
//return hold;
}
x /= 2;
//cout << hold << endl;
}
return atoi(hold);
}
int main()
{
char binstr[100];
int num = 0;
cout << "Enter a decimal string: ";
cin >> num;
cout << "its "<<dec2Bin(num, binstr) << endl;
}
What makes all these compilers so different? Is there anything I can do to make sure my code will work in any compiler?