使用结构我需要存储2个学生的信息并显示他们的结果

时间:2013-11-13 20:50:14

标签: c

#include<stdio.h>
struct student{
char name[80];
char subject
char country;

};

int main(){
struct student s[10];
int i;
printf("Enter the information of the students:\n");
for(i=0;i<4;++i)
{
printf("\nEnter name of the student: ");
scanf("%s",&s[i].name);
printf("\nEnter the subject of the student: ");
scanf("%s",&s[i].subject);
printf("\nEnter name of the student country: ");
scanf("%s",&s[i].country);
}
printf("\n showing the input of student information: \n");
for(i=0;i<10;++i)
{
printf("\nName: \n");
puts(s[i].name);
printf("\nMajor: \n",s[i].subject);
printf("\nCountry: \n",s[i].country);
}
return 0;
}

***当我试图显示结果时,它没有显示主题和国家。你能告诉我我的编码中有什么问题吗?

3 个答案:

答案 0 :(得分:1)

是否未显示主题和国家/或仅显示第一个字母?

我不熟悉C,但我建议你改变

char variableName 

char variableName[size]

正如你的名字,但你没有国家和主题。我不确定这是不是你的问题,但可能是,我相信char variableName只存储用户输入的一个字符。

答案 1 :(得分:0)

您需要提供转化模式,char%c

printf("\nMajor: %c\n",s[i].subject);
printf("\nCountry: %c\n",s[i].country);

另外

scanf("%s",&s[i].name); 

不正确,应该是

scanf("%s", s[i].name); // s[i].name is already an array

用于读取char,您也需要传递正确的转换模式

scanf("%c", &s[i].subject);
scanf("%c", &s[i].country);

答案 2 :(得分:0)

您的struct应该是这样的:

struct student{
char name[80];
char subject[80];
char country[80];
};