gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:39:48 25 4
gpt4 key购买 nike

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

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|错误:“当前”之前的预期声明说明符|

这是我到目前为止的代码:

#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;
}

最佳答案

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

应该是:

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

顺便说一句:此函数在 printf 指令处存在不一致:由于工资是 double ,因此 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 的函数中的格式之间存在空格。

关于c - 我的链接列表中的打印功能不断出现错误,我不知道这些错误告诉我什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com