HOW CAN I USE BINARY TO C++

helloo...actualy I am trying to code this thing...will anyone help me..??
But, it failed..Where can I get the recursion for binary..?
#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

int main()
{
int num;
int MyEnd;
int i;
int MyNum;

cout<<"Please enter any binary integer"<<endl;
cin>>num;


if(MyNum=(10^(num.size()-1))
cout<<num;
else
cout<<"Invalid number"<<endl;







cout<<"Press any integer number to continue..."<<endl;
cin>>MyEnd;

return 0;
}
Please explain exactly what you want. I don't think I understand :)
Input an integer containing only 0's and 1's. (i.e a binary integer) and print its decimal equivalent.
oh. Ok :) here's a function I found that displays how to convert it:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int binaryToBase10(int n) {

    int output = 0;

    for(int i=0; n > 0; i++) {

        if(n % 10 == 1) {
            output += pow(2, i);
        }
        n /= 10;
    }

    return output;
}


Similarly, if you want to convert decimal to binary, you can do:

1
2
3
4
5
6
7
8
9
10
11
12
//This will display the bits in a byte
void decimalToBase2(unsigned int n) 
{
for (int i = 128; i<n; i/2)
{
if (i&n)
cout << "1";
else
cout <<"0";
}



EDIT: And also, use the source code option to enclose your code to improve legibility (or you can just do [.code] at the beginning of your code and [/.code] at the end of it without the dots.
Last edited on
what does pow means..? what is stands for this code.. (int binaryToBase10(int n)?
By the way, it could not be executed...
Anyway, I am using the dev c+++ 4.9.9.2 but could not executed. The function 'pow' seems do not include in the software. I am also have included the header file aka header library that is, "#include<math>' still, it could not executed.
Anyway, I am using the dev c+++ 4.9.9.2


Don't.
That version is obsolete (last updated 2005).

Switch to a newer IDE and compiler, such as Orwell Dev-C++ Version 5.6.3 or code::blocks.

http://orwelldevcpp.blogspot.com/
http://www.codeblocks.org/

I am also have included the header file aka header library that is, "#include<math>'

that should be, #include <cmath>
Last edited on
This is my whole program...But, could not execute..

#include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;

int main()
{
int n;
int MyEnd;
int pow;

cout<<"Please insert any binary integer"<<endl;
cin>>n;

int output = 0;

for (int i=0; n>0; i++)
{ if (n % 10 ==1) {output+= pow(2^i);}
n/=10;}
cout<<n;
else
cout<<"Invalid binary integer"<<endl;
cout<<"Press any integer number to continue..."<<endl;
cin>>MyEnd;
return 0;
}
Pow is a function defined in the header <cmath> i think. so, do

#include <cmath>

There's a tutorial section this website:

http://www.cprogramming.com/fod/pow.html
Last edited on
Holy lack of readability, batman.

A few things. First off, it'd really help us if you could put code tags around your code. Highlight the code you've pasted, and click on the <> button to the right of the editing box. You'll get something like this:

1
2
3
4
5
#include <iostream>
int main() {
std::cout << "I AM COLORED NOW YAY!";
return 0;
}


Note that it doesn't indent your code. Something you could do too, as it really helps us (and you!) read it. What it DOES do is gives each line a number that we can easily refer to when telling you what exactly is wrong with your code.

Now. As for your problem. The line "else" and the three lines after that are rather out-of-place. You don't have an if-statement that the else needs to refer to.
http://www.cplusplus.com/doc/tutorial/control/

I understand that you want to check to make sure that the inputted number is actually binary. That's excellent! However, you're going to need to do a bit more work to get it working. That solution that IceStorm gave ignores digits that aren't 1. So the number 2310 would be treated as 0010. No alarms would be raised.

I'd like to draw your attention to "cout<<n;" as well. That's all I'll say about that.

@IceStorm
Mandatory reading: http://www.cplusplus.com/articles/DjGEy60M/
Thanks for your help, though.

-Albatross
Just as an additional help, you aren't really being asked to input binary anything.

You are being asked to convert a string (like "1010") to an integer (10).

So, when you ask for input, just read a string:

1
2
3
4
5
  string s;
  cout << "Please enter a number in binary representation> ";
  getline( cin, s );

  // now to convert s to an int 

You can validate the input on before converting, if you want, by just running through the characters in the string and complaining if any of them are not '0' or '1'.

Remember, to convert, you are adding powers of 2.

For example, 1010 is

    1*23 + 0*22 + 1*21 + 0*20

You can do that in a loop. Start with a result of zero (no digits read).
For each digit, starting from the left ('0' or '1'):
- multiply your result by 2 (power up!)
- add 0 or 1 depending on whether the digit is '0' or '1'

You'll need to work this out in your head before it makes sense.

Good luck!
If this wasn't a homework assignment you could have simply used a std::bitset<> something like this would do the trick:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <bitset>

int main()
{
    std::string binaryValue = "";
    std::cout << "Please enter a binary number(16 bits max): ";
    std::getline(std::cin, binaryValue);

    std::size_t const bits = 16;
    std::bitset<bits> binary(binaryValue);

    std::cout << "The decimal value of " << binary << " is " << 
    binary.to_ulong() << std::endl;

    return 0;
}
Please enter a binary number(16 bits max): 1111
The decimal value of 1111 is 15
Last edited on
Don't use Dev C++. VS2010 or C::B is much better, bleh
Thank god, I got no error. But, the thing is, I have to do more operations to make it execute. And, I do not know. Can anyone help me coding this part?
Example:
Using the formula of:=
1010= (1010%10=0)
1010/10= 101
Then again,
(101%10=1)
And recursively...
From that, I could make it like this, (2*0+0)+(2*1+1).....
But, I do not know how I could add up in the next coding...
for (int i=0; n>0; i++)
if (n % 10 ==1) output+=(2^i);
n/=10;
m=(n%10);
You could do something like:

1
2
3
4
5
6
7
8
unsigned powerOfTwo = 1;
unsigned decimal = 0;
while(binary) //while not equal to 0
{
    decimal += binary % 10 * powerOfTwo; //add the bit (if 0 add nothing)
    powerOfTwo <<= 1; //get the next bit value if set
    binary /= 10; //remove right most bit
}
Topic archived. No new replies allowed.