Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C program (string - binary to decimal conversion)

  1. Dec 4, 2006 #1
    here's what i have:

    Code (Text):
    #include<stdio.h>
    #include<string.h>
    main() {
    char string[32];
    int sum = 0;
    int a = 1;
    int i;
    printf("Please enter numbers:\n");
    gets(string);
    for (i=32;i<0;i--)
    {
    if(string[i] ==1)
    { sum = sum + a;
    }
    a = a * 2;
    printf("The converted string is: %s \n", string[i]);
    }
    won't compile.. says "compound" statement is missing at the end.

    what am i doing wrong?



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Dec 4, 2006 #2

    berkeman

    User Avatar

    Staff: Mentor

    Count the number of left and right braces {}. You just spaced the one at the end of the main().
     
  4. Dec 15, 2006 #3
    have you noticed one thing....that 1)in the program you have opened three braces and two closed braces{}, 2)why don't you declare as
    int sum=o,a=1,i; 3)why don't you prefer to use getch( ); before the ending brace.
     
  5. Dec 15, 2006 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It's a matter of style. Some prefer not to declare many things on one line, especially if there are initializers or modifiers involved.

    (A) There's no reason to wait for a keypress
    (B) getch is a nonstandard extension to the C language. You can only use it if you happen to be using a compiler or library that defines it. (Why not use getchar() instead?)
     
  6. Dec 15, 2006 #5

    jim mcnamara

    User Avatar

    Staff: Mentor

    Did your teacher ever mention strtoul() ?
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook