0

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 :(

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Aia
0
/* 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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.