fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7. double dec2Bin(int value, char binaryString[])
  8. {
  9. int x = 1;
  10. string hold = "";
  11. while(x <= value){
  12. x *= 2;
  13. }
  14. x /= 2;
  15.  
  16. while(x >= 1){
  17. //cout << x << " ";
  18. if(value > x){
  19. hold += "1";
  20. value -= x;
  21. }
  22. else if(value < x){
  23. hold += "0";
  24. }
  25. else if(value == x){
  26. hold += "1";
  27. value = 0;
  28. //return hold;
  29. }
  30. x /= 2;
  31.  
  32. //cout << hold << endl;
  33. }
  34. return atoi(hold);
  35.  
  36. }
  37. int main()
  38. {
  39. char binstr[100];
  40. int num = 0;
  41. cout << "Enter a decimal string: ";
  42. cin >> num;
  43. cout << "its "<<dec2Bin(num, binstr) << endl;
  44.  
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'double dec2Bin(int, char*)':
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*)'
     return atoi(hold);
                     ^
stdout
Standard output is empty