ive been working on the binary to decimal converter program. not having a fun time with it!!

this is what i have:

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


int main ( void ){

/* variables */

int decimalValue = 0;
int counter = 0;
int nextDigit = 0;
char binary = 0;

/* begin */
printf("Please enter a binary string of 1's and 0's: ");
scanf("%c",&binary);

/* get 1st digit of binary */
nextDigit = binary % 10;

while( binary )
{

if( nextDigit != 0 )
{

decimalValue += (int) pow(2,counter);

}
counter++;
/* get next digit of binary */

binary = binary / 10;
nextDigit = binary % 10;

}

printf("The Value is %i in decimal\n",decimalValue);
system ("PAUSE");
return 0;
} /* main */

it likes to return 3 as an answer no matter what. i cant figure out where its going wrong :(

/* begin */
printf("Please enter a binary string of 1's and 0's: ");
scanf("%c",&binary);

Are you teasing the user? You ask for a string, and then only care about the first char.

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.