error: expected identifier or ‘(’ before ‘{’ token {

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aydro
    New Member
    • Sep 2015
    • 1

    error: expected identifier or ‘(’ before ‘{’ token {

    Very new to C and wondering what do i need to do to finish off three expected identifier errors

    // char2int
    // Converts from a character to an integer digit
    // if character '0'..'9' convert to 0..9
    // else if character 'A'..'F' convert to 10..15
    // else convert to -1
    int char2int(char digit);
    {
    //int char2IntResult = digit;
    //char char2CharResult = digit;
    //if the digit is 9 - 0, result variable will subtract '0' from digit
    int if (digit < 'A' & digit >= 0)
    int char2IntResult = digit - '0';
    return &char2IntResult ;
    //if the digit is 10-15 result variable will subtract '0' from digit'
    char if (digit >= 'A' & digit <= 'F')
    char char2CharResult = digit - '0';
    return &char2CharResul t;
    int else
    return -1;
    }

    // int2char
    // Converts from an integer digit to a character
    // if integer 0..9 convert to '0'..'9'
    // else if integer 10..15 convert to 'A'..'F'
    // else convert to 'X'
    char int2char(int digit);
    {
    //int int2CharDigit = digit;
    //char int2CharChar = digit;
    //If digit is 0-9, result variable will convert to 0-9
    int if (digit < 'A' & digit >= 0)
    int int2CharDigit = digit + '0';
    return &int2CharDig it;
    // Else if digit is 10-15, result variable will convert to A-F
    char if (digit < 'F' & digit >= 'A')
    char int2CharChar = digit + '0';
    return &int2CharCha r;
    char else
    return 'X';
    }

    // Convert integer to string in specified base and print
    // 2 <= base <= 16
    void int2ascii(int value, int base);
    {
    int (return -1);
    }


    // Convert string in specified base to integer and print
    // 2 <= base <= 16
    void ascii2int(char *ascii, int base);
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    int char2int(char digit); <<<---!!!
    {
    	//int char2IntResult = digit;
    	//
    shows a semi-colon before the opening brace. That semi-colon makes the line a function prototype. However, here you are actually writing the function so you don't need a prototype.



    You are consistent on all three functions and you get three errors.

    You only use the prototype when you make a function call in a source file that does not have the function definition in it.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Some observations...
      Code:
      int char2int(char digit);
       {
       //int char2IntResult = digit;
       //char char2CharResult = digit;
       //if the digit is 9 - 0, result variable will subtract '0' from digit
       int if (digit < 'A' & digit >= 0)
       int char2IntResult = digit - '0';
       return &char2IntResult;
       //if the digit is 10-15 result variable will subtract '0' from digit'
       char if (digit >= 'A' & digit <= 'F')
       char char2CharResult = digit - '0';
       return &char2CharResult; 
      int else 
      return -1;
       }
      1. Line 1: @weaknessforcat s already told you what is wrong with this line.
      2. Lines 6, 10, and 13: the first word on each of these lines (int/char) is spurious and will trigger compiler errors.
      3. Lines 6 and 10: the logical AND operator is &&, not &.
      4. Line 6: this test (digit < 'A' && digit >= 0) does not tell you if the digit is 9 - 0.
      5. Lines 7 and 8: you want both of these lines to be executed if and only if the line 6 test is true. You need to enclose these two lines in curly brackets to group them together.
      6. Line 8: You want to return an integer value not the address of the variable holding the integer value.
      7. Line 11: I don't see why this variable is a char rather than an int.
      8. Line 9 and 11: this formula (digit - '0') will not convert letters 'A'-'F' to 10-15.
      9. Lines 11 and 12: you want both of these lines to be executed if and only if the line 10 test is true. You need to enclose these two lines in curly brackets to group them together.
      10. Line 12: You want to return an integer value not the address of the variable holding the integer value.
      11. Line 14: you want this line to execute if neither of the tests on lines 6 or 10 are true. You need to change line 10 from if to else if.
      12. Is there any reason why you don't want to also convert lowercase 'a'-'f' to 10-15?
      13. Your algorithm for recognizing digit characters '0'-'9' and for converting them to 0-9 assumes that the character codes for digits are contiguous and increase from 0 to 9. The language standard does indeed mandate this encoding so your assumption is safe.
      14. Your algorithm for recognizing letter characters 'A'-'F' and for converting them to 10-15 assumes that the character codes for these letters are contiguous and increase from A to F. The language standard does not mandate this encoding so the assumption is not safe (although it is true for the most commonly used character encoding, ASCII). There may be compiler implementations where your program won't work.

      It is easier to refer to your source code when there are line numbers. You get the line numbers by enclosing your code in CODE tags.

      Comment

      Working...