Menu

Help in solving a code problem in .c

Mantero
2011-11-17
2012-09-26
  • Mantero

    Mantero - 2011-11-17

    Ok so I tryed to make a program in which i could enter any binary number and
    transform it into a octal number (example. "111" would be "7"). The problem is
    that when I am compiling it, it shows a error saying:

    In function 'main'.
    syntax error at the end of input
    //It reefers to like 46, the one with the last brackets//
    

    So my code is bellow and I hope you are able to understand it and solve my
    problem, ty in advance.

    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    #include <string.h>
    main()
    {
          int b[100],n,i,j,k,l;
          char a[100];
          L1:printf("Unesite binarni broj: ");
          gets(a);
          n=strlen(a);
          for(i=0;i<n;i++)
          {
                          if(a[i]!='1' && a[i]!='0') goto L1;
                          }
          for(i=n-1,k=n-2,l=n-3,j=0;n>0;n--,j++)
          {
                            if(a[l]=='1')
                            {
                                           if(a[k]=='1')
                                           {
                                                           if(a[i]=='1') b[j]=7;
                                                           else b[j]=6;
                                                           }
                                           else
                                           { 
                                                           if(a[i]=='1') b[j]=5;
                                                           else b[j]=4;
                                                           }
                                           }
                            else
                            {
                                           if(a[k]=='1') 
                                           {
                                                          if(a[i]=='1') b[j]=3;
                                                          else b[j]=2;
                                                          }
                                           else
                                           {
                                                          if(a[i]=='1') b[j]=1;
                                                          else b[j]=0;
                                                          }
                                           }
          for(i=0;i<=j;i++)
          {
                           printf("%d",b[i]);
                           }
          getch();
    }
    
     
  • Mantero

    Mantero - 2011-11-17

    for(i=0;i<=j;i++)
    {
    printf("%d",b_);
    }
    getch();
    }
    This is the correct last part, for some reason the _ won't show in the code
    above.__

     
  • Mantero

    Mantero - 2011-11-17

    the "i" between the brackets wont show after <<(printf("%d",b);>>>
    sorry very much for double replye to my own post

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-17

    Well for starters you are missing a closing bracket "}"
    so there is an unepected EOF

    I would try to re-check all the brackets.

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-17

    I had to download and re-install Dev-C++ to compile this project and I have to
    say that although you have the right idea, there are several issues that need
    to be looked at.
    Once you add the nessarry bracket you will find that out.

    Add your closing bracket just before the final "for" loop.

    I will help you further if you would like, but for now I will point you in the
    right direction and let you see what else you can find out.

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-17

    include <stdio.h>

    #include <math.h>
    #include <conio.h>
    #include <string.h>
    
    int main()
    {
        int b[100],n,i,j,k,l;
        char a[100];
        L1:printf("Unesite binarni broj: ");
        gets(a);
        n=strlen(a);
        for(i=0;i<n;i++)
        {
            if(a[i]!='1' && a[i]!='0') goto L1;
        }
        for(i=n-1,k=n-2,l=n-3,j=0;n>0;n--,j++)
        {
            if(a[l]=='1')
            {
                if(a[k]=='1')
                {
                    if(a[k]=='1') b[j]=7;
                    else b[j]=6;
                }
                else
                { 
                    if(a[k]=='1') b[j]=5;
                    else b[j]=4;
                }
            }
            else
            {
                if(a[k]=='1') 
                {
                    if(a[k]=='1') b[j]=3;
                    else b[j]=2;
                }
                else
                {
                    if(a[k]=='1') b[j]=1;
                    else b[j]=0;
                }
            }
        }
        for(i=0;i<=j;i++)
        {
            printf("%d",b);
        }
        getch();
    }
    

    This compiles for me, although it does not work. I had to add the a to give
    the a a reference point or it was comparing a string to a number with the
    compilier would not allow.

    May I also suggest that you avoid the use of the GOTO command. Although it is
    very easy to use, it can cause you more headaches that it is worth.

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-18

    Well I was looking at your problem and decided to see what I would do if I
    were to be asked to write a program that did the same thing. First I would
    convert to decimal first and then convert to oct. Just seems easier to me that
    way. My binToDec function will do that for you and I have tested it. It can
    use some work to handle longer binary numbers but works fine on teh smaller
    ones.

    int binToDec (int binNum)
    {
        int count = 0;
        int returnVal = 0;
    
        while (binNum>0)
        {
              if (binNum % 10) returnVal += pow(2,count);
              binNum /= 10;
              count++;      
        }
        return returnVal;
    }
    

    Have not written the decToOct function yet but what I have written here may
    help you solve your own problem. If I do not finish this tonight, you will not
    hear from me for the rest of the week as I am leaving for holidays.

    Curtis

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-18

    Made a decToOct function. It too works fine (as far as I can tell). See if
    there is anything that you can use to made your program work.

    int decToOct (int decNum)
    {
        int count = 0;
        int powNum = 0;
        int maxPow = 0;
        int multi = 1;
        int returnVal = 0;
    
        while (decNum>0)
        {
              while (decNum >= pow(8,powNum)) powNum++;
              powNum--;
              if (powNum > maxPow) maxPow = powNum;
              while (decNum >= multi * pow(8,powNum)) multi++;
              multi--;
              returnVal += multi;
              if (powNum) returnVal *= 10;
              decNum -= (multi * pow(8,powNum));
              powNum = 0;
              multi = 1;
              count++;
        }
        while (maxPow > count)
        {
              returnVal *= 10;
              count++;
        }
        return returnVal;
    }
    

    Curtis

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-18

    int main()

    {
         printf ("%d",decToOct(binToDec(1001101011)));
        getch();
    }
    
     
  • Mantero

    Mantero - 2011-11-18

    Thank very much to everyones help! I know I still got some problems to solve
    that could come up, but this was mainly the main obstacle right now(now
    solved), this program was only for made for a binary number of this type
    "111"(atleast I wanted to so). now I am going to solve the problem if someone
    just enters "1111" in which I particulary have to add another 2 zeros.
    Well, ty very much again.

     
  • Mantero

    Mantero - 2011-11-18

    dam... my english up there is verrryyyy bad!
    Sorry for double replying again.

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-18

    Not understanding why you need to add another 2 zeros if someone enters
    "1111", do you need the number padded to 6 or eight digits? ex: 11 = 110000 or
    1010 = 101000

     
  • Mantero

    Mantero - 2011-11-18

    if someone enters 1111, then I add 2 zeros for "001 111" so my program works
    but I came upon a more complicated problem which I can't solve without being a
    really good programer, so I am going to use my "Bin to Dek" & "Dek to Okt"
    code which I made myself too.(Works without a problem)

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-18

    I do not understand the need for the leading zeros. they will not affect the
    actual value of the entered number. If you look through my code you will see
    that they are unnessarry and adding them could why you are having issues. If
    you were needing to add the numbers to the end then the values would change.
    you should re-look at my code. It can handle 0001111 or 1111 as they will come
    to the same number. Other than that, you would need to post your own functions
    so I can try to determine where they are having issues.

     
  • Curtis Sutter

    Curtis Sutter - 2011-11-18

    As for adding leading zeros, if you must do that then simply take the length
    of your string % 3 and do a switch case on the remainder to determine how many
    to add then string = "" & String

     
  • Mantero

    Mantero - 2011-11-18

    Ty for helping curtis8, but I already made 4 programs with avoiding that code
    I started with-names are here:
    Okt to Bin >> Okt to Dek>Dek to Bin
    Okt to Hex >> Okt to Dek>Dek to Hex
    Hex to Bin >> Hex to Dek>Dek to Bin
    Hex to Okt >> Hex to Dek>Dek to Okt
    Ty again for your help, everyone gave me some more knowledge about coding in
    Dev-c++.

     
  • jonsi

    jonsi - 2012-05-18

    Hi I use trial version of programming
    software
    because I can't understand Which of
    them are the best.

     
  • Diana Lauren Anderson

    I learned something new again. Shall use this. Thanks!

     

Log in to post a comment.