使用两个不同的函数打印身份证信息,应该在主函数中调用
print id card information using two different function and should be called in main function
我想使用两个不同的函数来打印身份证,一个用于来自用户的信息,第二个函数应该显示信息并且应该在主函数中调用。请指出我正在做的错误,因为它没有打印正确的值。
struct card
{
char name[20];
int clas;
char section[20];
int roll;
};
void info(struct card id)
{
printf("\n Name: ");
scanf(" %s", &id.name);
printf("\nClass: ");
scanf(" %d", &id.clas);
printf("\nSection: ");
scanf(" %s", &id.section);
printf("\nRoll: ");
scanf(" %d", &id.roll);
}
void displaydata(struct card id)
{
printf("\nName: %s", id.name);
printf("\nClass: %d", id.clas);
printf("\nSection: %s", id.section);
printf("\nRoll: %d", id.roll);
}
int main()
{
struct card id2[4];
int i;
for (i = 0; i < 3; i++)
{
info(id2[i]);
}
for (i = 0; i < 3; i++)
{
printf("------");
printf("student: %d", i + 1);
displaydata(id2[i]);
}
return 0;
}
这里是你的代码修改为使用两个函数的指针:
#include <stdio.h>
#include <stdlib.h>
struct card
{
char name[20];
int clas;
char section[20];
int roll;
};
void info(struct card *id)
{
printf("\n Name: ");
scanf(" %s", &id->name);
printf("\nClass: ");
scanf(" %d", &id->clas);
printf("\nSection: ");
scanf(" %s", &id->section);
printf("\nRoll: ");
scanf(" %d", &id->roll);
}
void displaydata(struct card *id)
{
printf("\nName: %s", id->name);
printf("\nClass: %d", id->clas);
printf("\nSection: %s", id->section);
printf("\nRoll: %d", id->roll);
}
int main()
{
struct card id2[4];
int i;
for (i = 0; i < (sizeof(id2) / sizeof(id2[0])); i++)
{
info(&id2[i]);
}
for (i = 0; i < (sizeof(id2) / sizeof(id2[0])); i++)
{
printf("------");
printf("student: %d", i + 1);
displaydata(&id2[i]);
}
return 0;
}
如您所见,我使用 sizeof
获取 id2
数组中的元素数。这比 reader 无法轻易找到意义的 magic 数字要好得多。
您还应该检查 scanf
是否成功。例如:
if (scanf(" %s", &id->name) != 1) {
fprintf(stderr, "Name not correctly input\n");
// more code to handle the error, as needed
}
我想使用两个不同的函数来打印身份证,一个用于来自用户的信息,第二个函数应该显示信息并且应该在主函数中调用。请指出我正在做的错误,因为它没有打印正确的值。
struct card
{
char name[20];
int clas;
char section[20];
int roll;
};
void info(struct card id)
{
printf("\n Name: ");
scanf(" %s", &id.name);
printf("\nClass: ");
scanf(" %d", &id.clas);
printf("\nSection: ");
scanf(" %s", &id.section);
printf("\nRoll: ");
scanf(" %d", &id.roll);
}
void displaydata(struct card id)
{
printf("\nName: %s", id.name);
printf("\nClass: %d", id.clas);
printf("\nSection: %s", id.section);
printf("\nRoll: %d", id.roll);
}
int main()
{
struct card id2[4];
int i;
for (i = 0; i < 3; i++)
{
info(id2[i]);
}
for (i = 0; i < 3; i++)
{
printf("------");
printf("student: %d", i + 1);
displaydata(id2[i]);
}
return 0;
}
这里是你的代码修改为使用两个函数的指针:
#include <stdio.h>
#include <stdlib.h>
struct card
{
char name[20];
int clas;
char section[20];
int roll;
};
void info(struct card *id)
{
printf("\n Name: ");
scanf(" %s", &id->name);
printf("\nClass: ");
scanf(" %d", &id->clas);
printf("\nSection: ");
scanf(" %s", &id->section);
printf("\nRoll: ");
scanf(" %d", &id->roll);
}
void displaydata(struct card *id)
{
printf("\nName: %s", id->name);
printf("\nClass: %d", id->clas);
printf("\nSection: %s", id->section);
printf("\nRoll: %d", id->roll);
}
int main()
{
struct card id2[4];
int i;
for (i = 0; i < (sizeof(id2) / sizeof(id2[0])); i++)
{
info(&id2[i]);
}
for (i = 0; i < (sizeof(id2) / sizeof(id2[0])); i++)
{
printf("------");
printf("student: %d", i + 1);
displaydata(&id2[i]);
}
return 0;
}
如您所见,我使用 sizeof
获取 id2
数组中的元素数。这比 reader 无法轻易找到意义的 magic 数字要好得多。
您还应该检查 scanf
是否成功。例如:
if (scanf(" %s", &id->name) != 1) {
fprintf(stderr, "Name not correctly input\n");
// more code to handle the error, as needed
}