帮忙大家帮我看一个简单的C程序问题... 我是大1的学C的菜鸟啊``

时间:2008-05-12 13:50:14   来源:论坛整理  作者:  编辑:chinaitzhe
#include <stdio.h>
struct student
{
int num;
char name[20];
float score[3];
};
void main()
{
void print(struct student);
struct student stu[5];
int i,j;
for(i=0;i <5;i )
scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],
stu[i].score[2]);
for(j=0;j <5;j )
print(stu[j]);
}
void print(struct student stu)
{
printf("%d,%s,%f,%f,%f\n",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]);
}

这个编译没错误... 运行结果有错误...
输入 1,a,11,11,11
2,s,22,22,22
3,d,33,33,33
4,f,44,44,44
5,g,55,55,55
时本来结果是要 同输入一样的..
但是结果是
1,a,11,11,11,-107374176.000000,-107374176.000000,-107374176.000000
2,s,22,22,22,-107374176.000000,-107374176.000000,-107374176.000000
3,d,33,33,33,-107374176.000000,-107374176.000000,-107374176.000000
4,f,44,44,44,-107374176.000000,-107374176.000000,-107374176.000000
5,g,55,55,55,-107374176.000000,-107374176.000000,-107374176.000000
不知道后面出来的是什么意思? 到底错误出在哪呢?
请高手们解决下`` 谢谢``

网友回复:那几个
stu[i].score[0],stu[i].score[1],stu[i].score[2]
应该改成
&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
表示在那几个变量的地址写数据,不然就会有不可意料的错误
网友回复:scanf中的要改,printf中的不用哈
网友回复:scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],
stu[i].score[2]);
stu[i].name有问题

最终根本没有得到stu.score[0],stu.score[1],stu.score[2]得值
网友回复:C语言输入时一定记住加地址符号&
网友回复:scanf 中%s与其它的分开写
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include  <stdio.h> 

struct student 

{ 

   int num; 

   char name[20]; 

   float score[3]; 

}; 

void main() 

{ 

    void print(struct student); 

    struct student stu[5]; 

    int i,j; 

    for(i=0;i <1;i  ) 

    {

        scanf("%d",&stu[i].num);//这样写 以空格隔开

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

        scanf("%f",&stu[i].score[0]);

        scanf("%f",&stu[i].score[1]);

        scanf("%f",&stu[i].score[2]); 

        fflush(stdin);

    }

    for(j=0;j <1;j  ) 

        print(stu[j]); 

} 

void print(struct student stu) 

{ 

   printf("%d,%s,%f,%f,%f\n",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]); 

} 




网友回复:scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],
stu[i].score[2]);
这句漏了&啊~~
网友回复:我想问一下6楼,scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1], &stu[i].score[2]); 为什么不行,我刚试的,总是直接跳过去不执行,呵呵,谢谢了
网友回复:
引用 8 楼 nkqhyf 的回复:
我想问一下6楼,scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1], &stu[i].score[2]); 为什么不行,我刚试的,总是直接跳过去不执行,呵呵,谢谢了

你输入的时候要用1,ABC,3.5....这样的格式,中间一定要有逗号
网友回复:好象还是搞不出来的啊?
那个name那里不用取地址符号了吧?
字符串...
网友回复:6楼的那个没错的...
请问一下为什么要把他们分开呢? 
 写在一起不行的?  
   还有就是那个  
fflush(stdin);
  看不懂是什么意思? 
 解释下啊?
网友回复:写一起没有关系的,
fflush(stdin)是刷新输入流,防止缓冲区满了.
网友回复:楼主多看书
网友回复:
引用 1 楼 baihacker 的回复:
那几个
stu[i].score[0],stu[i].score[1],stu[i].score[2]
应该改成
&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
表示在那几个变量的地址写数据,不然就会有不可意料的错误

网友回复:学习学习 !!!!!


网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





解决方法1:

#include  <stdio.h> 

struct student 

{ 

   int num; 

   char name[20]; 

   float score[3]; 

}; 

void main() 

{ 

    void print(struct student); 

    struct student stu[2]; 

    int i,j; 

    for(i=0;i <2;i  ) 

    {   printf("\n输入姓名:");

        scanf("%s",stu[i].name);//要害是这个姓名的字符串的问题,

                 //你把它放在中间,当你输入姓名完成时,再输入后面的成绩,那成绩是存放在name里面的,

                 //因为字符串没有满,所以,最后的score数组里根本没有值,就输入乱七八糟的东西了

        printf("\n输入编号,成绩:");

        scanf("%d,%f,%f,%f",&stu[i].num,&stu[i].score[0],&stu[i].score[1], &stu[i].score[2]);    

        fflush(stdin);

    }

    for(j=0;j <2;j  ) 

    {

        

        print(stu[j]); 

    }

} 

void print(struct student stu) 

{ 

   printf("%d,%s,%f,%f,%f\n",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]); 

} 

/////////////////////////////////

帮你调试了下,OK了.

////////////////////////////////

输入姓名:zhang



输入编号,成绩:1,22,33,44



输入姓名:li



输入编号,成绩:2,44,64,72

1,zhang,22.000000,33.000000,44.000000

2,li,44.000000,64.000000,72.000000

Press any key to continue






C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





//解决方法2:

#include  <stdio.h> 

struct student 

{ 

   int num; 

   char name[20]; 

   float score[3]; 

}; 

void main() 

{ 

    void print(struct student); 

    struct student stu[2]; 

    int i,j; 

    for(i=0;i <2;i  ) 

    { 

        printf("\n输入编号,成绩,姓名:");

            //scanf里还可以这样写,记得输入时,每个变量间要加逗号隔开

        scanf("%d,%f,%f,%f,%s",&stu[i].num,&stu[i].score[0],

            &stu[i].score[1], &stu[i].score[2],stu[i].name);    

        fflush(stdin);

    }

    for(j=0;j <2;j  ) 

    {

        

        print(stu[j]); 

    }

} 

void print(struct student stu) 

{ 

   printf("%d,%s,%f,%f,%f\n",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]); 

} 

//////////////////////////////////////

输入编号,成绩,姓名:1,33,44,55,li



输入编号,成绩,姓名:2,33,44,55,zhao

1,li,33.000000,44.000000,55.000000

2,zhao,33.000000,44.000000,55.000000

Press any key to continue









网友回复:scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],
stu[i].score[2]);
改成:
scanf("%d,%s,%f,%f,%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],
&stu[i].score[2]);
输入的时候一定别忘记加地址符号&
网友回复:16楼高手
网友回复:看的我头疼~呵呵
网友回复:
引用 18 楼 jillnicky 的回复:
16楼高手

网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1], 

    stu[i].score[2]); 




网友回复:哇哈哈`` 知道了`` 谢谢大家啊`
` 就是那个 fflush(stdin)是刷新输入流,防止缓冲区满了 书上貌似没有``
恩`` 以后多来学习学习``
关键字:帮忙,一个,简单,程序,问题,
上一篇:天天一贴

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面