Thread: find parse error on line 24 reading from a file using structures

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    83

    find parse error on line 24 reading from a file using structures

    im trying to read employee information from a file using structures but im having problems with getting the file to open and read in the information
    Code:
    // Write a programt that inputs (at most 50) records from a sequential file
    #include <stdio.h>
    #include <stdlib.h>
    struct employee // employee structure definition
    {
            char last_name[15];
            char first_name[15];
            unsigned month;
            unsigned day;
            unsigned year;
            float annual_salary;
            char dept_code;
            double phone_number[12];
    };
    
    
    
    int main(void)
    {
            FILE *employeePtr; // file pointer to employee document
    
            int result;// used to test whether fread read any bytes
    
            struct employee = {Beast, Savory, 05,12,1995,350000.00,b,382-747-6139};   // : parse error before `=' token
    
    
            if ((employeePtr=fopen("employee.dat","rb"))==NULL)// fopen opens the file; exits if the file does not open
    
    {
            puts("file could not be opened\n");
    }
            else
    {
            printf("file did worked\n");// prints statement if the file did open and worked
    }
    }
    Last edited by ashley1nonly; 04-04-2015 at 02:37 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    Oh, and please indent your code properly. Your main function looks like a mess so it is hard to tell at a glance if your braces match.
    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
    Feb 2015
    Posts
    83
    when I compile it says
    : parse error before `=' token
    im not sure whats wrong with it
    also updated the post with indents

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I formatted your code:
    Code:
    // Write a programt that inputs (at most 50) records from a sequential file
    #include <stdio.h>
    #include <stdlib.h>
    
    struct employee // employee structure definition
    {
        char last_name[15];
        char first_name[15];
        unsigned month;
        unsigned day;
        unsigned year;
        float annual_salary;
        char dept_code;
        double phone_number[12];
    };
    
    int main(void)
    {
        FILE *employeePtr; // file pointer to employee document
        int result; // used to test whether fread read any bytes
        // employee data with default information:
        struct employee = {Beast, Savory, 05, 12, 1995, 350000.00, b, 382 - 747 - 6139};
        if ((employeePtr=fopen("employee.dat","rb"))==NULL)
        {
            puts("file could not be opened\n");
        }
        else
        {
            // prints statement if the file did open and worked:
            printf("file did worked\n");
        }
    }
    Then compiled it:
    Code:
    test.c: In function ‘main’:
    test.c:22:21: error: expected identifier or ‘(’ before ‘=’ token
         struct employee = {Beast, Savory, 05, 12, 1995, 350000.00, b, 382 - 747 - 6139};
                         ^
    The error that you got was due to a failure to provide a name for the variable. Furthermore, you need to quote your string and character literals:
    Code:
    struct employee employee_data = {"Beast", "Savory", 05, 12, 1995, 350000.00, 'b', "382-747-6139"};
    I am assuming that you will change this:
    Code:
    double phone_number[12];
    To this:
    Code:
    double phone_number[13];
    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
    May 2010
    Posts
    4,632
    I am assuming that you will change this:
    Shouldn't that actually be changed to: ?
    Code:
    char phone_number[13];

    Jim

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jimblumberg View Post
    Shouldn't that actually be changed to: ?
    Code:
    char phone_number[13];
    Yes.
    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
    Feb 2015
    Posts
    83
    errors I got
    jammel.c: In function `int main()':
    jammel.c:24: cannot convert `const char*' to `double' in initialization
    jammel.c:24: excess elements in aggregate initializer
    Code:
    // Write a programt that inputs (at most 50) records from a sequential file
    #include <stdio.h>
    #include <stdlib.h>
    struct employee // employee structure definition
    {
            char last_name[15];
            char first_name[15];
            unsigned month;
            unsigned day;
            unsigned year;
            float annual_salary;
            char dept_code;
            double phone_number[13];
    };
    
    
    int main(void)
    {
            FILE *employeePtr; // file pointer to employee documen
      
            int result;// used to test whether fread read any bytes
            
            struct employee employee_data = {"Beast", "Savory", 05, 12, 1995, 350000.00, 'b', "382-747-6139"};           
            
            if ((employeePtr=fopen("employee.dat","r"))==NULL)// fopen opens the file; exits if the file does not open
            {
    
            puts("file could not be opened\n");
    }
            else
    {
            printf("file did worked\n");// prints statement if the file did open and worked
    }
    }
    Last edited by ashley1nonly; 04-04-2015 at 02:53 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashley1nonly
    errors I got
    jammel.c: In function `int main()':
    jammel.c:24: cannot convert `const char*' to `double' in initialization
    jammel.c:24: excess elements in aggregate initializer
    As jimblumberg noted in post #5, I made in a typographical error in post #4 by forgetting to change double to char. For your part though, you should not take my code as if I were an oracle. Rather, faced with the compile error, you should have investigated it and fixed it.
    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
    Feb 2015
    Posts
    83
    why would I char the phone number

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashley1nonly
    why would I char the phone number
    Look at the code in post #1. For the phone number, you wrote: 382-747-6139. This can be simplified to: -6504. Is the phone number supposed to be -6504.0?
    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
    Feb 2015
    Posts
    83
    isn't char only used for letters like names and things like that but not numbers like the phone number

  12. #12
    Registered User
    Join Date
    Feb 2015
    Posts
    83
    thanks very much for the help!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashley1nonly
    isn't char only used for letters like names and things like that but not numbers like the phone number
    An array of char can be used to store a null terminated string. A phone number, despite the word "number" as part of the name, is a string. Even if you remove the dashes, which you might do to try and normalise the phone number into a common format, it is still conceptually a string rather than an integer or a double (although you arguably could store most phone numbers in an unsigned long long after stripping them of non-digit characters).
    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

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by laserlight View Post
    An array of char can be used to store a null terminated string. A phone number, despite the word "number" as part of the name, is a string. Even if you remove the dashes, which you might do to try and normalise the phone number into a common format, it is still conceptually a string rather than an integer or a double (although you arguably could store most phone numbers in an unsigned long long after stripping them of non-digit characters).
    This is an important point that bears repeating ... just because input data contains numbers does not necessarily mean it should be stored as a number.

    Telephone numbers are a good example. Yes, it's composed largely of numbers ... but can also contain other characters that are completely independent of mathematical operators.

    And in most cases, phone numbers are what they are ... they do not require mathematical manipulation of any sort.

    Considering they may or may not contain non-numeric characters, and they are values that do not require mathematical manipulation, data like phone numbers are usually better off expressed as strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. Parse error on line 28 and 36
    By jollykiddo in forum C Programming
    Replies: 4
    Last Post: 12-18-2011, 04:28 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. Parse error in .rc file.
    By Ranedhel in forum Windows Programming
    Replies: 1
    Last Post: 07-29-2003, 02:52 PM
  5. If there's a parse error, I can't find it
    By BlackCitan in forum C Programming
    Replies: 1
    Last Post: 02-05-2002, 03:49 PM