paprika1204 2022-04-19 21:51 采纳率: 66.7%
浏览 37
已结题

求快看看 为什么会说使用未初始化的局部变量x 怎么修改


 
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include "ATM.h"
 
struct Date
{
    int year;
    int month;
    int day;
};
 
 
struct Account           //定义结构体
{
    char name[20];
    char no[20];
    int money;
    char mima[6];
    struct Account* next;          //定义结构体
};
 
struct Record
{
    char no[20];
    struct Date date;
    char number;
};
 
 
 
/*主函数*/
void main()
{
    char choose; //choose为定义输入选择的变量
    int flag = 1;
    struct Record* Phead = NULL; //Phead为定义二层头指针
    struct Account* head = NULL;   //head为定义一层头指针
    printf("*******************************\n");
    printf("***欢迎使用ATM自动取款机系统***\n");
    printf("*******************************\n");
    printf("——————————————\n");
    printf("|    1  开户               |\n");
    printf("——————————————\n");
    printf("|    2  登陆               |\n");
    printf("——————————————\n");
    printf("|    3 前台客户信息查询中心|\n");
    printf("——————————————\n");
 
    int x ;
    gets(x);
    system("cls");
 
    if (x > 0 && x < 3)
    {
        switch (x)
        {
        case 1:
            system("cls");
            kaihu(head);   //调用开户函数
            break;
 
        case 2:
            system("cls");
            denglu(head);   //调用登陆函数
            break;
 
        case 3:
            system("cls");
            menu();   //调用后台菜单函数
            break;
        }
    }
    
    
}
/*开户函数*/
void kaihu(struct Account* head)
{
    head = NULL;
    FILE* fp;   //定义文件指针
    struct Account* p1 = NULL, * p2 = NULL;   //p1,p2为定义链表指针
    p1 = (struct Account*)malloc(sizeof(struct Account));  //开辟内存单元
    printf("请输入您的姓名:\n");  //请数据输入链表中
    scanf("%s", p1->name);
    printf("请设置您的卡号:\n");
    scanf("%s", p1->no);
    printf("请设置您银行卡密码:\n");
    scanf("%s", p1->mima);
    p1->money = 0;
    p1->next = NULL;
    printf("您的个人信息为");
    printf("姓名:%s \n卡号:%s \n余额:%4d\n", p1->name, p1->no, p1->money);
    if (NULL == head)           //为新用户开辟内存单元
    {
        head = (struct Account*)malloc(sizeof(struct Account));
        head->next = p1;    //进行头插法,将其作为第一个节点
    }
    else    //为新增客户开辟内存单元
    {
        for (p2 = head; p2->next != NULL; p2 = p2->next); //进行尾插
        p2->next = p1;
    }
    if ((fp = fopen("accounts.txt", "ab+")) == NULL) //打开文件
    {
        printf("can not poen file\n");
        return;
    }
    if (fwrite(p1, sizeof(struct Account), 1, fp) != 1)  //将链表信息写入文件中
        printf("file write error\n");
    fclose(fp);
    printf("\n");
    printf("恭喜您开户成功,请登录\n");
    system("pause");
    system("cls");
    denglu(head);
}
 
 
//登陆函数
 
void denglu(struct Account* head)
{
    char d[20];
    char mima[20];
    int i, j;
    FILE* fp;     //定义文件指针
    struct Account* p, * q = NULL;
    if ((fp = fopen("accounts.txt", "rb+")) == NULL)   //打开一个二进制文件,为读方式
    {
        printf("不能打开文件\n");   //如不能打开,则结束程序
    }
    p = (struct Account*)malloc(sizeof(struct Account));   //申请空间
    head = p;
    while (!feof(fp))       //循环读数据直到文件尾结束
 
    {
        if (1 != fread(p, sizeof(struct Account), 1, fp))
            break;   //如果没读到数据,跳出循环
        p->next = (struct Account*)malloc(sizeof(struct Account));  //为下一个结点申请空间
        q = p;  //保存当前节点的指针,作为下一结点的前驱
        p = p->next;  //指针后移,新读入数据链到当前表尾
 
    }
    q->next = NULL;  //最后一个结点的后继指针为空
    fclose(fp);
    printf("  **********************\n");
    printf("  ***   欢迎来到ATM  ***\n");
    printf("  **********************\n");
    for (j = 1; j < 4; j++)      //限制卡号输入的次数的循环
    {
        printf("请输入您的卡号\n");
        scanf("%s", d);
        for (q = head; q != NULL; q = q->next)   //遍历链表
        {
            if (strcmp(q->no, d) != 0)  //核对账号
            {
                continue;   //跳出循环
            }
            else
            {
                int count = 1;
                while(strcmp(q->mima, mima) != 0)   //限制密码输入的次数的循环
                {
                    if (count < 3)
                    {
                        printf("\n\n请输入您的密码\n");
                        scanf("%s", mima);
                        count++;
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
        }
 
 
        printf("\n\n\n您输入的卡号有误,请重试\n");
        system("pause");
        system("cls");
    }
    printf("您的卡号三次输入错误,谢谢使用\n");
    exit(0);
}
 
 
 
//银行菜单函数
 
void caidan(struct Account* head)
{
    head = NULL;
    int i;      //i为客户选择输入的变量
    while (1)
    {
        printf("*********************************\n");
        printf("**  1 取款   *****   2 查询    **\n");
        printf("*********************************\n");
        printf("**  3 转账   *****   4 修改密码**\n");
        printf("*********************************\n");
        printf("**  5 存款   *****   6 退出    **\n");
        printf("*********************************\n");
        scanf("%d", &i);
        if (i < 6 || i>0)
        {
            switch (i)
            {
            case 1:qukuan(head);       //调用银行取款函数
                system("pause");
                system("cls");
                break;
            case 2:system("cls");
                chaxun(head);  //调用银行查询函数
                break;
            case 3:system("cls");
                zhuanzhang(head);  //调用银行转账函数
                break;
            case 4:system("cls");
                xgmm(head);  //调用银行修改密码函数
                break;
            case 5:system("cls");
                cunkuan(head);  //调用银行存款函数
                break;
            case 6:system("cls");
                tuichu();  //调用银行退出函数
                break;
            }
        }
        else
        {
            printf("您的输入有误\n");
            system("pause");
            system("cls");
        }
    }
}
 
 
//银行取款函数
void qukuan(struct Account* head)
{
    head = NULL;   //head为链表头指针
    int i;
    FILE* fp;          //定义文件指针
    struct Account* p, * q = NULL;
    if ((fp = fopen("records.txt", "rb+")) == NULL) //打开一个二进制文件,为读方式
    {
        printf("不能打开文件\n");  //如不能打开,则结束程序
    }
    p = (struct Account*)malloc(sizeof(struct Account));  //申请空间
    head = p;
    while (!feof(fp))   //循环读数据直到文件尾结束
    {
        if (1 != fread(p, sizeof(struct Account), 1, fp))
            break;   //如果没有读到数据,跳出循环
        p->next = (struct Account*)malloc(sizeof(struct Account));  //为下一个结点申请空间
        q = p;   //保存当前结点的指针,作为下一个结点的前驱
        p = p->next;  //指针后移,新读入数据链到当前表尾
    }
    q->next = NULL;  //最后一个结点的后继指针为空
    fclose(fp);
    system("cls");
    printf("请按要求选择您要取款的金额\n");
    scanf("%d", &i);
    if (i > 6 || i <= 0)    //限制输入范围
    {
        printf("对不起,您的输入有误\n\n");
        return;
    }
    else
    {
        i = 100 * i;  //对应选项乘以一百为取款金额
        if (i > q->money)
        {
            printf("对不起,您的金额不足\n");
            system("pause");
            system("cls");
            caidan(head);   //调用取款机菜单函数
        }
        else
        {
            q->money -= i;  //对金额进行处理
            if ((fp = fopen("records.txt", "wb+")) == NULL)  //打开文件
            {
                printf("cannot open file\n");
                return;
            }
            if (fwrite(q, sizeof(struct Account), 1, fp) != 1) //将修改的信息重新写入文件
                printf("file write error\n");
            printf("您已经成功取走%d元\n");
            q->next = NULL;
            fclose(fp);    //关闭文件
        }
 
    }
}
 
 
//银行转账函数
void zhuanzhang(struct Account* head)
{
    head = NULL;
    FILE* fp;  //定义文件指针
    struct Account* p, * q = NULL;
    if ((fp = fopen("records.txt", "rb+")) == NULL)  //打开一个二进制文件,为读方式
    {
        printf("不能打开文件\n");  //如不能打开,则结束程序
    }
    p = (struct Account*)malloc(sizeof(struct Account));   //申请空间
    head = p;
    while (!feof(fp))    //循环读数据直到文件尾结束
    {
        if (1 != fread(p, sizeof(struct Account), 1, fp))
            break;    //如果没读到数据,跳出循环
        p->next = (struct Account*)malloc(sizeof(struct Account));  //为下一个结点申请空间
        q = p;   //保存当前结点的指针,作为下一个结点的前驱
        p = p->next;   //指针后移,新读入数据链到当前表尾
    }
    q->next = NULL;   //最后一个结点的后继指针为空
    fclose(fp);
    int i, j, k;
    printf("请输入帐号号码\n");
    scanf("%d", &i);
    printf("请再次输入帐号号码\n");   //核对卡号
    scanf("%d", &j);
    if (i != j)
    {
        printf("两次账号不同,请重新输入\n");
        zhuanzhang(head);
    }
    else
    {
        system("cls");
        printf("请输入转账金额\n");
        scanf("%d", &k);
        if (k > 6 || k <= 0)
        {
            printf("对不起,您的输入有误\n\n");
            return;
        }
        else
        {
            k = k * 100;
            if (k > q->money)    //对余额进行判断
            {
                printf("对不起,您的余额不足\n");
                system("pause");
                system("cls");
                caidan(head);
            }
            else
            {
                printf("您已成功转账%d元\n", k);
                q->money -= k;
                if ((fp = fopen("records.txt", "wb+")) == NULL)
                {
                    printf("cannot open file\n");
                    return;
                }
                if (fwrite(q, sizeof(struct Account), 1, fp) != 1)  //将数据重新写入文件
                    printf("file write error\n");
                q->next = NULL;
                fclose(fp);
                system("pause");
                system("cls");
            }
        }
    }
}
 
 
 
//银行查询函数
void chaxun(struct Account* head)
{
    head = NULL;  //链表头指针
    FILE* fp;  //定义文件指针
    struct Account* p, * q = NULL;
    if ((fp = fopen("records.txt", "rb+")) == NULL)  //打开一个二进制文件,为读方式
    {
        printf("不能打开文件\n");  //如不能打开,则结束程序
    }
    p = (struct Account*)malloc(sizeof(struct Account));   //申请空间
    head = p;
    while (!feof(fp))    //循环读数据直到文件尾结束
    {
        if (1 != fread(p, sizeof(struct Account), 1, fp))
            break;    //如果没读到数据,跳出循环
        p->next = (struct Account*)malloc(sizeof(struct Account));  //为下一个结点申请空间
        q = p;   //保存当前结点的指针,作为下一个结点的前驱
        p = p->next;   //指针后移,新读入数据链到当前表尾
    }
    q->next = NULL;   //最后一个结点的后继指针为空
    fclose(fp);
    printf("您卡上原有余额%d元\n\n", q->money);
    system("pause");
    system("cls");
}
 
 
//银行修改密码函数
//
 
void xgmm(struct Account* head)
{
    head = NULL;     
    char mima[20];
    FILE* fp;  //定义文件指针
    struct Account* p, * q = NULL;
    if ((fp = fopen("records.txt", "rb+")) == NULL) 
    {
        printf("不能打开文件\n"); 
    }
    p = (struct Account*)malloc(sizeof(struct Account));   //申请空间
    head = p;
    while (!feof(fp))    
    {
        if (1 != fread(p, sizeof(struct Account), 1, fp))
            break;  
        p->next = (struct Account*)malloc(sizeof(struct Account));  
        q = p;  
        p = p->next;   
    }
    q->next = NULL;  
    fclose(fp);
    printf("请输入您的原密码\n");
    scanf("%s", mima);
    if (strcmp(q->mima, mima) == 0)  
{
        {
            printf("密码正确\n");
            printf("请输入您的新密码:\n");
            scanf("%s", q->mima);
            if ((fp = fopen("accounts.txt", "wb+")) == NULL) 
            {
                printf("cannot open file\n");
            }
            if (fwrite(q, sizeof(struct Account), 1, fp) != 1)   
                printf("file write error\n");
            fclose(fp);
            printf("修改密码成功\n\n\n\n\n");
        }
    }
    else
    {
        printf("您输入的密码与原密码不同\n");
        return;
        system("pause");
    }
    q->next = NULL;
}
 
 
//银行存款函数
 
 
 
void cunkuan(struct Account* head)
{
    int i;
    head = NULL; 
    FILE* fp; 
    struct Account* p, * q = NULL;
    if ((fp = fopen("records.txt", "rb+")) == NULL)  
    {
        printf("不能打开文件\n");  
    }
    p = (struct Account*)malloc(sizeof(struct Account));   
    head = p;
    while (!feof(fp))    //循环读数据直到文件尾结束
    {
        if (1 != fread(p, sizeof(struct Account), 1, fp))
            break;    //如果没读到数据,跳出循环
        p->next = (struct Account*)malloc(sizeof(struct Account));
        q = p;   
        p = p->next;  
    }
    q->next = NULL; 
    fclose(fp);
    system("cls");
    printf("您卡上原有余额%d元\n", q->money);
    printf("请选择您要存入的余额\n");
    scanf("%d", &i);
    if (i > 6 || i <= 0)
    {
        printf("对不起,您的输入有误\n\n");
        return;
    }
    else
    {
        i = 100 * i;
        q->money += i;
        if ((fp = fopen("records.txt", "wb+")) == NULL)  
        {
            printf("cannot open file\n");
        }
        if (fwrite(q, sizeof(struct Account), 1, fp) != 1)  
            printf("file write error\n");
        printf("您已经成功存取%d元\n", i);
        q->next = NULL;
        fclose(fp);
        system("pause");
        system("cls");
    }
}
 
 
 
//退出银行函数
 
void tuichu()
{
    printf("谢谢使用\n");
    exit(0);
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-04-19 22:11
    关注

    第54行 第55行
    int x ;
    scanf("%d",&x); //gets(x);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月27日
  • 已采纳回答 4月19日
  • 创建了问题 4月19日

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教