Binary to Decimal Converter

This is a discussion on Binary to Decimal Converter within the C Programming forums, part of the General Programming Boards category; I'm trying to make a program that takes up to a seven digit binary number and converts it to its ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Binary to Decimal Converter

    I'm trying to make a program that takes up to a seven digit binary number and converts it to its decimal equivalent. I'm still a beginner, so this might be a simple question but how do I stop the user from entering anything but 1s and 0s? This means no decimals or numbers other than 1 or 0.

    I already made it so it won't accept anything below 0 or above 1111111.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Welcome to the forum. Next time you post, make sure you post your try in code tags, like these [code]/* your code*/[/code].

    Because it's your first post, I am going to give a sample code, in order to get ideas. Let's say I have an array, that I wanted to be filled only with zeroes and ones, by the user, in whatever cell the user wants too.
    I could do that with this code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
         int array[5];
    
         /* i will go through all the cells of the array */
         int i;
    
         for( i = 0 ; i < 5 ; i++)
         {
             do{
                 /* Print message and read element,
                  * until it gets the value 1 or 0 */
                 printf("Input %i-th element\n", i);
                 scanf("%d", &array[i]);
             }while(array[i] != 1 && array[i] != 0);
         }
    
         printf("The array is:\n");
         for( i = 0 ; i < 5 ; i++)
         {
             printf("array[%d] = %d\n", i, array[i]);
         }
    
         return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-03-2012, 03:42 AM
  2. Hex String to Decimal converter
    By wrex in forum C Programming
    Replies: 16
    Last Post: 11-05-2008, 05:06 PM
  3. Decimal to Hex Converter
    By rocketman03 in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:50 AM
  4. Help with a decimal to binary converter
    By danielerasmus in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2008, 10:37 AM
  5. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21