私は自分のクラスのためにこのプログラムを書きました。GNU g++ コンパイラで問題なくコンパイルおよび実行されることがわかりました。私の教授は、Microsoft Visual Studio コンパイラを使用する彼の Web サイトからプログラムを自動採点し、エラーをスローします。このプログラムを BSD の clang コンパイラでも試してみましたが、まったく別のエラーが発生しました。
#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;
}
これらすべてのコンパイラの違いは何ですか? コードがどのコンパイラでも動作することを確認するためにできることはありますか?