Thread: Binary to Decimal Converter

  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
    Nice, France
    Posts
    2,694
    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, 04:42 AM
  2. Hex String to Decimal converter
    By wrex in forum C Programming
    Replies: 16
    Last Post: 11-05-2008, 06: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, 11:37 AM
  5. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM