Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,095 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,635 people online right now. Registration is fast and FREE... Join Now!




binary to decimal prog

 
Reply to this topicStart new topic

binary to decimal prog

jrice528
3 Oct, 2007 - 04:56 PM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 31


My Contributions
The idea of this program is to take a binary into decimal. this is my 2nd program, and I dont know if I am doing this right. When I enter 0's it has logic errors. Any suggestions on what to do? Its not finished and the idea is to get it to read 9 individual digits from the number entered.






CODE
#include <iostream>
using namespace std;

int main()
{
unsigned number;
unsigned count= 0;
cout<<"Please enter in a binary number, and I will give you the number ";
cout<< "in decimal form"<<endl;
cin>>number;
number = number % 10;

switch (number)
{
        case 0: number ==0;
             count=count + 0;
                           break;
        case 1: number==1;
             count=count + 1;
                           break;
default:
      
       cout << "You did not enter a binary digit"<<endl;
      
      
      
}


number = number % 100;


switch (number)
{
       case 0: number ==0;
            count=count + 0;
            break;
       case 1: number==1;
            count=count + 2;
            break;
            
default:
        cout << "You did not enter a binary digit"<<endl;
}

number= number % 1000;

switch (number)
{
       case 0: number == 0;
            count=count + 0;
            break;
       case 1: number==1;
       count=count + 4;
            break;
            
default:
        cout << "You did not enter a binary digit"<<endl;
}

cout<< count<<endl;





  
system("pause");
return 0;  
    
}




User is offlineProfile CardPM
+Quote Post

jrice528
RE: Binary To Decimal Prog
3 Oct, 2007 - 08:10 PM
Post #2

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 31


My Contributions
Ok, im really stuck, I need to turn binary into decimal. I don't really know HOW to do that. I can go from digit to digit just fine. But I dont know how to assign a number to that individual digit. I know i do the /=10 to move from digit but how would i assign a number to each digit?
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Binary To Decimal Prog
3 Oct, 2007 - 08:25 PM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
ok, a couple of things:

don't double post - you're dealing with the exact same question in another thread.

the statement number = number % 10; after the user input converts the number into a value from 0-9. are you sure that is what you want to do?

the best approach would seem to be using a loop to run through each digit of the entered number. for each digit of the binary number, test whether the value at position i is a 1 or a 0. then translate this into the decimal equivalent, and add the translated value to a storage variable.

you should probably look into using the pow(x,y) function (from cmath or math.h), which returns x raised to the y - you are likely to need it for the translation of a 1 or 0 at a particular position into it's decimal equivalent. for example, a 1 at the nth position (from the least significant end) has a decimal value of pow(2.0,n-1.0).

hope that helps get you started.

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month