c-我在我的链接列表中不断收到打印功能的错误,我不知道这些错误在告诉我什么



这是我需要输入的示例文本文件:

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

我得到的错误是一个声明错误,但我确实声明了我的指针:

C:employeeDatabase.C||在函数"打印列表"中:||82|错误:在"current"之前需要声明说明符|

这是我迄今为止的代码:

#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 namen" );
    employee* current;
    current = head;
    while ( current != NULL )
    {
            if( current->rank == submenu )
            {
                printf( "%sn", 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_IDt EMP NAMEtt DEPTtt RANKtt SALARY " );
    while ( current != NULL )
    {
        printf( "n%dt %s tt %dtt %dtt %dn", 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 menun" );
        printf( "1 to add a new employeen" );
        printf( "2 to delete an employeen" );
        printf( "3 to modify an employee recordn" );
        printf( "4 to query employees by rankn" );
        printf( "5 to print all employee informationn" );
        printf( "0 to exit the programn" );
        scanf( "%d", &menu_choice );
        if( menu_choice == 1 )
        {
            printf( "Choice 1n" );
            menu_choice = -1;
        }
        if ( menu_choice == 2 )
        {
            printf( "Choice 2n" );
            menu_choice = -1;
        }
        if ( menu_choice == 3 )
        {
            printf( "Choice 3n" );
            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;
}

您似乎忘记了函数printlist 的开花括号

应该是:

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

BTW:在printf指令中,这个函数存在不一致性:由于工资是两倍,printf格式不应该是%d,但例如%lf(%f也适用于printf)

printf( "n%dt %s tt %dtt %dtt %lfn", current->EMP_ID, current->name, current->dept, current->rank, current->salary );

同样,同样的不一致出现在fscanf中(在函数main中),应该是:

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

NB1:此处%lf不能替换为%fNB2:在类似scanf的函数中避免格式之间的空格。

相关内容

  • 没有找到相关文章

最新更新