gpt4 book ai didi

c - 函数不从链接列表中读取

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

我有以下代码,可以在其中添加节点并打印链接列表中的所有当前节点。当我尝试使用查询功能插入 ID 排名并仅打印那些我没有得到任何结果并且与“修改”相同的情况时,我输入了 ID 和要更改的金额,但什么也没有。

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

struct employeeData {
int EMP_ID;
char name[20];
int dept;
int rank;
int salary;

struct employeeData* next;
};

void initializeList(struct employeeData** List);
void add(struct employeeData** List);
void Delete(struct employeeData** List);
void modify(struct employeeData** List);
void query(struct employeeData** List);
void print(struct employeeData** List);

int main() {
struct employeeData* myList = NULL;
int inputNUM, myListInput;
// initializeList(myList);
while (inputNUM != 0) {
printf("Please select an option 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\n");
printf("Enter 0 to stop \n\n");

printf("Input: ");
scanf("%d", &inputNUM);

printf("\n");

if (inputNUM == 1) {
add(&myList);
}
if (inputNUM == 2) {
Delete(&myList);
}
if (inputNUM == 3) {
modify(&myList);
}
if (inputNUM == 4) {
query(&myList);
}
if (inputNUM == 5) {
print(&myList);
}
}

return 0;
}

/*
void initializeList(struct employeeData **List)

{

FILE *ifp;
ifp = fopen("empInfo.txt","r");

struct employeeData *Temp = NULL;
Temp = (struct employeeData*)malloc(sizeof(struct employeeData));

while (ifp != NULL)
{
fscanf(ifp, "%d %s %d %d %d", &Temp->EMP_ID, Temp->name, &Temp->dept,
&Temp->rank, &Temp->salary);

Temp->next;
}

while (*List->next != NULL)
{
*List = *List->next;
}

*List-next = Temp;

fclose(ifp);

}

*/

void add(struct employeeData** List) {
struct employeeData* Temp = NULL;
Temp = (struct employeeData*)malloc(sizeof(struct employeeData));

printf("Please enter the information of the employee: ");
scanf("%d %s %d %d %d", &Temp->EMP_ID, Temp->name, &Temp->dept, &Temp->rank,
&Temp->salary);

printf("\n");
Temp->next = NULL;
if (*List == NULL) {
*List = Temp;
}

else {
struct employeeData* last = *List;

while (last->next != NULL) {
last = last->next;
}

last->next = Temp;
}
}

void Delete(struct employeeData** List) {
int TEMPID = NULL;

struct employeeData* Temp = *List;

printf("Please enter the EMP_ID for the employee that you would like to be "
"deleted: ");
scanf("%d", &TEMPID);

while (Temp->EMP_ID != TEMPID) {
if (Temp->next->EMP_ID == TEMPID) {
break;
} else {
Temp = Temp->next;
}
}

if (Temp->next->next == NULL) {
free(Temp->next);
*List = Temp;
} else {
struct employeeData* connect = Temp;
struct employeeData* del = Temp->next;
connect = Temp->next->next;
Temp->next = connect;
free(del);
*List = Temp;
}
}

void modify(struct employeeData** List) {
int TEMPID = 0, NewSAL = 0;

struct employeeData* Temp = *List;

printf("Please enter the EMP_ID and new salary for the employee that you "
"would like to modify: ");
scanf("%d", &TEMPID, &NewSAL);

while (Temp->EMP_ID != TEMPID) {
Temp = Temp->next;
}

Temp->salary = NewSAL;
*List = Temp;
}

void query(struct employeeData** List) {
int TEMPID = NULL;

printf("Please provide the rank that you would like to query: ");
scanf("%d", &TEMPID);

struct employeeData* Temp = *List;
while (Temp != NULL) {
if (Temp->EMP_ID == TEMPID) {
printf("%s \n", Temp->name);
Temp = Temp->next;
} else {
Temp = Temp->next;
}
}

printf("\n");
}

void print(struct employeeData** List) {
struct employeeData* Temp = *List;
while (Temp != NULL) {
printf("%d %s %d %d %d \n", Temp->EMP_ID, Temp->name, Temp->dept,
Temp->rank, Temp->salary);

Temp = Temp->next;
}

printf("\n");
}

最佳答案

这是 GCC 4.9.1 使用 -Wall 报告的错误:

E:\test.cpp: In function 'int main()':
E:\test.cpp:23:19: warning: unused variable 'myListInput' [-Wunused-variable]
int inputNUM, myListInput;
^
E:\test.cpp: In function 'void modify(employeeData**)':
E:\test.cpp:153:33: warning: too many arguments for format [-Wformat-extra-args]
scanf("%d", &TEMPID, &NewSAL);

第二个报告如果关于行:scanf("%d", &TEMPID, &NewSAL);scanf 函数需要两个格式说明符,因为两个用于读取 TEMPIDNewSAL 的参数。

更改者:scanf("%d %d", &TEMPID, &NewSAL);

这是 Clang 3.5 使用 -Wall 报告的错误:

E:\test.cpp:23:19: warning: unused variable 'myListInput' [-Wunused-variable]
int inputNUM, myListInput;
^
E:\test.cpp:23:9: warning: variable 'inputNUM' is used uninitialized whenever function 'main' is called [-Wsometimes-uninitialized]
int inputNUM, myListInput;
~~~~^~~~~~~~
E:\test.cpp:25:12: note: uninitialized use occurs here
while (inputNUM != 0) {
^~~~~~~~
E:\test.cpp:23:17: note: initialize the variable 'inputNUM' to silence this warning
int inputNUM, myListInput;
^
= 0

您在 while 条件中使用未初始化的变量(这是未定义的行为,任何事情都可能发生):while (inputNUM != 0) {,在这种情况下,解决方案是初始化变量 (例如:值为 != 0),用于进入 while 循环。

建议始终以可用的最高级别警告进行编译。

关于c - 函数不从链接列表中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26004750/

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