Dec 4, 2016 at 12:22am UTC
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 Dec 4, 2016 at 12:40am UTC
Dec 4, 2016 at 3:07am UTC
You should definitely attempt your homework before asking for help.
Googling "How to convert numbers to binary" brought up loads of results.
Dec 4, 2016 at 12:21pm UTC
i think you didn't see my code @Patagon
Dec 4, 2016 at 3:14pm UTC
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