binary to decimal conversion

the program converts binary numbers input from user into decimal in c+

Page 1 of 1

4 Replies - 3221 Views - Last Post: 19 October 2010 - 06:31 PM Rate Topic: -----

#1 iambewildered  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!!

:code:

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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,423
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 iambewildered  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,015
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: binary to decimal conversion

Posted 19 October 2010 - 06:31 PM

View Postiambewildered, 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.

Let's look at what the compiler says about your code
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 :code: 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1