Hey guys,
I was wondering if anyone can help me in terms of using atoi to get my my results to add the 1's of my binary output and then join them together... eg. 110101 and 100101 is 4 and 3 = 43.
I was thinking of integer dividing the number by 10, but i am unsure of how to do that.
Here is my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void decToBin(int number, int base);
int main()
{
int answer;
int count=0;
cout<<"Enter a string: ";
string name;
getline(cin,name);
/* for (unsigned int i=0; i<name.size(); i++)
{
cout << name[i] << " binary (";
decToBin(name[i], 2);
cout << ")" << endl;
}
*/
for (unsigned int i=0; i<name.size(); i++)
{
decToBin(name[i],2)=answer;
answer=answer/10;
cout<<answer<<endl;
}
system("PAUSE");
return 0;
}
void decToBin (int number, int base)
{
if (number>0)
{
decToBin(number/base, base);
cout << number%base;
}
}
The commented part gets the numbers into binary, the other loop under it i was trying to do so decToBin became an int and could then div 10.
As you can tell i am not sure of the direction i should go, any help would be appreciated, thanks!