鱼C论坛

 找回密码
 立即注册
查看: 3969|回复: 4

关于C语言中的结构体的赋值问题

[复制链接]
发表于 2013-1-18 21:19:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <string.h>
struct student
{
        int num[9];
        char name[20];
        char sex;
        float jsj_score;
                float yw_score;
        float sx_score;
        float yy_score;
};

void main ()
{
    int i;
        struct student d[10];
        for (i = 0; i < 10; i++)
        {
                fflush(stdin);
                        printf ("学号:");
                scanf ("%d",d[i].num);
                printf ("名字:");
                scanf ("%s",d[i].name);
                printf ("性别:");
                scanf ("%s",&d[i].sex);
                printf ("计算机成绩:");
                scanf ("%f",&d[i].jsj_score);
                printf ("语文成绩:");
                scanf ("%f",&d[i].yw_score);       
                printf ("数学成绩:");
                scanf ("%f",&d[i].sx_score);
                        printf ("英语成绩:");
                scanf ("%f",&d[i].yy_score);

        }

}
这样的话输入到计算机成绩的时候就会出错。。不知道为什么,
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-1-18 21:28:43 | 显示全部楼层
额  人呢?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-19 00:00:59 | 显示全部楼层
学习看看 等待大侠指导
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-19 01:59:50 | 显示全部楼层
  1. //都用注释给你解释了,看一下就知道了。
复制代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. struct student
  4. {
  5.         char num[9]; // 你用int类型数组来放一个数,我觉得逻辑不对
  6.         char name[20];
  7.         char sex;
  8.         float jsj_score;
  9.         float yw_score;
  10.         float sx_score;
  11.         float yy_score;
  12. };

  13. void main ()
  14. {
  15.     int i;
  16.         struct student d[10];
  17.         for (i = 0; i < 10; i++)
  18.         {
  19.                 fflush(stdin);
  20.                 printf ("学号:");
  21.                 scanf ("%s",d[i].num);
  22.                 printf ("名字:");
  23.                 scanf ("%s",d[i].name);
  24.                 printf ("性别:");
  25.                 scanf ("%s",&d[i].sex);
  26.                 printf ("计算机成绩:");
  27.                                 d[i].jsj_score=0; //scanf对浮点数变量取值有个bug,需要先对浮点数进行赋值。
  28.                 scanf ("%f",&d[i].jsj_score);
  29.                 printf ("语文成绩:");
  30.                 scanf ("%f",&d[i].yw_score);        
  31.                 printf ("数学成绩:");
  32.                 scanf ("%f",&d[i].sx_score);
  33.                         printf ("英语成绩:");
  34.                 scanf ("%f",&d[i].yy_score);

  35.         }

  36. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-1-19 13:09:33 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-9 00:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表