Thread: Convert a binary number into a decimal number

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    3

    Convert a binary number into a decimal number

    Here's the part of the codes where I tried to use boolean expression:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      int num;
      cout << "8-bit Binary Number=";
      cin >> num;
      
      bool valid = true;
    .
    .
    .
    .
    .
    .
    .
    .
    .
    if (valid)
    {
        cout << "Decimal Number = " << sum;
      }
    }
    May I know that how can I get started with the body?

    Thanks for your guides!

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    May I know that how can I get started with the body?
    The expected behavior is not clear. Please provide input and corresponding output examples.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless you have a design in mind, start with a flowchart to flesh out the logic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Note the default for cin and cout is to input and output decimal strings, while the numbers are stored internally as binary numbers. So it appears that your program will input a decimal string into a binary number, then convert the number as if the input was a binary string. You'll have to assume the user only input the characters '0' and '1' for the number. The program could input a string, then convert that string into a number.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    Hi guys, thanks for your replies!
    These are the output expected from me,
    case 1:
    8-bit Binary Number= 11111111
    Decimal Number = 255

    case2:
    8-bit Binary Number=10101010
    Decimal Number = 170

    case 3:
    8-bit Binary Number=101010102
    Number entered is not a binary number

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The program should input a string.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    1
    Hi there, I bet you're an IT student from MMU .Right? =P

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    i Bet it too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  2. Binary Number to Decimal
    By theCanuck in forum C++ Programming
    Replies: 12
    Last Post: 02-09-2011, 11:25 PM
  3. Replies: 15
    Last Post: 10-06-2009, 11:20 PM
  4. how to convert decimal to floating point number
    By -EquinoX- in forum C Programming
    Replies: 98
    Last Post: 03-04-2008, 01:25 PM
  5. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 06:19 PM

Tags for this Thread