我目前正在尝试从struct函数创建一个程序,如下所示。我不想直接从程序中打印一行,而是修改程序以打印一条消息,要求用户输入关于某个人的信息,并按结构指示显示它。
而不是做"struct p“和插入信息,我希望它被输入为扫描。
#include <stdio.h>
typedef struct {
int day, month, year;
} DATA;
typedef struct person {
char name[100];
int age;
float salary;
DATA birth;
} PERSON;
void Mostrar(struct person x){
printf("name: %s\n",x.name);
printf("age: %d\n",x.age);
printf("Salário: %f\n",x.salary);
printf("Dt.birth: %d/%d/%d\n",x.birth.day, x.birth.month, x.birth.year);
}
int main(){
struct person p = {"Carlos", 23, 12345, {23,5,1954}};
Mostrar(p);
}谢谢你们的帮助!
发布于 2022-11-17 15:52:31
只需使用scanf
PERSON getPerson(void)
{
PERSON person;
if(!fgets(person.name, sizeof(person.name), stdin)) { /* error handling */} //you can also remove \n at the end if you need to
if(scanf("%d %f", &person.age, &person.salary) != 2) { /* error handling */}
if(scanf("%d %d %d", &person.birth.day, &person.birth.month, &person.birth.year) != 3) { /* error handling */}
return person;
}https://stackoverflow.com/questions/74478309
复制相似问题