鱼C论坛

 找回密码
 立即注册
查看: 2562|回复: 5

c语言输出问题

[复制链接]
发表于 2019-8-19 08:57:46 | 显示全部楼层 |阅读模式

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

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

x

  1. //成绩录入模块
  2. create()
  3. {
  4.         FILE *fp;
  5.         STUDENT stud;
  6.         int i, stu_c;
  7.         if ((fp = fopen("cj1.dat", "wb+")) == NULL)
  8.         {
  9.                 printf("file open error!\n");
  10.                 exit(1);
  11.         }
  12.         system("cls");
  13.         printf("please input students total counts:");
  14.         scanf("%d%*c", &stu_c);
  15.         printf("input %d students info\n", stu_c);
  16.         for (i = 1;i <= stu_c;i++)
  17.         {
  18.                 printf("input NO%d student information:\n", i);
  19.                 printf("Number:");
  20.                 //gets(stud.num);
  21.                 scanf("%[^\n]%*c",stud.num);
  22.                 printf("Name:");
  23.                 //gets(stud.name);
  24.                 scanf("%[^\n]%*c",stud.name);
  25.                 printf("3 scores(separato:):");
  26.                 scanf("%f,%f,%f%*c", &stud.score[0], &stud.score[1], &stud.score[2]);
  27.                 stud.score[3] = (stud.score[0] + stud.score[1] + stud.score[2]) / 3;
  28.                 fwrite(&stud, sizeof(STUDENT), 1, fp);
  29.         }
  30.         fclose(fp);
  31.         return 0;
  32. }
复制代码


调试结果
please input students total counts:3
input 3 students info
input NO1 student information:
Number:001
Name:aaa
3 scores(separato:):30 50 70
input NO2 student information:
Number:Name:002
3 scores(separato:):40 10 20
input NO3 student information:
Number:Name:003
3 scores(separato:):



第二次和第三次输出的时候number跟name一起出来了,只能接收一个值,这个地方第一次通过了不知道怎么处理

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-19 21:40:37 | 显示全部楼层
没人吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-19 22:32:09 | 显示全部楼层
建议把代码贴完整
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2019-8-20 22:23:27 | 显示全部楼层
         scanf("%[^\n]%*c",stud.num);  //这一行代码 ————你的结构体中定义的num是 int  还是int * ,如果是 int      ;建议改成:

scanf("%[^\n]%*c",&stud.num);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-22 07:06:14 | 显示全部楼层
人造人 发表于 2019-8-19 22:32
建议把代码贴完整

只是这个模块问题。已经解决了,输入的时候没打逗号。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-22 07:08:37 | 显示全部楼层
Richard149 发表于 2019-8-20 22:23
scanf("%[^\n]%*c",stud.num);  //这一行代码 ————你的结构体中定义的num是 int  还是int *  ...

好的 已经解决了,输入的时候没打逗号
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-16 14:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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