qq_39492111
2018-04-04 14:46
采纳率: 100%
浏览 1.3k
已采纳

编译没有报错但是运行出错,求解决

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>

void choice();
void read();
void add();
void browse();
void refer();
void amend();
void result();

struct cost
{
    char num[20];//编号
    char type[20];//类型 
    char name[20];//经办人姓名
    char reason[200];//原因
    double amount;//收入金额
    char remarks[200];//备注
    double balances;//余额
}all[100];

int a;
double b;

int main()
{
    char b;
    do
    {
        choice();
        printf("是否要继续使用?(yes/no)\n");
        scanf("%c", &b);
    } while (b!='n');
    printf("\n");
    printf("谢谢使用\n");
    return 0;
}

void choice()
{
    char b;
    printf("********************************************\n");
    printf("             班费收支管理系统\n");
    printf("请选择您所需要的服务,输入对应的序号:\n");
    printf("             1.添加班费收支信息\n");
    printf("            2.浏览所有班费收支信息\n");
    printf("             3.查询班费收支信息\n");
    printf("             4.修改班费收支信息\n");
    printf("             5.直接查询余额信息\n");
    printf("********************************************\n");
    scanf("%c", &b);
    while (b !='1'&&b != '2'&&b != '3'&&b != '4')
    {
        printf("请重新输入有效数字\n");
        scanf("%c", &b);
    }
    switch (b)
    {
    case '1': add(); break;
    case '2': browse(); break;
    case '3': refer(); break;
    case '4': amend(); break;
    case '5': result(); break;
    }
}

void read()
{
    FILE * fp, *NOM;

    if ((fp=fopen("Class_expense_information","rb")) ==NULL)
    {
        a = 0;
        fp = fopen("Class_expense_information", "wb");
        fwrite(&a, sizeof(int), 1, fp);
        fclose(fp);
    }
}

void add()
{

    FILE * fp, *NOM;

    printf("请输入收支编号:\n");
    scanf("%s", all[a].num);
    printf("请输入经办人姓名:\n");
    scanf("%s", all[a].name);
    printf("请输入原因:\n");
    scanf("%s", all[a].reason);
    printf("请输入收支类型(in/out):\n");
    scanf("%s", all[a].type);
    printf("请输入金额:\n");
    scanf("%lf", all[a].amount);
    printf("请输入备注:\n");
    scanf("%s", all[a].remarks);
    switch (a)
    {
    case 0: fp = fopen("data", "wb");   break;
    default: fp = fopen("data", "ab");  break;
    }
    fwrite(&all[a], sizeof(struct cost), 1, fp);
    fclose(fp);

    a++;

    NOM = fopen("Class_expense_information", "wb");
    fwrite(&a, sizeof(int), 1, NOM);
    fclose(NOM);
}

void browse()
{
    FILE *fp = NULL;
    int c;
    int sign = 1;
    double sum;

    fp = fopen("data", "rb");

    for (c = 0; c < a; c++)
    {
        fread(&all[c], sizeof(struct cost), 1, fp);
        printf("收支编号:%s\n", &all[c].num);
        printf("收支类型:%s\n", &all[c].type);
        printf("经办人姓名:%s\n", &all[c].name);
        printf("原因:%s\n", &all[c].reason);
        printf("金额:%lf\n", &all[c].amount);
        printf("备注:%s\n", &all[c].remarks);
        if (strcmp(all[c].type, "in") == 0) sign = 1;
        else if (strcmp(all[c].type, "out") == 0) sign = -1;
        sum = sum + (all[c].amount *sign);
        all[c].balances = sum;
        printf("余额:%lf\n", &all[c].balances);
    }

}

void amend()                                                //  按不同字段查询班费信息 
{
    FILE *fp = NULL;
    int x, i, k, result[100];
    char key_world[20];

begin3:
    k = 0;
    printf("\n");
    printf("1.按班费收支编号查询\n");
    printf("2.按班费收支类型查询\n");
    printf("3.按班费经办人姓名查询\n");
    printf("4.按班费收支原因查询\n");
    printf("5.返回上一级\n");
    printf("请输入你的选择:");

begin2:
    scanf("%d", &x);

    printf("请输入要查询的关键字:");

    scanf("%s", key_world);
    fp = fopen("data", "rb");

    switch (x)
    {
    case 1:
        for (i = 0; i < a; i++)
        {
            fread(&all[i], sizeof(struct cost), 1, fp);

            if (strcmp(all[i].num, key_world) == 0)
            {
                result[k] = i;
                k++;
            }
        }
        break;
    case 2:
        for (i = 0; i < a; i++)
        {
            fread(&all[i], sizeof(struct cost), 1, fp);

            if (strcmp(all[i].type, key_world) == 0)
            {
                result[k] = i;
                k++;
            }
        }
        break;
    case 3:
        for (i = 0; i < a; i++)
        {
            fread(&all[i], sizeof(struct cost), 1, fp);

            if (strcmp(all[i].name, key_world) == 0)
            {
                result[k] = i;
                k++;
            }
        }
        break;
    case 4:
        for (i = 0; i < a; i++)
        {
            fread(&all[i], sizeof(struct cost), 1, fp);

            if (strcmp(all[i].reason, key_world) == 0)
            {
                result[k] = i;
                k++;
            }
        }
        break;
    case 5:
        return;
        break;
    default:
        printf("输入错误!请重新输入:");
        fflush(stdin);
        fclose(fp);
        goto begin2;
        break;
    }

    if (k == 0)
    {
        printf("没有查到相关的内容!\n");
        fclose(fp);
        goto begin3;
    }

    else
    {
        printf("\n\n查找到%d条记录:\n\n", k);

        for (i = 0; i < k; i++)
        {
            fread(&all[result[i]], sizeof(struct cost), 1, fp);
            printf("收支编号:%s\n", &all[result[i]].num);
            printf("收支类型:%s\n", &all[result[i]].type);
            printf("经办人姓名:%s\n", &all[result[i]].name);
            printf("原因:%s\n", &all[result[i]].reason);
            printf("金额:%lf\n", &all[result[i]].amount);
            printf("备注:%s\n", &all[result[i]].remarks);
            printf("当时余额:%lf\n", &all[result[i]].balances);
        }

        fclose(fp);
    }
}

void refer()                                              //    修改班费信息 
{
    FILE * fp = NULL;
    int i, k, x, result[100];
    char key_world[20];

    printf("请输入你要修改的班费信息的编号:");
    scanf("%s", key_world);
    k = 0;
    fp = fopen("data", "rb");

    for (i = 0; i < a; i++)
    {
        fread(&all[i], sizeof(struct cost), 1, fp);

        if (strcmp(all[i].num, key_world) == 0)
        {
            result[k] = i;
            k++;
        }
    }

    fclose(fp);

    if (k == 0)
    {
        printf("没有查到相关的内容!\n");
        return;
    }

    else
    {
        printf("\n\n查找到%d条记录:\n\n", k);
        fp = fopen("data", "rb");

        for (i = 0; i < k; i++)
        {
            printf("记录%d:\n", i + 1);
            printf("收支编号:%s\n", &all[result[i]].num);
            printf("收支类型:%s\n", &all[result[i]].type);
            printf("经办人姓名:%s\n", &all[result[i]].name);
            printf("原因:%s\n", &all[result[i]].reason);
            printf("金额:%lf\n", &all[result[i]].amount);
            printf("备注:%s\n", &all[result[i]].remarks);
            printf("当时余额:%lf\n", &all[result[i]].balances);
        }

        close(fp);

    begin4:
        printf("请输入需要修改的记录的记录序号:");
        scanf("%d", &x);

        if (x > k)
        {
            printf("输入错误!");
            fflush(stdin);
            goto begin4;
        }

        else
        {
            printf("请输入班费编号:");
            scanf("%s", all[result[x - 1]].num);
            printf("请输入收支类型:");
            scanf("%s", all[result[x - 1]].type);
            printf("请输入经办人姓名:");
            scanf("%s", all[result[x - 1]].name);
            printf("请输入原因:");
            scanf("%s", all[result[x - 1]].reason);
            printf("请输入金额:");
            scanf("%lf", all[result[x - 1]].amount);
            printf("请输入备注:");
            scanf("%s", all[result[x - 1]].remarks);


            fp = fopen("data", "rb+");

            for (i = 0; i < a; i++)
            {
                if (i != result[x])
                {
                    fwrite(&all[i], sizeof(struct cost), 1, fp);
                }

                else
                {
                    fwrite(&all[result[x]], sizeof(struct cost), 1, fp);
                }
            }

            fclose(fp);
        }
    }
}

void result()
{
    FILE *fp = NULL;
    int c;
    int sign = 1;
    double sum;

    fp = fopen("data", "rb");

    for (c = 0; c < a; c++)
    {
        fread(&all[c], sizeof(struct cost), 1, fp);
        if (strcmp(all[c].type, "in") == 0) sign = 1;
        else if (strcmp(all[c].type, "out") == 0) sign = -1;
        sum = sum + (all[c].amount *sign);
        all[c].balances = sum;
        printf("余额:%lf\n", &all[c].balances);
    }

}

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • SecretGarden 2018-04-04 15:20
    已采纳

    所有的scanf函数中的变量,应该传入变量的地址,例如:
    将:

    printf("请输入金额:\n");
    scanf("%f", all[a].amount);
    

    改为:

    printf("请输入金额:\n");
        scanf("%f", &all[a].amount);
    

    另外,建议修改代码,消除所有的编译警告。你的问题也是因为你忽略警告造成的。

    点赞 打赏 评论
  • blownewbee 2018-04-04 14:55

    你是怎么运行的,出了什么错,把你的键盘输入贴出来

    点赞 打赏 评论
  • blownewbee 2018-04-04 14:56

    还有,请你修改问题,把代码用工具栏上</>的按钮格式化下,你看你现在include后面的内容都没有,怎么帮你看。

    点赞 打赏 评论
  • ahui886 2018-04-04 15:02

    refer函数,begin4上面一行,close(fp)改成fclose(fp)

    点赞 打赏 评论
  • 闭上左眼看世界 2018-04-04 15:23

    scanf("%lf", &all[result[x - 1]].amount);

    点赞 打赏 评论

相关推荐 更多相似问题