-1

I've managed to convert an array of char into a string but now I want to do the other way around, I tried using strcpy in my code but it doesn't seem to give me what I want. The expected result should be 5, I'm getting 40959 as a result

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>



using namespace std;

string DecToBin(int);
int BinToDec(string);

int main()
{
    int x = 5;
    string y = DecToBin(x);
    reverse(y.begin(), y.end());
    int z = BinToDec(y);
    cout << z << endl;

}


string DecToBin(int num)
{

    char res[16];
    for (int n = 15; n >= 0; n--)
    {
        if ((num - pow(2, n)) >= 0)
        {
            res[n] = '1';
            num -= pow(2, n);
        }
        else
        {
            res[n] = '0';
        }

    }

    for (int n = 15; n >= 0; n--)
    {
        res[n];
    }

    return res;

}

int BinToDec(string num)
{

    char x[16];
    strcpy(x, num.c_str());
    int res;
    for (int n = 15; n >= 0; n--)
    {
        if (x[n] == '1')
        {
            res += pow(2, n);
        }
    }

    return res;
}
Renz
  • 37
  • 7
  • 4
    I dont quite understand the question, string IS an array of chars. The object std::string returns a pointer to the char array using the method c_str(), which returns const char* (a pointer to the first element in the char array). – TCS Dec 13 '14 at 13:27
  • yes I know but, my DecToBin function is for arrays, and if I try to use it with data type string it wont give me what I want. – Renz Dec 13 '14 at 13:30
  • why are you reversing the array in your main function? – Jimmy Dec 13 '14 at 13:31
  • @Jimmy because for some reason it gives me the reverse of my expected answer – Renz Dec 13 '14 at 13:59

1 Answers1

1

I can find the following errors:

  1. res is not zero-terminated in DecToBin before it is copied into the string,
  2. res is not initialized in BinToDec,
  3. You reverse the binary representation but try to read it back in the same way you wrote it.

This looks to me like homework, so I leave fixing the code to you. In the event that you really need this productively for something, an easier way to achieve this (that will not be accepted as homework anywhere) is

#include <iostream>
#include <bitset>
#include <string>

using namespace std;

string DecToBin(int num) {
  std::bitset<16> b(num);
  return b.to_string();
}

int BinToDec(string num) {
  std::bitset<16> b(num);
  return static_cast<int>(b.to_ulong());
}
Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • yes this solution would be a lot faster, but we're not allowed to use bitset functions :) But thank you anyway – Renz Dec 13 '14 at 14:01
  • and res is inside of BintoDec – Renz Dec 13 '14 at 14:01
  • There is a `res` in both functions. In `DecToBin` it's a C-style string (or would be, were it zero-terminated), in `BinToDec` it's an `int`. – Wintermute Dec 13 '14 at 14:05
  • it's a local variable so it shouldn't matter right? – Renz Dec 13 '14 at 14:49
  • 1
    No, it's not a problem to have local variables with the same name in different functions. You still have to initialize them properly for what you want to do with them, though. For example, in `BinToDec`, you want `int res = 0;` instead of `int res;`, or you'll be adding stuff to nonsense later. – Wintermute Dec 13 '14 at 14:52
  • oh, I tried it, but it still gives me the same results – Renz Dec 13 '14 at 16:21
  • 1
    I'm pretty sure we'll be told to take this to chat in a moment, so: last attempt. Read the list again. Did you fix `res` **in both functions**? Then you should get 40960 (2^15 + 2^13), because you reversed the string but read it back in the original order. Simple fix: Don't reverse the string. – Wintermute Dec 13 '14 at 16:33