我想基本上以姓名,姓氏,年龄,薪水为标题的表格形式打印姓名,姓氏,年龄和薪水的结果。目前,它的输出方式是错误的。非常感谢我应做的任何提示!
#include <stdio.h>
int main()
{
char name[20], surname[20];
int age, salary;
for (int i = 1; i <= 2; ++i) {
printf("Insert name:");
scanf("%s", name);
printf("Insert surname:");
scanf("%s", surname);
printf("Insert age:");
scanf("%d", &age);
printf("Insert salary:");
scanf("%d", &salary);
for (int i = 1; i <= 2; ++i) {
printf("%s\t%s\t%d\t%d\t", name, surname, age, salary);
}
}
printf("Name\tSurname\tAge\tSalary\t\n");
printf("%s\t%s\t%d\t%d\t", name, surname, age, salary);
return 0;
}
答案 0 :(得分:0)
我想基本上打印出姓名,姓氏,年龄和 彼此之间的薪水,以标题名称表的形式出现, 姓氏,年龄,工资。
在打印结果之前只需使用printf("Name\tSurname\tAge\tSalary\t\n");
,并在新行中使用'\n'
将结果打印出来。
printf("Name\tSurname\tAge\tSalary\t\n");
for (int i = 1; i <= 2; ++i) {
printf("%s\t%s\t%d\t%d\t\n", name, surname, age, salary);}
编辑:
使用Array of Structure
存储多个员工的数据。
首先存储数据,然后使用loop
打印结果。
这是工作代码:
#include <stdio.h>
struct employee{ //<-----define the structure
int age, salary;
char name[20];
char surname[20];
};
int main()
{
struct employee em[3]; //<----create an array of employee
for (int i = 1; i <= 2; ++i) { //<----take the data from user
printf("Insert name:");
scanf("%s", em[i-1].name);
printf("Insert surname:");
scanf("%s", em[i-1].surname);
printf("Insert age:");
scanf("%d", &em[i-1].age);
printf("Insert salary:");
scanf("%d", &em[i-1].salary);
}
printf("Name\tSurname\tAge\tSalary\t\n");
for (int i = 1; i <= 2; ++i) { //<---print the data
printf("%s\t%s\t%d\t%d\t\n", em[i-1].name, em[i-1].surname, em[i-1].age, em[i-1].salary);
}
return 0;
}