0
#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;
}

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

4

3 回答 3

1

是不显示主题和国家还是只显示第一个字母?

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

char variableName 

char variableName[size]

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

于 2013-11-13T21:01:18.497 回答
0

struct应该是这样的:

struct student{
char name[80];
char subject[80];
char country[80];
};
于 2013-11-13T21:04:05.200 回答
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

要读取字符,您也需要传递正确的转换模式

scanf("%c", &s[i].subject);
scanf("%c", &s[i].country);
于 2013-11-13T20:53:34.390 回答