-2

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?

  • 4
    Learn to use your compiler of choice and crank up it's warning level to maximum. Correct all warnings. – nvoigt Feb 01 '15 at 07:23
  • 1
    Oh and if you know your professor uses Visual Studio to check your code, do yourself a favor and use Visual Studio, too. At least once before you send your code in. It's free. That's not the "perfect-by-the-book-and-standard"-solution, but it's the solution that gets you the best grades. – nvoigt Feb 01 '15 at 07:26
  • I would be amazed if this worked with GCC. Which version did you use? – juanchopanza Feb 01 '15 at 07:45
  • Here some extra advice. If you learn C++, you will be faster if you can actually debug your programs comfortably. Unless you want to master command line gdb or emacs gdb debugging, I recommend you always write on VS first, debug your code routinely (not only when something fails), then compile it with other compilers just to make sure nothing "MS specific" slipped in. – BitTickler Feb 01 '15 at 08:26
  • Now, can anyone please explain to me why his professor would ask him to have dec2bin return a double? :) – BitTickler Feb 01 '15 at 08:33

2 Answers2

5

"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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thank you for that. I don't know how I missed it. That aside, what would make a program compile in one computer and not in another? What are the benefits of explicitly writing `std:: string`? – abrahimladha Feb 01 '15 at 07:52
  • 1
    @ibayibay1 Using `std::` namespace prefix explicitely will save you from ugly namespace clashings, when there are colliding function or class names introduced from global namespace, or others specified with `using` statements. – πάντα ῥεῖ Feb 01 '15 at 08:09
1

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 :-)

6502
  • 112,025
  • 15
  • 165
  • 265