【问题标题】:I keep getting errors for my print funtion in my Linked List and I have no idea what these errors are telling me我的链接列表中的打印功能不断出现错误,我不知道这些错误告诉我什么
【发布时间】:2014-09-24 21:22:10
【问题描述】:

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

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

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

C:employeeDatabase.c||在函数'printlist'中:| |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 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;
}

【问题讨论】:

  • 在函数体的开头忘记{
  • 注意:如果您要保持固定的缓冲区长度,这:sizeof( char )*NAME_LENGTH 毫无意义。您也可以将struct employeeDatachar *name 成员声明为char name[NAME_LENGTH];,因为无论如何您都在动态分配结构。而current-&gt;salarydouble,通过%d 格式说明符发送printf 几乎可以确定不是你想要做什么。使用%f
  • 是的!就是这样!!我忘了开括号大声笑!现在我的删除功能不起作用
  • @user3739121 这将被称为“进度”。你必须真的想看它,但就是这样。如果你看得足够深入,你会在这段代码中发现很多个错误的东西。例如:while(!feof(data)) 是错误的(see here for why)main 中的name 分配大小不正确(基于sizeof(char*),应该是sizeof(char),即使这样也不是需要)。等等。有很多事情可以让你忙于解决这个问题。

标签: c struct linked-list


【解决方案1】:

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

应该是:

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

顺便说一句:这个函数在 printf 指令中存在不一致:因为薪水是双倍的 printf 格式不应该是 %d 而是例如 %lf(%f 也适用于 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 );

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

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

NB1:这里的 %lf 不能被 %f 替换 NB2:在类似 scanf 的函数中避免格式之间的空格。

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多