求解答可以这样scanf给赋值吗?if下面的判断行不通
求解答可以这样scanf给赋值吗?if下面的判断行不通 怎样输入都是 两个数相等怎样解决下面判断大小,哪里错了
# include <stdio.h>
struct student
{
int num;
char name;
float score;
}student1,student2;
int main(void)
{
struct student1;
struct student2;
scanf("%d,%s,%f",&student1.num, &student1.name, &student1.score);
scanf("%d,%s,%f",&student2.num, &student2.name, &student2.score);
if(student1.score > student2.score)
{
printf("%s%f\n",student1.name, student1.score);
}
else if(student1.score < student2.score)
{
printf("%s%f\n",student2.name, student2.score);
}
else if(student1.score == student2.score)
{
printf("他们两个人的成绩相等");
}
}
我运行的结果 怎样输入都是他们两人成绩相等
struct student1;
struct student2;
这两句 你是要闹哪样?? 我也是初学者,目测输入他们的分数printf("")放在scanf(""),前面试试,要先输入分数才能获取。不知道对不对:big 牡丹花下死做鬼 发表于 2014-9-8 12:26
这两句 你是要闹哪样??
是不是 应该struct studentst变量 怎么搞得,struct student这个是你定义的结构体类型的类型名,不是变量名,要用你定义的结构体类型来定义一个变量应该是 struct student 变量名={。。。。。。。}; 不会啊 牡丹花下死做鬼 发表于 2014-9-8 12:26
这两句 你是要闹哪样??
哟哟 切克闹 牡丹又来论坛大显神威了,。。。,,。,。, # include <stdio.h>
struct student
{
int num;
char name;
float score;
};
void main()
{
struct student student1;
struct student student2;
scanf("%d %s %f",&student1.num,&student1.name,&student1.score);
scanf("%d %s %f",&student2.num,&student2.name,&student2.score);
if(student1.score > student2.score)
{
printf("%s%f\n",student1.name, student1.score);
}
else if(student1.score < student2.score)
{
printf("%s%f\n",student2.name, student2.score);
}
else if(student1.score == student2.score)
{
printf("他们两个人的成绩相等");
}
}
注意scanf赋值处
bobo_dreamer 发表于 2014-9-11 11:40
# include
struct student
{
那若我想用逗号作为分隔符,怎么处理,谢谢! 你可以将student.score与student.name输入顺序替换。
scanf("%d, %f,%s",&student1.num,&student1.score,&student1.name);
scanf("%d,%f,%s",&student2.num,&student2.score,&student2.name);
出现你那种情况是因为计算机无法识别你输入的name和score之间的逗号是分隔符还是字符。
页:
[1]