-2

Here is the sample text file I need for input:

12   JackSprat   2     1    65000
13   HumptyDumpty  5   3    30000
17   BoPeep  2       3      30000
20   BoyBlue    3    2      58000
0

The error Im getting is a declaration error but I do declare my pointer:

C:employeeDatabase.c||In function 'printlist':| |82|error: expected declaration specifiers before 'current'|

This is the code I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME_LENGTH 20

typedef struct employeeData                   //employee struct declaration
{
    int EMP_ID;
    char* name;
    int dept;
    int rank;
    double salary;

    struct employeeData *next;
}employee;

employee* initializeList( int EMP_ID, char* name, int dept, int rank, double salary ) //initialize LL function for employees
{
    employee* tmp = ( employee* )( malloc(sizeof(struct employeeData ) ) );
    tmp->name = ( char* )malloc( sizeof( char )*NAME_LENGTH );

    strcpy( tmp->name, name );
    tmp->salary = salary;
    tmp->EMP_ID = EMP_ID;
    tmp->dept = dept;
    tmp->rank = rank;
    tmp->next = NULL;

    return tmp;
}

employee* insertEmp( employee* head, employee* tmp )            //function to insert employees into the LL
{
    employee* current = NULL;
    current = head;
    if( current == NULL || strcmp( current->name, tmp->name ) > 0)  // checking order
    {
            tmp->next = current;
            return tmp;
    }
    else
    {
            while( current->next != NULL && strcmp( current->next->name, tmp->name ) < 0 ) //changing order
            {

                    current = current->next;
            }
    }
            tmp->next = current->next;
            current->next = tmp;
            return head;

}

void query( employee *head, int submenu )  //query by employee rank function
{
    printf( "\nEMP name\n" );
    employee* current;
    current = head;
    while ( current != NULL )
    {

            if( current->rank == submenu )
            {
                printf( "%s\n", current->name );
                if( current->next == NULL )
                {
                break;
                }
                current = current->next;
            }

    current = current->next;
    }
    return;
}

void printlist( employee* head )    //print result function HERE IS WHERE MY PROBLEM IS

    employee* current;
    current = head;
    printf( "EMP_ID\t EMP NAME\t\t DEPT\t\t RANK\t\t SALARY " );
    while ( current != NULL )
    {
        printf( "\n%d\t %s \t\t %d\t\t %d\t\t %d\n", current->EMP_ID, current->name, current->dept, current->rank, current->salary );
        current = current->next;
    }
    return;
}

int main( void )
{
    FILE* data = fopen( "empInfo.txt", "r" );                 //read in emp data here
    int EMP_ID, dept, rank, menu_choice = -1, submenu;
    double salary;
    char* name = ( char* )malloc( sizeof( char* )*NAME_LENGTH );

    employee *head = NULL;

    while( !feof( data ) )  //initialize the list by calling its function
    {
        fscanf( data, "%d %s %d %d %d", &EMP_ID, name, &dept, &rank, &salary );
            {
                if ( EMP_ID == 0 )
                    break;
            }
            employee* hold = initializeList( EMP_ID, name, dept, rank, salary );
            head = insertEmp( head, hold );
    }

    while ( menu_choice != 0 )  //Menu declaration and listed choices
    {
        printf( "\nPlease select an action from the following menu\n" );
        printf( "1 to add a new employee\n" );
        printf( "2 to delete an employee\n" );
        printf( "3 to modify an employee record\n" );
        printf( "4 to query employees by rank\n" );
        printf( "5 to print all employee information\n" );
        printf( "0 to exit the program\n" );
        scanf( "%d", &menu_choice );

        if( menu_choice == 1 )
        {
            printf( "Choice 1\n" );
            menu_choice = -1;
        }

        if ( menu_choice == 2 )
        {
            printf( "Choice 2\n" );
            menu_choice = -1;
        }

        if ( menu_choice == 3 )
        {
            printf( "Choice 3\n" );
            menu_choice = -1;
        }
        if ( menu_choice == 4 )
        {
            printf( "Please provide the rank of the employee you would like to query.\n" );
            scanf( "%d", &submenu );
            query( head, submenu );
            menu_choice = -1;
        }
        if ( menu_choice == 5 )
        {
            printlist( head );
            menu_choice = -1;
        }
    }
    fclose( data );

    while (getchar () != '\n')   //I use this instead of system ("PAUSE")
    getchar ();
    return 0;
}
  • 3
    forget `{` at begin of function body. – BLUEPIXY Sep 24 '14 at 21:25
  • Note: this: `sizeof( char )*NAME_LENGTH` is pointless if you're going to keep the fixed buffer length. You may as well just have the `char *name` member of `struct employeeData` be declared as `char name[NAME_LENGTH];`, since you're dynamically allocating the structure anyway. And `current->salary` is a `double`, sending it `printf` via a `%d` format specifier is nearly assured *not* what you wanted to do. Use `%f`. – WhozCraig Sep 24 '14 at 21:26
  • YES! thats it!! I forgot the open bracket lol! Now my delete function isnt working – user3739121 Sep 24 '14 at 21:30
  • @user3739121 that would be called "progress". You gotta *really* wanna see it, but that's what it is. You're going to find *many* things in this code that are wrong if you look deep enough. For example: `while(!feof(data))` is wrong [**(see here for why)**](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). The `name` allocation in `main` is sized incorrectly (based on `sizeof(char*)`, should be `sizeof(char)`, and even that isn't *needed*). Etc. There's plenty to keep you busy in fixing this. – WhozCraig Sep 24 '14 at 21:31

1 Answers1

0

It seems you forgotten an open curly bracket for function printlist

should be:

void printlist( employee* head )    //print result function HERE IS WHERE MY PROBLEM IS
{ // <-- DON'T FORGET ME !

BTW: there is an inconsistency in this function, at the printf instruction: since salary is a double the printf format should not be %d but for instance %lf (%f also works for printf)

printf( "\n%d\t %s \t\t %d\t\t %d\t\t %lf\n", current->EMP_ID, current->name, current->dept, current->rank, current->salary );

Similarly, the same inconsistency appears in the fscanf (in function main), should be:

fscanf( data, "%d%s%d%d%lf", &EMP_ID, name, &dept, &rank, &salary );

NB1: here %lf cannot be replaced by %f NB2: avoid the space between the formats in scanf-like functions.

didou
  • 692
  • 3
  • 7