convert from decimal to binary

i have to convert from decimal string to binary in class in 3 files
here is my program:
Decbin.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Dec2Bin.h"
#include<iostream>
using namespace std;
   
Dec2Bin::Dec2Bin(string decimal)
{
    Dec = decimal;
}
 
int Dec2Bin::GetBinary()
{
    return Bin;
}
 
void Dec2Bin::convert()
{
    Bin = 1;
    for(int i = 0; i < Dec.size(); i++)
    {
                Bin *= 10;
                Bin += Dec[i] - '0';
    }
}

Dec2Bin.h
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
class Dec2Bin
{
    string Dec;
    int Bin;  
    public:
    void convert();
    Dec2Bin(std::string);
    int GetBinary();
};

the main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>
#include "Dec2Bin.h"
using namespace std;
int main()
{
string s=("d43");
Dec2Bin conv(s);
conv.convert();
cout<<conv.GetBinary()<<endl;
return 0;
}

the answaer must be :b00101010 ,but im getting 6243 in the output
can any one help me?
Last edited on
The answer must be : b00101010, but I'm getting 6243 in the output

That means you haven't even tried to convert the decimal into binary. At least please show us the number with only 0 and 1.
You should definitely attempt your homework before asking for help.
Googling "How to convert numbers to binary" brought up loads of results.
i think you didn't see my code @Patagon
I think you didn't see my code @Patagon

That is not true.

1
2
3
4
5
6
7
8
9
void Dec2Bin::convert()
{
    Bin = 1;
    for(int i = 0; i < Dec.size(); i++)
    {
                Bin *= 10;
                Bin += Dec[i] - '0';
    }
}

That is NOT the code to convert decimal to binary.

The answer must be : b00101010, but I'm getting 6243 in the output

What answer is this? Is it even a binary number?
exactly here is my problem i saw some codesn on google but they wasn't with string,my problem :i don't know how i can convert the numbers with string . i thought that my code is a part of the solution
Topic archived. No new replies allowed.