这是一个准备简单学生标记列表的程序:
#include <stdio.h>
#include <conio.h>
#define SIZE 50
struct mark {
char name[50];
float marks[5];
};
int main() {
int sn, subn, i, j;
printf ("\n Enter Number of Students : ");
scanf ("%d",&sn);
printf ("\n");
struct mark n[SIZE];
for (i = 1; i < sn + 1; ++i){
printf (" Enter Name of Student %d : ",i);
scanf ("%s",&n[i - 1].name);
for (j = 1; j < 6; ++j){
printf (" Enter Marks of Subject %d : ",j);
scanf ("%f",&n[i - 1].marks[j - 1]);
}
}
printf ("\n\n ------------------------------------------------------------------- \n\n");
printf (" Student Name \t\t Sub 1\t Sub 2\t Sub 3\t Sub 4\t Sub 5");
printf ("\n --------------------------------------------------------------------\n");
for (i = 0; i < sn; ++i) {
printf ("\n %s \t\t ",n[i].name);
for (j = 0; j < subn; ++j){
printf ("%d \t",n[i].marks[j]); /* Problem with this line. Prints integers like -24611 etc.*/
}
}
getch();
return 0;
}
当我到达标记的行时,程序会打印一些数字,如-241563等。
出了什么问题?这是结构的东西吗?首先,当打印标记时,它会打印5个0。然后打印像-241563这样的整数。请帮忙。
答案 0 :(得分:0)
printf
中的格式字符串与参数列表
变化:
printf("%d \t", n[i].marks[j]);
到
printf("%f \t", n[i].marks[j]);