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 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++)
{
}
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!

## All 15 Replies

``decToBin(name[i],2)=answer;``

This is illegal. What did you want with this line?

i was trying to make it into an integer, because when i try to just div 10 the decToBin(name,2) it won't let me... thats my understanding of what i had to do. But it didn't work. So yeah thats why i am asking here, sorry limited knowledge :(

A litle of help. In your for loop you can make a temp array wich will convert the name to decimal with atoi.

``````char tmp = { 0, 0 };
tmp = name[i];

and then join them together

Figure this out

I think some major clarificarion is needed.

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.

Do you mean you want to convert series of 1's and 0's typed in by the user into it's octal (base 8) representation? That's what this example shows. There is nothing decimal (base 10) about it. In decimal, this value is 37, not 43.

if im corect um u said u wanted to put together 4 and 3 to make 43 well u can do that by this math equation 4*10+3 then suplement thouthes for there bionary equivlents or whatever conversion u want. also if u wand the user to input the number you could do a*10+b. also if u wana do division lots of times it wont come out even so you have to use float on the variables?
dont know if that was any help but there u go

sorry about my explanation... i don't wish to change into decimal, i was wanting to get the output of the binary's and add each binary's 1's...
1101110 number of 1's = 5
1001101 number of 1's = 4
1011011 number of 1's = 5

Then join those numbers together, so the number would be 545.
Sorry for the misunderstanding, my bad.

Some basic parts:

``````#include <stdio.h>

int count_ones(const char *text)
{
int ones = 0;
for ( ; *text; ++text )
{
if ( *text == '1' )
{
++ones;
}
}
return ones;
}

int main(void)
{
const char *text[] =
{
"1101110", "1001101", "1011011",
};
int total = 0;
size_t i;
for ( i = 0; i < sizeof text / sizeof *text; ++i )
{
total *= 10;
total += count_ones(text[i]);
}
printf("total = %d\n", total);
return 0;
}

/* my output
total = 545
*/``````

thanks for your help guys appreciate it...

i am struggling to grasp the concept! but not to worry, as i was going to ask if anyone knew of some good tutorials or books that i can get to gain a better understanding.. i just can't comprehend the logic to what i need to do. So i figure i need to go back to step 1.

Thanks again guys.

That depends exactly what part you don't understand. Essentially, your problem seems to be about looping through arrays, and performing some operation on each element. If you're not comfortable with arrays, then do a google search for array tutorials, or check:
http://www.daniweb.com/tutorials/tutorial1732.html
http://www.cprogramming.com/tutorial/lesson8.html
Here's one for 'C' Style strings, which are null terminated arrays of char
http://www.cprogramming.com/tutorial/c/lesson9.html
and one for looping
http://www.cprogramming.com/tutorial/lesson3.html

You might find it handy to bite a smaller chunk off your problem, and create a simple program to solve that. for example, write a program which takes a series of 1's and 0's, and outputs the number of 1's in that series.

Create a table of commonly used binary digits with decimal digits and just memorize it

cheers for them, ill take a look over it.

here is an update on my array and how to add them together, although it doesn't seem to add them for me... i have kept the code to output the binary (to make sure it is still doing it) but when i try add together i always get 0...

``````for (unsigned int i=0; i<name.size(); i++)
{
cout << name[i] << " binary (";
decToBin(name[i], 2);
cout << ")" << endl;

char tmp = { 0, 0 };
tmp = name[i];

if (name[i]=='1')
{
count++;
}

}
system("PAUSE");
return 0;``````

So my output is:
j binary (binary #)
o binary (binary #)
h binary (binary #)
n binary (binary #)
0

i also tried to use

``````if (answer[i]=='1')
count++;``````

Ok here it is

``````for (unsigned int i=0; i<name.size(); i++)
{
cout << name[i] << " binary (";
decToBin(name[i], 2);
cout << ")" << endl;

char tmp = { 0, 0 };
tmp = name[i];
}``````

but you should figured this out from post num. 8. You need to initialise `answer` to zero `int answer = 0;` OK I shouldn't do it but you showed some effort

hey thanks for your ongoing help andor, appreciate it.

My only concern is that, no matter what string i put in, it outputs 4006960. I am getting closer, but not quite there...
Maybe it is not being split up correctly.
If i put in john i should get a value of: 4635
j has 4 1's. o has 6 1's. h has 3 1's. n has 5 1's.

I tried a few changes with the code you gave me, to see if the 4006960 would come up again

``````char tmp = { 0, 0 };
tmp = name[i];

i still had the same output of 4006960, so i get the feeling i am adding all the binary's together. So, 1101010 + 1101111 + 1101000 + 1101110 = 4006960. I believe the program isn't finding the number of 1's in the first binary and then adding that to the number of 1's found in the second binary. I might not have explained it well. With the above output, i should get: 4635.

Sorry for being so unclear with what i was trying to do. Thanks again for all your help, appreciate it.

``````#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
void decToBin(int number, int base);
int numOnes = 0;

int main()
{
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++)
{
cout << name[i] << " binary (";
decToBin(name[i], 2);
cout << ")" << endl;

numOnes = 0;
//      char tmp = { 0, 0 };
//      tmp = name[i];
}
system("PAUSE");
return 0;
}
void decToBin (int number, int base)
{
if (number>0)
{
int binDigit;
decToBin(number/base, base);
binDigit = number%base;
if (binDigit == 1)
numOnes++;
cout << (int) binDigit;
}
}``````
commented: very helpful indeed, helped me through my problem +1

Thanks heaps. Sorry for putting you through that haha.

Thankyou to everyone else's input too, extremely helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.