need help debugging a program that converts integer to binary

This is a discussion on need help debugging a program that converts integer to binary within the C Programming forums, part of the General Programming Boards category; i wrote a program that was supposed to run a function that makes a conversion of inter digits to binary ...

  1. #1
    bpf
    bpf is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    7

    need help debugging a program that converts integer to binary

    i wrote a program that was supposed to run a function that makes a conversion of inter digits to binary

    when i try to compile the program, the following errors come up:

    int_to_bin.c:33: error: conflicting types for 'counv'
    int_to_bin.c:28: error: previous implicit declaration of 'counv' was here
    int_to_bin.c: In function 'counv':
    int_to_bin.c:87: warning: return makes integer from pointer without a cast
    int_to_bin.c:87: warning: function returns address of local variable

    here is the code:
    Code:
    /********************************************************
     *							*
     * FILE: ch10_ex4.c					*
     * CREATED: 8/15/07					*
     * BY: bpf						*
     *							*
     *  this program will run a function that converts an	*
     *   integer to the binary code for each of the		*
     *   integer's digits					*
     *							*
     ********************************************************/
    
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
      char line[100];
      char itgr[100]; /* the integer value of the input number */
      char conv(); /* function prototype */
    
      /* input number */
      (void)printf("enter an integer: ");
      (void)fgets(line, sizeof(line), stdin);
      (void)sscanf(line, "%s", &itgr);
    
      /* print new number */
      (void)printf("%c\n", counv(itgr));
    
      return(0);
    }
    
    char counv(char itgr[]) {
    
      int index; /* index into integer string */
      int length = strlen(itgr); /* length of string */
      char bin[100]; /* string for binary number */
    
      /* starting from the last digit, concatonize the binary equivalent to the binary string */
      for (index = length - 1; index < 0; index--) {
    
        switch(itgr[index]) {
    
        case '1':
          (void)strcat(bin, "0001");
          break;
    
        case '2':
          (void)strcat(bin, "0010");
          break;
    
        case '3':
          (void)strcat(bin, "0011");
          break;
    
        case '4':
          (void)strcat(bin, "0100");
          break;
    
        case '5':
          (void)strcat(bin, "0101");
          break;
    
        case '6':
          (void)strcat(bin, "0110");
          break;
    
        case '7':
          (void)strcat(bin, "0111");
          break;
    
        case '8':
          (void)strcat(bin, "1000");
          break;
    
        case '9':
          (void)strcat(bin, "1001");
          break;
    
        default:
          /* do nothing */
          break;
    
        } /* closes switch statement */
      } /* closes for loop */
    
      return(bin);
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    - in 'main' you return an integer, but you havent declared main as such, use 'int main', keeping the return

    - not an error, but, (i think) you dont need to use all those voids in front of the printf, etc, functions, just ignore the int that it returns (dont do anything about it).

    "int_to_bin.c:87: warning: return makes integer from pointer without a cast"
    - you have declared your counv function to return a char, but you return 'bin' which is char[].

    "int_to_bin.c:87: warning: function returns address of local variable"
    - you create a local array in counv and then return the address of it. however, once the counv function finishes execution, all its local variables are destroyed and the address you received isnt of any use.
    - an option is to change the prototype to: void counv(char itgr[], char dest[]). then in main you create your 'destination' (the 'bin' in counv) array, and pass it. then inside counv you write directly to that array, changing the actual array declared in main.

    hope it helps

    edit:
    Code:
      for (index = length - 1; index < 0; index--) {
    this is saying to loop while index is < 0, i think you mean > 0. i havent looked at the rest of your algorithm in this for loop, but i assume once you get all the previous errors, and this error, sorted out you can do some testing and get it working properly.
    Last edited by nadroj; 08-24-2007 at 11:09 AM.

  3. #3
    bpf
    bpf is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    7
    actually after looking at the loop, i wasn't paying attention to what it was doing. i had it reading the string from left to right which isn't correct obviously

    got it working, thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > char conv(); /* function prototype */
    This is a function declaration, not a prototype. To be a prototype, it needs to have a parameter list (even if it is just void) to enable the compiler to check parameters.

    > (void)sscanf(line, "%s", &itgr);
    The & is wrong in this context. itgr is a char array, so simply saying itgr gets you the char * pointer which the %s format expects.

    > (void)printf("%c\n", counv(itgr));
    Better check the spelling with the prototype.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    - in 'main' you return an integer, but you havent declared main as such, use 'int main', keeping the return
    That code uses the implicit int rule. It's an old style of C from K&R; C89 supported it, but it was deprecated. Basically, the rule is that if no return value is specified, int is assumed. But new code shouldn't use this rule; C++ doesn't support it, and I'm pretty sure that C99 doesn't either.

    It's often combined with the old way of declaring parameters, so that you'll see function like this:
    Code:
    main(argc, argv)
    int argc;
    char *argv[];
    {
        /* ... */
    }
    Since the keyword void didn't used to exist, the implicit int rule was often used where void functions are used now.

    - not an error, but, (i think) you dont need to use all those voids in front of the printf, etc, functions, just ignore the int that it returns (dont do anything about it).
    Some programs like lint complain if you discard any values at all, including the return value of printf(), etc. So casting these return values to (void) is to statisfy lint. I don't like it, myself. Lint just makes your job harder in this case.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging bomb program
    By Mike_Smith in forum C Programming
    Replies: 3
    Last Post: 10-25-2010, 12:41 PM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. Replies: 7
    Last Post: 08-19-2007, 08:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21