标题:大佬求改bug。周日中午前DDL
只看楼主
Cxiao白
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-9-20
结帖率:50%
已结贴  问题点数:20 回复次数:11 
大佬求改bug。周日中午前DDL



#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <windows.h>

int count=0;

//定义结构体
struct logistics
{
    char name[20];
    char code[6];
    char ID[18];
    int money;
    int num;
};

//用户主界面
int UI_main()
{
    printf("                                                        **************************************\n");
    printf("                                                        *      欢迎使用银行ATM存取款系统     *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    printf("                                                                 1.开户\n");
    printf("                                                                 2.登录\n");
    printf("                                                                 0.退出\n");
    printf("                                                             请输入你的选择,回车确认;\n");
    return 100;
}

//开户
int inputinformation()
{
    printf("                                                        **************************************\n");
    printf("                                                        *            欢迎使用开户系统        *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    FILE *fp;
    logistics input;
    fp = fopen("information.dat", "a+");
    if (fp == NULL)
    {
        printf("文件打开失败。");
        return 0;
    }
    else
    {
        char code[6];
        printf("                                                                 请输入姓名\n");
        scanf("%s", &input.name);
        printf("                                                                请输入身份证号\n");
        scanf("%s", &input.ID);
        input.money = 0;
        count++;
        input.num = count;
        while (1)
        {
            printf("                                                                 请输入密码\n");
            scanf("%s ", &input.code);
            printf("                                                                请再次输入密码\n");
            scanf("%s ", &code);
            if (strcmp(input.code, code) == 0)
            {

                fwrite(&input, sizeof(logistics), 1, fp);
                printf("                                                                   开户成功,您的账户是%s", input.ID);
                break;
            }
            else
            {
                printf("                                                              两次密码不一致请重新输入");
                system("pause");
                system("CLS");
            }
        }
    }
    fclose(fp);

    return 1;
}

//登录
int login()
{
    printf("                                                        **************************************\n");
    printf("                                                        *      欢迎登录银行ATM存取款系统     *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    FILE *fp1;
    logistics login;
    char n[18];
    char m[6];
    printf("                                                                 请输入您的账户\n");
    scanf("%s", &n);
    printf("\n");
    if ((fp1 = fopen("information.dat", "r")) != NULL)
    {
        rewind(fp1);
        fread(&login, sizeof(login), 1, fp1);

        while (!feof(fp1))
        {

            if (strcmp(login.ID, n)==0)
            {
                while(1)
                {
                    printf("                                                   请输入密码\n");
                    scanf("%s", &m);
                    if (strcmp(login.code, m) == 0)
                    {
                        printf("                                               登录成功");
                        break;
                    }
                    else
                    {
                        printf("                                               密码错误");
                        system("CLS");
                    }

                }
               
            }
            fread(&login, sizeof(login), 1, fp1);
            
        }
    }
    else
    {
        printf("cannot open the file\n");
        return 0;
    }

    fclose(fp1);
    return login.num;
}

//业务菜单
void UI_next()
{
    printf("                                                        **************************************\n");
    printf("                                                        *            请选择您的业务          *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    printf("                                                                     1.存款\n");
    printf("                                                                     2.取款\n");
    printf("                                                                     3.转账\n");
    printf("                                                                     4.查询\n");
    printf("                                                                     5.修改密码\n");
    printf("                                                                     0.退出\n\n");
   
}

//查询
int search()
{
    FILE *fp1;
    logistics find;
    char n[18];
    printf("请输入您的账户\n");
    scanf("%s", &n);
    printf("\n");
    if ((fp1 = fopen("information.dat", "r")) != NULL)
    {
        rewind(fp1);
        fread(&find, sizeof(find), 1, fp1);

        while (!feof(fp1))
        {

            if (strcmp(find.ID,n)==0)
            {
                printf("您的账户是\n");
                printf("%s\n", find.ID);
                printf("您当前账户余额为\n");
                printf("%d\n", find.money);
                system("pause");

            }

            fread(&find, sizeof(find), 1, fp1);
        }
        return 1;

    }
    else
    {
        printf("cannot open the file\n");
        return 0;
    }

    fclose(fp1);
}

//存款
int again(int admin)
{
    int  b = 0, n = 0, i = 0,a = 0;
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "r+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;
    rewind(fp);
    while (!feof(fp))
    {
        fread(&chang[i], sizeof(logistics), 1, fp);
        n++;
        if (chang[i].num== a )
        {
            printf("请放入存款\n");
            scanf("%d", &b);
            chang[i].money = chang[i].money + b;
            break;

        }
        else
        {
            fclose(fp);
            return 0;
        }
    }
    i++;

    fp = fopen("information.dat", "r+");
    for (i = 0; i < n; i++)
    {

        fwrite(&chang[i], sizeof(logistics), 1, fp);
    }


    fclose(fp);
    return 0;
}

//取款
int take(int admin)
{
    int  b = 0, n = 0, i = 0, a = 0,c=0;
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "r+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;
    rewind(fp);
    while (!feof(fp))
    {
        fread(&chang[i], sizeof(logistics), 1, fp);
        n++;
        if (chang[i].num == a)
        {
            printf("请输入取款金额\n");
            scanf("%d", &b);
            c = chang[i].money - b;
            if (c > 0)
            {
                printf("取款成功");
                chang[i].money = c;
                break;
            }
            else
            {
                printf("余额不足");
                fclose(fp);
                return 0;

            }
        }
        else
        {
            fclose(fp);
            return 0;
        }
    }
    i++;

    fp = fopen("information.dat", "r+");
    for (i = 0; i < n; i++)
    {

        fwrite(&chang[i], sizeof(logistics), 1, fp);
    }


    fclose(fp);
    return 1;
}

//转账
int bring(int admin)
{
    int  b = 0, n = 0, i = 0, a = 0, c = 0;
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "r+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;
    rewind(fp);
    while (!feof(fp))
    {
        fread(&chang[i], sizeof(logistics), 1, fp);
        n++;
        if (chang[i].num == a)
        {
            printf("请输入转账金额\n");
            scanf("%d", &b);
            c = chang[i].money - b;
            if (c > 0)
            {
                FILE *fp1;
                logistics find;
                char n[18];
                printf("请输入转账用户帐号\n");
                scanf("%s", &n);
                printf("\n");
                if ((fp1 = fopen("information.dat", "r")) != NULL)
                {
                    rewind(fp1);
                    fread(&find, sizeof(find), 1, fp1);

                    while (!feof(fp))
                    {

                        if (strcmp(find.ID,n)==0)
                        {
                            find.money = find.money + b;
                        }

                        fread(&find, sizeof(find), 1, fp1);
                    }
                    printf("转账成功");
                    chang[i].money = c;
                    break;
                }
                else
                {
                    printf("余额不足");
                    fclose(fp);
                    return 0;

                }
            }
            else
            {
                fclose(fp);
                return 0;
            }
        }
        i++;

        fp = fopen("information.dat", "r+");
        for (i = 0; i < n; i++)
        {

            fwrite(&chang[i], sizeof(logistics), 1, fp);
        }


        fclose(fp);
        return 1;
    }
    return 1;
}

//更改密码
int change(int admin)
{
    int   n = 0, i = 0, a = 0;
    char b[6];
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "r+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;
    rewind(fp);
    while (!feof(fp))
    {
        fread(&chang[i], sizeof(logistics), 1, fp);
        n++;
        if (chang[i].num == a)
        {
            while (1)
            {
                printf("                                                                 请输入密码\n");
                scanf("%s ", &chang[i].code);
                printf("                                                                请再次输入密码\n");
                scanf("%s ", &b);
                if (strcmp(chang[i].code, b) == 0)
                {

                    fwrite(&chang[i], sizeof(logistics), 1, fp);
                    printf("                                                                   更改成功");
                    break;
                }
                else
                {
                    printf("                                                              两次密码不一致请重新输入");
                    system("CLS");
                }
            }
            break;

        }
        else
        {
            fclose(fp);
            return 0;
        }
    }
    i++;

    fp = fopen("information.dat", "r+");
    for (i = 0; i < n; i++)
    {

        fwrite(&chang[i], sizeof(logistics), 1, fp);
    }


    fclose(fp);
    return 0;
}

int main()
{
    UI_main();
    int pos = -1,admin=0,poe=-1;
    scanf("%d", &pos);
    fflush(stdin);
    while (pos != 10)
    {
        switch (pos)
        {
        case 1:inputinformation();
            break;
        case 2:
        
            admin = login();
            if (admin != 0)
            {
                UI_next();
                scanf("%d", &poe);
                while (poe != 10)
                {
                    switch (poe)
                    {
                    case 1:again(admin);
                        break;
                    case 2:take(admin);
                        break;
                    case 3:bring(admin);
                        break;
                    case 4:search();
                        break;
                    case 5:change(admin);
                        break;
                    case 0:exit(0);
                        break;
                    default:
                        printf("输入信息有误,请重新输入。\n");
                    }

                    system("pause");
                    system("CLS");

                    UI_next();
                    scanf("%d", &poe);
                    system("CLS");
                }
            }
        
            else
            {
                break;
            }
            break;
        case 0:exit(0);
            break;
        default:
            printf("输入信息有误,请重新输入。\n");
        }
        system("pause");
        system("CLS");

        UI_main();
        scanf("%d", &pos);
        system("CLS");
    }
}



[此贴子已经被作者于2017-9-24 11:27编辑过]

搜索更多相关主题的帖子: int printf return scanf break 
2017-09-23 19:22
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
得分:5 
参考一下你这个怎么写的。今天也写了一个,搞得好累。

菜鸟一枚,求各位大神多多关照。
2017-09-23 21:59
Cxiao白
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-9-20
得分:0 
回复 2楼 yu965634383
东大的啊,这个运行有bug……
不要照抄啊,给条活路
2017-09-24 00:07
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:5 
字符串输入不用加&, 类似scanf("%s", str);,  scanf("%s这里不要加无用的空格,当然你要占一个位置的话你就加一个空格", ...);
还有一些其他的地方

程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <windows.h> 

int count = 0;

//定义结构体
struct logistics
{
    char name[20];
    char code[16];
    char ID[20];
    int money;
    int num;
};

//用户主界面
int UI_main()
{
    printf("                                                        **************************************\n");
    printf("                                                        *      欢迎使用银行ATM存取款系统     *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    printf("                                                                 1.开户\n");
    printf("                                                                 2.登录\n");
    printf("                                                                 0.退出\n");
    printf("                                                             请输入你的选择,回车确认;\n");
    return 100;
}


//开户
int inputinformation()
{
    printf("                                                        **************************************\n");
    printf("                                                        *            欢迎使用开户系统        *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    FILE *fp;
    logistics input;
    fp = fopen("information.dat", "ab+");
    if (fp == NULL)
    {
        printf("文件打开失败。");
        return 0;
    }
    else
    {
        char code[16];
        printf("                                                                 请输入姓名 和 自定义账户(身份证号)\n");
        scanf("%s", input.name);    //字符串不用加&
        scanf("%s", input.ID);     //字符串不用加&
        input.money = 0;
        count++;
        input.num = count;
        while (1)
        {
            printf("                                                                 请输入密码\n");
            scanf("%s", input.code);
            printf("                                                                请再次输入密码\n");
            scanf("%s", code);           //字符串不用加&
            if (strcmp(input.code, code) == 0)
            {

                fwrite(&input, sizeof(logistics), 1, fp);
                printf("                                                                   开户成功,您的账户是%s", input.ID);
                break;
            }
            else
            {
                printf("                                                              两次密码不一致请重新输入");
                system("pause");
                system("CLS");
            }
        }
    }
    fclose(fp);
    Sleep(1400);
    return 1;
}

//登录
int login()
{
    printf("                                                        **************************************\n");
    printf("                                                        *      欢迎登录银行ATM存取款系统     *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    FILE *fp1;
    logistics login;
    char n[18];
    char m[16];
    printf("                                                                 请输入您的账户\n");
    scanf("%s", n);    //字符串不用加&
    printf("\n");
    if ((fp1 = fopen("information.dat", "rb")) != NULL)
    {
        rewind(fp1);

        int flag = 0;
        while (!feof(fp1))
        {
            fread(&login, sizeof(logistics), 1, fp1);
            if (strcmp(login.ID, n) == 0)
            {
                while (1)
                {
                    printf("                                                   请输入密码\n");
                    scanf("%s", m);           //字符串不用加&
                    if (strcmp(login.code, m) == 0)
                    {
                        printf("                                               登录成功");
                        flag = 1;
                        break;
                    }
                    else
                    {
                        printf("                                               密码错误");
                        system("CLS");
                    }

                }

            }
            //fread(&login, sizeof(logistics), 1, fp1);
            if (flag == 1)
            {
                break;
            }

        }
    }
    else
    {
        printf("cannot open the file\n");
        return 0;
    }
    printf("\n登录中...");
    Sleep(800);
    fclose(fp1);
    fflush(stdin);
    system("cls");

    return login.num;
}

//业务菜单
void UI_next()
{
    printf("                                                        **************************************\n");
    printf("                                                        *            请选择您的业务          *\n");
    printf("                                                        **************************************\n");
    printf("\n");
    printf("                                                                     1.存款\n");
    printf("                                                                     2.取款\n");
    printf("                                                                     3.转账\n");
    printf("                                                                     4.查询\n");
    printf("                                                                     5.修改密码\n");
    printf("                                                                     0.退出\n\n");

}

//查询
int search()
{
    system("cls");
    FILE *fp1;
    logistics find;
    char n[18];
    printf("请输入您的账户\n");
    scanf("%s", n);
    printf("\n");
    if ((fp1 = fopen("information.dat", "rb")) != NULL)
    {
        rewind(fp1);
        fread(&find, sizeof(logistics), 1, fp1);

        while (!feof(fp1))
        {

            if (strcmp(find.ID, n) == 0)
            {
                printf("您的账户是\n");
                printf("%s\n", find.ID);
                printf("您当前账户余额为\n");
                printf("%d\n", find.money);
                system("pause");

            }

            fread(&find, sizeof(find), 1, fp1);
        }
        return 1;

    }
    else
    {
        printf("cannot open the file\n");
        return 0;
    }
    printf("\n页面跳转中...");
    Sleep(1000);
    fclose(fp1);
}

//存款
int again(int admin)
{
    system("cls");
    int  b = 0, n = 0, i = 0, a = 0;
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "rb+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;

    rewind(fp);
    fread(&chang[i], sizeof(logistics), 1, fp);
    while (!feof(fp))
    {

        n++;
        if (chang[i].num == a)
        {
            printf("请放入存款\n");
            scanf("%d", &b);
            chang[i].money = chang[i].money + b;
            break;

        }

        i++;
        fread(&chang[i], sizeof(logistics), 1, fp);
    }

    fclose(fp);
    fp = fopen("information.dat", "rb+");
    for (i = 0; i < n; i++)
    {

        fwrite(&chang[i], sizeof(logistics), 1, fp);
    }

    fclose(fp);
    printf("\n存款中...");
    Sleep(1000);
    return 0;
}

//取款
int take(int admin)
{
    system("cls");
    int  b = 0, n = 0, i = 0, a = 0, c = 0;
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "rb+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;
    rewind(fp);
    fread(&chang[i], sizeof(logistics), 1, fp);
    while (!feof(fp))
    {

        n++;
        if (chang[i].num == a)
        {
            printf("请输入取款金额\n");
            scanf("%d", &b);
            c = chang[i].money - b;
            if (c > 0)
            {
                printf("取款成功");
                chang[i].money = c;
                break;
            }
            else
            {
                printf("余额不足");
                fclose(fp);
                break;

            }
        }

        i++;
        fread(&chang[i], sizeof(logistics), 1, fp);
    }

    fclose(fp);
    fp = fopen("information.dat", "r+");
    for (i = 0; i < n; i++)
    {

        fwrite(&chang[i], sizeof(logistics), 1, fp);
    }

    fclose(fp);
    printf("\n取款中...");
    Sleep(1000);
    return 1;
}

//转账
int bring(int admin)
{
    system("cls");
    int  b = 0, nb = 0, i = 0, a = 0, c = 0, j = 0;
    logistics chang[999];
    FILE *fp;
    fp = fopen("information.dat", "rb+");
    rewind(fp);
    fread(&chang[i], sizeof(logistics), 1, fp);
    while (!feof(fp))
    {
        i++;
        fread(&chang[i], sizeof(logistics), 1, fp);
    }
    for (j = 0; j <= i; j++)
    {
        if (chang[j].num == admin)
        {
            printf("请输入转账金额:\n");
            scanf("%d", &b);
            chang[j].money -= b;
            break;
        }
    }
    char id[29];
    printf("\n请输入转账账户:\n");
    scanf("%s", id);
    for (j = 0; j <= i; j++)
    {
        if (strcmp(chang[j].ID, id) == 0)
        {
            chang[j].money += b;
            break;
        }
    }
    rewind(fp);
    for (j = 0; j <= i; j++)
    {
        fwrite(&chang[j], sizeof(logistics), 1, fp);
    }
    fclose(fp);
    printf("\n转账中...");
    Sleep(1000);
    return 0;
}


//更改密码
int change(int admin)
{
    system("cls");
    int   n = 0, i = 0, a = 0;
    char b[16];
    FILE *fp;
    logistics chang[999];
    fp = fopen("information.dat", "rb+");
    if (fp == NULL)
    {
        printf("无法打开\n");
        return 0;
    }

    a = admin;
    rewind(fp);
    fread(&chang[i], sizeof(logistics), 1, fp);
    while (!feof(fp))
    {

        n++;
        if (chang[i].num == a)
        {
            while (1)
            {
                printf("                                                                 请输入密码\n");
                scanf("%s", chang[i].code);
                printf("                                                                请再次输入密码\n");
                scanf("%s", b);
                if (strcmp(chang[i].code, b) == 0)
                {

                    fwrite(&chang[i], sizeof(logistics), 1, fp);
                    printf("                                                                   更改成功");
                    break;
                }
                else
                {
                    printf("                                                              两次密码不一致请重新输入");
                    system("CLS");
                }
            }
            break;

        }

        i++;
        fread(&chang[i], sizeof(logistics), 1, fp);
    }

    fclose(fp);
    fp = fopen("information.dat", "rb+");
    for (i = 0; i < n; i++)
    {

        fwrite(&chang[i], sizeof(logistics), 1, fp);
    }


    fclose(fp);
    fflush(stdin);
    printf("\n密码更新中...");
    Sleep(1000);
    return 0;
}

int main()
{
    
    UI_main();
    int pos = -1, admin = 0, poe = -1;
    scanf("%d", &pos);
    fflush(stdin);
    while (pos != 0)
    {
        switch (pos)
        {
        case 1:inputinformation();
            break;
        case 2:

            admin = login();
            if (admin != 0)
            {
                UI_next();
                scanf("%d", &poe);
                while (poe != 0)
                {
                    switch (poe)
                    {
                    case 1:again(admin);
                        break;
                    case 2:take(admin);
                        break;
                    case 3:bring(admin);
                        break;
                    case 4:search();
                        break;
                    case 5:change(admin);
                        break;
                    case 0:UI_main();
                        break;
                    default:
                        printf("输入信息有误,请重新输入。\n");
                    }

                    system("CLS");

                    UI_next();
                    scanf("%d", &poe);
                    system("CLS");
                }
            }

            else
            {
                break;
            }
            break;
        case 0:exit(0);
            break;
        default:
            printf("输入信息有误,请重新输入。\n");
        }
        system("CLS");
        UI_main();
        scanf("%d", &pos);
        system("CLS");
    }
}


[此贴子已经被作者于2017-9-24 00:47编辑过]


早知做人那么辛苦!  当初不应该下凡
2017-09-24 00:40
Cxiao白
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-9-20
得分:0 
回复 4楼 炎天
两点了睡不着上来看看,居然有大佬回信!语言已经不能描述我内心此刻的激动了!!!
真的,真的,真的得非常感谢!
这些问题我会一行一行照着看的,真的是,非常感谢!
试着运行了一下,发现查询那里好像显不出来,我会试着改改看,总觉得弄明白这个题我就朝着远离小白的方向迈出了一大步!
最后,再次奉上我真诚的祝福,大佬一生幸福平安!
2017-09-24 01:59
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
得分:0 
回复 4楼 炎天
scanf的用法还有这样的要求啊,怪不得我这里有那么多警告

菜鸟一枚,求各位大神多多关照。
2017-09-24 08:34
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
得分:0 
为什么已登录就发生警告呢,程序直接被终止了。

菜鸟一枚,求各位大神多多关照。
2017-09-24 08:50
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
得分:0 
以下是引用Cxiao白在2017-9-24 01:59:15的发言:


试着运行了一下,发现查询那里好像显不出来,

怎么显不出来?

早知做人那么辛苦!  当初不应该下凡
2017-09-24 08:52
Cxiao白
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-9-20
得分:0 
回复 8楼 炎天
可能是我C++的版本太低,就是在查询那里输入账户后,直接跳转到上一级页面,没有显出账户信息。
2017-09-24 11:26
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
得分:0 
回复 3楼 Cxiao白
Desktop.zip (4.59 KB)

我昨天写的,今天看看感觉好乱啊 。

菜鸟一枚,求各位大神多多关照。
2017-09-24 11:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-480896-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.396311 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved