Thread: Errors in this function, dont know why

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    31

    Errors in this function, dont know why

    I need help, I wrote this piece of function and try to compile with gcc this code and it gives me errors like this, I am new at c language still trying to get used to it, any help or suggestions are much appreciated . thanks in advance

    1- error: ‘YES’ undeclared (first use in this function) inhex = YES;
    2- note: each undeclared identifier is reported only once for each function it appears in
    error: ‘NO’ undeclared (first use in this function)
    inhex = NO;




    Code:
    int xtoi (char hexstring[]){
    
    
      int hexdigit, i, inhex, n;
      i=0;
    
    
      if(hexstring[i] == '0') {
        ++i;
        if(hexstring[i] == 'x' || hexstring[i] == 'X') {
    
    
          ++i;
        }
     }
    
    
      n = 0;
      inhex = YES;
      for(; inhex == YES; ++i) {
        if(hexstring[i] >= '0' && hexstring[i] <= '9') {
          hexdigit = hexstring[i] - '0';
        }else if(hexstring[i] >= 'a' && hexstring[i] <= 'f') {
          hexdigit = hexstring[i] - 'a' + 10;
        }else if(hexstring[i] >= 'A' && hexstring[i] <= 'F') {
          hexdigit = hexstring[i] -'A' + 10;
        }else{
          inhex = NO;
    
    
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by erald23
    1- error: ‘YES’ undeclared (first use in this function) inhex = YES;
    2- note: each undeclared identifier is reported only once for each function it appears in
    error: ‘NO’ undeclared (first use in this function)
    inhex = NO;
    That's exactly what it says: you didn't declare the identifiers named YES and NO. Perhaps you should just use 1 and 0 instead, or true and false if you #include <stdbool.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    I did as you suggested I put DEFINE YES 1, DEFINE NO 0, and the errors are fixed, but I'm having another error in my void function like this

    expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
    void itox (char hexstring[], int n)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That means that you have a syntax error. If you want me to elaborate, you would need to elaborate with the relevant code. I suggest posting the smallest and simplest program that you expect should compile but which demonstrates this error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    /* trying to convert integer to hexa */
    even other suggestion to shrink down it would be appreciated I feel like is too messy like this


    Code:
    #include <stdio.h>
    #include <string.h>
    #include "xbits.h"
    #define YES 1
    #define  NO  0
    
    
    void itox (char hexstring[], int n)
      printf("in itox, processing %d\n", n);
    {
      char hexdigit[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
      int index = 0;
      int remain;
    
    
      while(n > 0) {
      remain = n%16;
      n/=16;
      hexstring[index++] = hexdigit[remain];
    }
    
    
      hexstring[index] = '\0';
      printf("decimal: %d\thexstring %s\n", n, hexstring);
    
    
    }
    Last edited by erald23; 09-25-2018 at 10:29 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you wrote the first printf call before the opening brace for the definition of the itox function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    I did delete printf and now is duplicated the error.


    Code:
    expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
    void itox (char hexstring[], int n)
    ^
    error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    {
    ^
    error: expected ‘{’ at end of input
    }
    [


    QUOTE=laserlight;1280074]Notice that you wrote the first printf call before the opening brace for the definition of the itox function.[/QUOTE]

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by erald23
    I did delete printf and now is duplicated the error.
    Look, it is good that you post the error messages, but you have to post the code too. If you made a change in response to help, post the updated code along with the new error message. Make another change in response to help, post the newly updated code with the latest error message.

    I deleted the printf and your unnecessary includes and #define statements and I find that there is not even a warning:
    Code:
    #include <stdio.h>
    
    
    void itox (char hexstring[], int n)
    {
      char hexdigit[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
      int index = 0;
      int remain;
    
    
      while(n > 0) {
      remain = n%16;
      n/=16;
      hexstring[index++] = hexdigit[remain];
    }
    
    
      hexstring[index] = '\0';
      printf("decimal: %d\thexstring %s\n", n, hexstring);
    
    
    }
    
    int main(void)
    {
        return 0;
    }
    So what gives? Obviously, you made a mistake, but we already knew that, and even now I cannot tell you what mistake you made because I don't know what's your current code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    ok so this program has two functions the first one"void itox" converts interger to hexastring and second one 'int xtoi" converts hexstring to its own value.

    The whole code I have is this and I'm having problems compiling

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "xbits.h"
    #define YES 1
    #define  NO  0
    
    
    
    
    
    
     /* function converts int n to a hexstring  */
    
    
    void itox (char hexstring[], int n)
    
    
    {
      char hexdigit[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E\
    ','F'};
      int index = 0;
      int remain;
      printf("in itox, processing %d\n", n);
      while(n > 0) {
      remain = n%16;
      n/=16;
      hexstring[index++] = hexdigit[remain];
    
    
    }
    
    
      hexstring[index] = '\0';
      printf("decimal: %d\thexstring %s\n", n, hexstring);
    
    
    }
    
    
    
    
     /* converts a hexstring to its int   */
    int xtoi (char hexstring[])
    {
    
    
      int hexdigit, i, inhex, n;
      i=0;
    
    
      if(hexstring[i] == '0') {
        ++i;
        if(hexstring[i] == 'x' || hexstring[i] == 'X') {
    Code:
    int xtoi (char hexstring[]){
    
    
      int hexdigit, i, inhex, n;
      i=0;
    
    
      if(hexstring[i] == '0') {
        ++i;
        if(hexstring[i] == 'x' || hexstring[i] == 'X') {
    
    
          ++i;
        }
     }
    
    
      n = 0;
      inhex = YES;
      for(; inhex == YES; ++i) {
        if(hexstring[i] >= '0' && hexstring[i] <= '9') {
          hexdigit = hexstring[i] - '0';
        }else if(hexstring[i] >= 'a' && hexstring[i] <= 'f') {
          hexdigit = hexstring[i] - 'a' + 10;
        }else if(hexstring[i] >= 'A' && hexstring[i] <= 'F') {
          hexdigit = hexstring[i] -'A' + 10;
        }else{
          inhex = NO;
    
    
        }
        if(inhex == YES) {
          n = 16 * n+ hexdigit;
        }
    
    
      }
    
    return n;
    }
    errors are

    Code:
    error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’ void itox (char hexstring[], int n)
     ^
    error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
     {
     ^
     error: expected ‘{’ at end of input
     }
    Last edited by erald23; 09-25-2018 at 11:20 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this all in a single file? It looks like you made a typo error such that you ended up with two code blocks. Combining them:
    Code:
    #include <stdio.h>
    #include <string.h>
    /*#include "xbits.h"*/
    
    #define YES 1
    #define  NO  0
    
    
    /* function converts int n to a hexstring  */
    void itox (char hexstring[], int n)
    {
        char hexdigit[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        int index = 0;
        int remain;
        printf("in itox, processing %d\n", n);
        while(n > 0) {
            remain = n%16;
            n/=16;
            hexstring[index++] = hexdigit[remain];
        }
    
        hexstring[index] = '\0';
        printf("decimal: %d\thexstring %s\n", n, hexstring);
    }
    
    
    int xtoi (char hexstring[])
    {
        int hexdigit, i, inhex, n;
        i=0;
    
        if(hexstring[i] == '0') {
            ++i;
            if(hexstring[i] == 'x' || hexstring[i] == 'X') {
                ++i;
            }
        }
    
        n = 0;
        inhex = YES;
        for(; inhex == YES; ++i) {
            if(hexstring[i] >= '0' && hexstring[i] <= '9') {
                hexdigit = hexstring[i] - '0';
            }else if(hexstring[i] >= 'a' && hexstring[i] <= 'f') {
                hexdigit = hexstring[i] - 'a' + 10;
            }else if(hexstring[i] >= 'A' && hexstring[i] <= 'F') {
                hexdigit = hexstring[i] -'A' + 10;
            }else{
                inhex = NO;
            }
            if(inhex == YES) {
                n = 16 * n+ hexdigit;
            }
        }
    
        return n;
    }
    
    
    int main(void)
    {
        return 0;
    }
    I still don't see any compile error. Notice that I commented out the include of "xbits.h" though, which you apparently don't need. Perhaps the error lies there, but you shouldn't be including header files that you don't need.

    Also, notice that I indented your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    sorry I should've said that we are using a driver in a separate file and execute them together on emacs to combine them, I'm still working on it and is like this
    Code:
    #include <stdio.h>#include <string.h>
    #include "xbits.h"
    
    
    #define ENOUGH_SPACE (sizeof(int) * 2 + 1)
    #define LINELEN      1024
    
    
    int main(void)
    {
      char hexstring[ENOUGH_SPACE], line[LINELEN];
      int m, n;
    
    
      /*loop                                                                                                                                 
       * use fgets(line, LINELEN, stdin) to read from keyboard                                                                               
       * stop the loop if EOF                                                                                                                
       * use sscanf(line, "%d", &n) to convert input to int n                                                                                
       */
      while  (( fgets(line, LINELEN, stdin) != NULL ) {
    
    
         scanf(line, "%d", %n);
               printf("%d\n" %n);
    
    
      itox(hexstring, n);
      m = xtoi(hexstring);
      printf("\ninput decimal int   : %d\n", n);
      printf("hex representation  : %s\n", hexstring);
      printf("reconverted decimal : %d\n\n", m);
    
    
        }
      return 0;

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by erald23
    sorry I should've said that we are using a driver in a separate file and execute them together on emacs to combine them, I'm still working on it and is like this
    You desperately need to format your code properly, especially regarding indentation. Here's what you posted with proper indentation:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "xbits.h"
    
    
    #define ENOUGH_SPACE (sizeof(int) * 2 + 1)
    #define LINELEN      1024
    
    
    int main(void)
    {
        char hexstring[ENOUGH_SPACE], line[LINELEN];
        int m, n;
    
    
        /*loop
         * use fgets(line, LINELEN, stdin) to read from keyboard
         * stop the loop if EOF
         * use sscanf(line, "%d", &n) to convert input to int n
         */
        while  (( fgets(line, LINELEN, stdin) != NULL ) {
            scanf(line, "%d", %n);
            printf("%d\n" %n);
    
            itox(hexstring, n);
            m = xtoi(hexstring);
            printf("\ninput decimal int   : %d\n", n);
            printf("hex representation  : %s\n", hexstring);
            printf("reconverted decimal : %d\n\n", m);
        }
    
        return 0;
    Can you spot the mistake now?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C function.. I dont understand
    By nihoe in forum C Programming
    Replies: 1
    Last Post: 03-15-2017, 03:41 AM
  2. Replies: 5
    Last Post: 03-12-2009, 04:13 PM
  3. Replies: 16
    Last Post: 02-20-2009, 12:29 AM
  4. i dont understand this loop and function
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 03-20-2008, 11:36 PM
  5. Some errors and warnings i dont understand
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 10-18-2002, 11:16 AM

Tags for this Thread