hey there;
i wanted to write a program to convert binary to decimal.
i did that using the " character approach".
it works just fine.
then i decided to enhance the program by adding an error message whenever the input are invalid (i.e anything other than 0, 1 ), but i havent been able to figure it out yet.
i've been playing around with it but it is still not coming.
my knowledge of C program is very limited ( i've just started learning C).
was wondering if you guys could possibly help me with it.
any help is appreciated!

here is the codes of what i wrote:

#include <stdlib.h>
#include <stdio.h>

int main ()
{
    int decimalValue=0;
    int nextDigit;
    char binary;
    
    printf(" Enter Binary Number ! \n");
    binary = getchar();
    
    while (binary == '1' || binary == '0')
          {                 
               if (  binary == '1')
                    { 
                             nextDigit=1;           
                    }
                    
               else   
                     {                     
                             nextDigit=0;
                     }      
                                          
                            
                                  
              decimalValue = decimalValue*2 + nextDigit ; 
              binary = getchar();
                      
          }
          
    printf ("The Decimal Value= %i\n", decimalValue);
    
    system("PAUSE");
    return 0;
}

Recommended Answers

All 8 Replies

hey there;
i wanted to write a program to convert binary to decimal.
i did that using the " character approach".
it works just fine.
then i decided to enhance the program by adding an error message whenever the input are invalid (i.e anything other than 0, 1 ), but i havent been able to figure it out yet.
i've been playing around with it but it is still not coming.

I must be missing something because there seems to be no attempt in the posted code to deal with invalid input. We therefore can't tell what you are doing wrong.

here's the best i came up with.
but sitll, it doesn't account for invalid numbers which start with 0 or 1 ( ie 123)!
thanks in advance.

#include <stdlib.h>
#include <stdio.h>

int main ()
{
    int decimalValue=0;
    int nextDigit;
    char binary;

    
    printf("Enter Binary Number ! \n");
    binary = getchar();
    
    
    if (binary > '1' || binary < '0')
          {
              printf("ERROR!  \n");
          }
    else
          {
                    while (binary == '1' || binary == '0')
                          {                 
                                              if (  binary == '1')
                                                    { 
                                                              nextDigit=1;           
                                                    }
                    
                                              else   
                                                    {                     
                                                                nextDigit=0;
                                                    }      
                                          
                            
                                  
                              decimalValue = decimalValue*2 + nextDigit ; 
                              binary = getchar();
 
                                                   
                                                            
                                                   
                                               
                                                                    
                          }   
                        
                             
                                                              
                          
                  
                       printf ("The Decimal Value = %i\n", decimalValue);
                                                                                
            }
            
   
    
    system("PAUSE");
    return 0;
}

Dave's short version: read all user input as the text it is; interpret it at your leisure.

Dave's short version: read all user input as the text it is; interpret it at your leisure.

but how?
I read the very first input.
but i'm stuck when it comes to the the second input and third and ....
it would have been great had you explained a bit more.
thanks though.
cheers

> but sitll, it doesn't account for invalid numbers which start with 0 or 1 ( ie 123)!
Maybe put the 'if' statement inside the 'while' loop ?

But separating input from conversion is good as well, as Dave has remarked.

putting if statement in the while loop didn't seem to work. at least the way i did it .
Could you please show on the codes what you meant.

how do you seperate the input form the conversion?
the link Dave gave out was way too high-level for me, which i did'nt really understand.
Could you be more explicit and clear.
I told ya.
I'm just beginning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.