abrahimladha
abrahimladha

Reputation: 117

Why does this C++ program work in some compilers but not others? What is the major difference between c++ compilers?

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?

Upvotes: -2

Views: 1517

Answers (2)

6502
6502

Reputation: 114461

Your code is not correct (calls atoi passing a std::string instance when the function expects a const char * instead) and it should not compile cleanly. Learn to always enable all warnings and learn to understand their meaning.

However what is stated in the question can indeed happen with C++ and the reason is "undefined behavior".

When you do something wrong at runtime in a C++ program in most cases what happens is totally unpredictable; it can crashes (if you are very lucky), it can provide you nonsense results (if you're somewhat lucky) or it can just work fine anyway despite your error (the most dangerous but common case). Of course the behavior can change depending on compiler, OS, phase of the moon.

Murphy's law applied to C++ tells that everything will work anyway as you are testing your program, but it will fail miserably when you are on a stage showing the program on the large screen in front of a huge crowd that includes your boss and parents :-)

Upvotes: 1

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"What makes all these compilers so different? Is there anything I can do to make sure my code will work in any compiler?"

This program code will not actually work with any c++ compiler. If you had one that compiled the program without throwing any error or warning, it has a serious bug (the other suspicion could be, you're not showing your original code here).

When I'm compiling your program on Ideone I get the following error message

prog.cpp:34:21: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'

This indicates you should use

 return atoi(hold.c_str());

because std::string isn't automatically converted to a const char*. The mentioned std::string::c_str() function is there to do so.

Also you've been missing to #include <string>, and instead of using namespace std; you should better explicitely write std::string.

Here's a compile clean version of your code.

Upvotes: 5

Related Questions