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;
}