gpt4 book ai didi

c - C 中的打印输出有问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:42 25 4
gpt4 key购买 nike

我这里的程序打印不正确。这是输入:

有多少员工? 2

输入员工 1 信息:月/日/年、年龄、高度、姓名:3/21/34, 43, 3.4, hdsfgdf

输入员工 2 信息:月/日/年、年龄、高度、姓名:4/44/44, 44, 6.2, dfgtesas

这是输出:

员工 1 信息:0/-1081689528/134514548, 16564212, 0.0, ��

员工 2 信息:0/1/14608664, -1217230008, 0.0, ��������

我唯一的猜测是我没有正确填充数组,或者我打印的是地址而不是数据。我这样假设对吗?任何一点建议都会有所帮助。太感谢了!

我的代码在 3 个文件中。

这是主要的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "personal.c"

LIST *start, *end;

int main(void)
{
int i, numEmp;

PERSON person;

start=end=NULL;
printf("How many employees? ");
scanf("%d", &numEmp);
PERSON employees[numEmp];

for (i = 0; i < numEmp; i++)
{
printf("Enter employee %d info: month/day/year, age, height, name:\n", i+1);
scanf("%d/%d/%d,%d,%f,%s", &person.bday.month, &person.bday.day,
&person.bday.year, &person.age, &person.height, person.name);
add(&start, &end, person);

}
for (i = 0; i < numEmp; i++)
{
printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1, employees[i].bday.month, employees[i].bday.day, employees[i].bday.year, employees[i].age, employees[i].height, employees[i].name);

delete(&start, &end);
}

这是具有以下结构的列表:

#ifndef LIST_H_ /* to prevent re-definitions */
#define LIST_H_ /* that cause errors */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct DATE
{
int month;
int day;
int year;
} DATE;

typedef struct PERSON
{
char name[41];
int age;
float height;
DATE bday;

} PERSON;

typedef struct list
{
PERSON data;
struct list *next;
} LIST;

#endif

这就是我所有方法所在的地方:

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


int delete (LIST **head, LIST **tail){
LIST *temp;

if (*head == NULL)
return -1;

PERSON retVal = (*head)->data;

if(*head==*tail){
free(*head);
*head=*tail=NULL;
}
else{
temp=(*head)->next;
free(*head);
*head=temp;
}

//return retVal;
}

void add(LIST **head, LIST **tail, PERSON data){
if(*tail==NULL){
*head=*tail=(LIST *) malloc(sizeof(LIST));
(*head)->data=data;//use arrow when struct is pointer. Use . if have direct access to struct
(*head)->next=NULL;
}
else{
(*tail)->next= (LIST *) malloc(sizeof(LIST));
*tail=(*tail)->next;
(*tail)->data=data;
(*tail)->next=NULL;
}
}

最佳答案

printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1, 
employees[i].bday.month, employees[i].bday.day, employees[i].bday.year,
employees[i].age, employees[i].height, employees[i].name);

您正在尝试打印从未初始化的 employees 数组。

关于c - C 中的打印输出有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331092/

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