954,148 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error message!

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;
}
megan-smith
Newbie Poster
13 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

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;
}
megan-smith
Newbie Poster
13 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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

megan-smith
Newbie Poster
13 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

megan-smith
Newbie Poster
13 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

So post your latest effort, not a description of what you may have tried.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You