musthavehair 2021-06-21 12:00 采纳率: 40%
浏览 15
已采纳

stu的数据怎么都录不到文件里去 不知道问题出在哪里

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define N 2             
struct student
{
    char nam[10];
    int age;
}stu;

struct prob
{
    int n;
    int m;
    int answer;
    int Answer;
    char ch;
}prob[N];

Input(struct student stu,FILE *fp) 
{
    printf("Please Input Your Name:");
    getchar();
    gets(stu.nam);
    printf("Please Input Your Age:");
    scanf("%d",&stu.age);
    printf("\n");
}

Print()
{
    printf("\t题目\t\t标准答案\t你的答案\n");
    printf("\n");
    int i;
    for(i=0;i<N;i++)
    {
        printf("      %d %c %d =\t\t  %d\t\t   %d\n",prob[i].n,prob[i].ch,prob[i].m,prob[i].answer,prob[i].Answer);
        printf("\n");
    }
}

In(FILE *fp,int sum)
{
    int i;
    if( ( fp = fopen("E:\\studinf.txt", "w") ) == NULL) 
    {  
        printf("File open error!\n");
        exit(0);
    }
    fprintf(fp,"姓名:          %s\t年龄:  %d\n",stu.nam,stu.age);
    fprintf(fp,"\n");
    fprintf(fp,"您的测验成绩为:    %d\n",sum);
    fprintf(fp,"\n");
    fprintf(fp,"\t题目\t\t标准答案\t你的答案\n");
    for(i=0;i<N;i++)
    {
        fprintf(fp,"\t%d %c %d =\t\t  %d\t   %d\n",prob[i].n,prob[i].ch,prob[i].m,prob[i].answer,prob[i].Answer);
        printf("\n");
    }
    fclose(fp);
}

int problem()
{
    int i,n,m;
    char a[4]={'+','-','*','/'};
    char c;
    int sum=0;
    for(i=0;i<N;i++)
    {
        n=1+rand()%100;
        m=1+rand()%100;
        prob[i].n=n;
        prob[i].m=m;
        printf("第%d道题目: ",i+1);
        if(n%2==0)
        {
            c=a[0];
            prob[i].answer=m+n;
        }
        else if(n%3==0)
        {
            c=a[1];
            prob[i].answer=m-n;
        }
        else if(n%7==0)
        {
            c=a[2];
            prob[i].answer=m*n;
        }
        else
        {
            c=a[3];
            prob[i].answer=n/m;
        }
        prob[i].ch=c;
        printf("%d %c %d =   \n",n,c,m);
        printf("请输入你的答案:");
        scanf("%d",&prob[i].Answer);    
        if(prob[i].Answer==prob[i].answer)
            sum+=10;
    }
    return(sum);
}
            
int main()
{
    srand((unsigned)time(NULL));
    printf("\t数学测验软件\t\n");
    int size=sizeof(stu);
    int c;
    int sum;
    int i,n,m;
    FILE *fp;
    do{
        printf("\n"); 
        printf("菜单:\t1:输入学生信息  2:开始测验  3:查看测验结果  0:保存并退出\n");
        printf("请输入你的选择:");
        scanf("%d",&c);
        printf("\n");
        printf("\n");
        switch(c){
            case 1:
                Input(stu,fp);
                break;
            case 2:
                printf("开始测验 :\t(提示:除法题目答案只保留整数部分)\n");
                printf("\n");
                sum=problem();
                break;
            case 3:
                printf("本次测试您的分数为:    %d\n",sum);
                printf("\n");
                printf("\t\t以下是您的的测试结果:\n");
                printf("\n");
                printf("\n");
                Print();
                break;
            case 0:
                In(fp,sum);
                break;
        }
    }while(c!=0);
    
    return 0;
}

 

 

这是我文件里的数据:

姓名:              年龄:  0

您的测验成绩为:    0

    题目        标准答案    你的答案
    95 / 15 =          6       15
    17 / 89 =          0       12
 

  • 写回答

1条回答 默认 最新

  • qfl_sdu 2021-06-21 12:39
    关注

    void Input(struct student stu,FILE *fp) 函数不需要添加参数,加上参数以后,Input函数里的stu就是局部变量了,改成:

    void Input() ;//你fp也没用,所以一起删掉

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c