# binary to decimal conversion

Page 1 of 1

## 4 Replies - 7245 Views - Last Post: 19 October 2010 - 06:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=195628&amp;s=6644c2f31857bf0cad4150bb1a0f74c8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iambewildered

Reputation: 0
• Posts: 2
• Joined: 18-October 10

# binary to decimal conversion

Posted 18 October 2010 - 04:04 PM

//this program is supposed to convert the binary numbers into decimal numbers

```#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
string binary;
cout << "Enter the binary value you wish to convert into decimal: " << endl;
cin >> binary;

int counter = 0;
double decimal = 0;

while (counter < binary.size())
{
if(binary[counter] = 1)
{
decimal = (decimal + pow(2.0,counter));
}
else
decimal = (decimal +pow(0.0, counter));

counter++;
}
cout << "The decimal representation of the given binary is: " << decimal << endl;

return 0;
}
```

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 18 October 2010 - 04:07 PM

Is This A Good Question/Topic? 0

## Replies To: binary to decimal conversion

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: binary to decimal conversion

Posted 18 October 2010 - 04:07 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
• Post the code you're having problems with DONE
• Post the exact error you're receiving, if you are receiving one
• If no error explain what the code is doing versus what you want it to do
• Post your question in the body of your post, not the description field

This:
```        if(binary[counter] = 1)
```

is an assignment, not a comparison. == is for comparison.

This post has been edited by JackOfAllTrades: 18 October 2010 - 04:08 PM

### #3 iambewildered

Reputation: 0
• Posts: 2
• Joined: 18-October 10

## Re: binary to decimal conversion

Posted 19 October 2010 - 06:22 PM

the program gives the decimal value for any binary input as it were all one's. in other words the program will output 15 if you have 1111 or 1010 as your binary input. apparently the program doesn't go through the if else statements that specify the multiplication of 2 only in case of binary 1. what do i do? oh and the comparison operator doesn't work at all in this program. i tried that before

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3795
• Posts: 13,738
• Joined: 08-August 08

## Re: binary to decimal conversion

Posted 19 October 2010 - 06:30 PM

Did you fix line 18? As it's written, it will return true if binary[counter] is successfully set to 1.

### #5 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: binary to decimal conversion

Posted 19 October 2010 - 06:31 PM

iambewildered, on 20 October 2010 - 10:22 AM, said:

the comparison operator doesn't work at all in this program. i tried that before

What?
Seriously, what?
That's definitely a contender for the weirdest thing I have seen written down this week.

```DIC.cpp: In function ‘int main()’:
DIC.cpp:16: warning: comparison between signed and unsigned integer expressions
DIC.cpp:18: warning: suggest parentheses around assignment used as truth value

```

So it is very unhappy about you using assignment "=" when you are trying to do a comparison.

The fact that your mistakes are greater and deeper than that one doesn't mean that one is not a mistake.

Fix it, retest your code, if it is still not working post your new code in code tags, like this and answer these questions.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
( e ) What have you already tried to fix it?