7 Replies - 1328 Views - Last Post: 15 August 2012 - 11:55 AM Rate Topic: -----

#1 ma21212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-July 12

Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:00 AM

I've written a program that asks the user to input a number using strings, the program then will convert that number to decimal, however Im having a problem with it, when I compile (using -lm) and run the a.out, lets say I try to convert 11 to decimal it gives a huge number... not sure where I went wrong so would anyone help please?
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{

 char string[100];
 int s;
 char a;
 char j;
 int sum;

 printf("B = B to D\n");
 printf("D = D to B\n");
 printf("choose which one to convert to:");
 scanf("%c%c", &a, &j);

 if (a == 'B')
 {
        printf("enter binary number to convert to decimal: ");
        scanf("%s", string);

        for(s = strlen(string)-1; s >= 0; s--)
        {

                if(string[s] == '1')
                {
                sum = sum + pow(2,s);

                }
        }
 printf("the decimal number is: %d\n", sum);
 }

 return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Converting Binary to Decimal using strings

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:14 AM

sum = sum + pow(2,s);


What is the initial value of sum the first time you use it in this expression?
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1707
  • View blog
  • Posts: 3,275
  • Joined: 30-May 10

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:15 AM

You need to initialise sum.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:17 AM

Sum looks to be uninitialized.
Was This Post Helpful? 0
  • +
  • -

#5 ma21212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-July 12

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:23 AM

derp that fixes everything.... thank you. Now this is a akward thing to ask but, how would I be able to convert the other way? from decimal to binary, im guessing I need a remainder and and a % but Im not really sure, any help?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:29 AM

Actually, before you go further, try the binary number "100" and see what result you get. I think you'll need to fix that problem first before you move on.

As for decimal to binary, how do you do it on paper? Can you articulate the steps you take? If so, then you can write a program that follows those steps.

Aren't you getting enough advice from here and here?
Was This Post Helpful? 1
  • +
  • -

#7 ma21212  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-July 12

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:32 AM

Yeah your right, it only gives me odd numbers...
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:55 AM

The binary number "1101" is also odd, but I bet it also is giving you the incorrect answer. It's not a matter of odd or even, but the place values you are giving each digit.

Here's a hint: in decimal, a number 325 is: 3*100 + 2*10 + 5*1 = 3 * 10^2 + 2*10^1 + 5*10^0.

Step through your code and see how you are using the pow() function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1