| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2169 人关注过本帖
标题:C++ ATM机大作业 文件写入报错
只看楼主 加入收藏
牧人马
Rank: 4
等 级:业余侠客
威 望:6
帖 子:49
专家分:229
注 册:2017-12-24
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:4 
C++ ATM机大作业 文件写入报错
build函数报错,代码未编完,我只是想试试刚刚编的一部分(build和log_in)能不能用,我贴图,问的比较仓促,不清楚的我可以详细解释我的问题。

图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
程序代码:
#include <iostream>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<fstream>
#include<stdio.h>
using namespace std;
class ATM
{
public:
    void build();                            //建立账户
    int log_in();                             //登录账户
    void deposit();                       //存款
    void withdrawal();                  //取款
    void balance_enquiry();        //余额查询
    void interest();                         //利率计算
    ATM(string acc,string pas,string nam,char g,string addr,string pho,string id,double s):
        account(acc),password(pas),name(nam),gender(g),address(addr),phone(pho),ID_num(id),sum(s) {}
protected:
    string account;
    string password;
    string name;
    char gender;
    string address;
    string phone;
    string ID_num;
    double sum;
};



void ATM::build()                                   //新建账户
{
   cout<<"!!!";
    int flag=1;
     ofstream outfile_account("d0.dat"),outfile_password("d1.dat");
     ofstream outfile_name("d2.dat"),outfile_gender("d3.dat");
     ofstream outfile_address("d4.dat"),outfile_phone("d5.dat");
     ofstream outfile_idnum("d6.dat"),outfile_sum("d7.dat");
     cout<<"@@@";
     if(!outfile_account) {cerr<<"open d0.dat error"<<endl;
       exit(1);}
    if(!outfile_password) {cerr<<"open d1.dat error"<<endl;
      exit(1);}
      if(!outfile_name) {cerr<<"open d2.dat error"<<endl;
       exit(1);}
       if(!outfile_gender) {cerr<<"open d3.dat error"<<endl;
       exit(1);}
       if(!outfile_address) {cerr<<"open d4.dat error"<<endl;
       exit(1);}
       if(!outfile_phone) {cerr<<"open d5.dat error"<<endl;
       exit(1);}
       if(!outfile_idnum) {cerr<<"open d6.dat error"<<endl;
       exit(1);}
       if(!outfile_sum) {cerr<<"open d7.dat error"<<endl;
       exit(1);}
cout<<"&&&";
        typedef struct user
        {
    string account;
    string password;
    string name;
    char gender;
    string address;
    string phone;
    string ID_num;
    double sum;
        }USER;

        USER users[30]={0};

        for(int i=0;flag==1;i++)
        {cout<<"请输入要创建的银行卡账号:";
            cin>>users[i].account;   outfile_account<<users[i].account<<" ";
            cout<<"请输入要创建的银行卡密码:";
        cin>>users[i].password;  outfile_password<<users[i].password<<" ";
        cout<<"请输入您的姓名:";
        cin>>users[i].name;   outfile_name<<users[i].name<<" ";
        cout<<"请输入您的性别:";
        cin>>users[i].gender;   outfile_gender<<users[i].gender<<" ";
        cout<<"请输入您的现住址:";
        cin>>users[i].address;   outfile_address<<users[i].address<<" ";
        cout<<"请输入您的电话或手机号码:";
        cin>>users[i].phone;   outfile_phone<<users[i].phone<<" ";
        cout<<"请输入您的身份证号:";
        cin>>users[i].ID_num;  outfile_idnum<<users[i].ID_num<<" ";
        users[i].sum=0;
        outfile_sum<<users[i].sum<<" ";



}
}




int ATM::log_in()                                   //输入账号和密码对应后登录成功,最多尝试输入3次
{
    string account1="0"  , password1;
    ifstream infile_account ("d0.dat",ios::in),infile_password("d1.dat",ios::in);
    int n=3,flag=1,m;
    cout<<"请输入银行账户的账号:";
    cin>>account;
    for(int i=0;i<9999 ;i++)    // !!!!!!!!!!!!!!!缺少条件
    {
        infile_account>>account1;
        if(account1==account)
        {
            m=i;
            flag--;
            break;
        }
    }
    if(flag==1) cout<<"无此账号",exit(1);
    if(flag==0) cout<<"下一步,";
    for(; n>=0;)
    {
        cout<<"请输入密码:";
        cin>>password;
        infile_password>>password1;
        if(password==password1)
        {
            cout<<"登录成功"<<endl;
            break;
        }
        else
        {
            cout<<"账号与密码不匹配,你还有"<<n<<"次尝试次数"<<endl;
            n--;
            if(n<0)
            {
                cout<<"次数用尽"<<endl;
                return 0;
            }
        }
    }
}

void ATM::deposit()
{
    int add;
    cout<<"请输入要存入ATM机的钱数(仅支持人民币的面额为100元,即输入整百的数字):";
    cin>>add;
    sum=sum+add;
}

void ATM::withdrawal()

 {

int add;
    cout<<"请输入要取出ATM机的钱数(仅支持人民币的面额为100元,即输入整百的数字):";
    cin>>add;
    sum=sum-add;
}

void ATM::balance_enquiry()
{
    cout<<"您的余额为:"<<sum<<""<<endl;
}
void ATM::interest()
{
    int a,income;
    double in,time;
    cout<<"0.返回"<<endl;
    cout<<"1.活期存款-0.35%"<<endl;
    cout<<"2.定期存款三个月-1.10%"<<endl;
    cout<<"3.定期存款半年-1.30%"<<endl;
    cout<<"4.定期存款一年-1.50%"<<endl;
    cout<<"5.定期存款二年-2.10%"<<endl;
    cout<<"6.定期存款三年-2.75%"<<"\n"<<endl;
    cout<<"请选择存款类型"<<endl;
    cin>>a;
    switch(a){
    case 0 :in=0;break;
    case 1: in=0.0035;break;
    case 2: in=0.011,time=0.25;break;
    case 3: in=0.013,time=0.5;break;
    case 4: in=0.015,time=1;break;
    case 5: in=0.021,time=2;break;
    case 6: in=0.0275,time=3;break;
}
cout<<"请输入存入钱数:";
cin>>income;
if(in==0)  ;
else cout<<"可获利息"<<income*in*time<<""<<endl;
}
int main()
{
    ATM a1("1","1","1",'1',"1","1","1",1);
    a1.build();
    a1.log_in();
    return 0;
}[local]2[/local]



新建文本文档.zip (2.17 KB)
搜索更多相关主题的帖子: string cout dat users 输入 
2018-05-16 23:49
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:10 
抛出了一个 logic_error 逻辑错误异常
USER users[30] = {0};
把后面的初始化去掉
2018-05-17 10:59
牧人马
Rank: 4
等 级:业余侠客
威 望:6
帖 子:49
专家分:229
注 册:2017-12-24
收藏
得分:0 
回复 2楼 Jonny0201
谢谢啦,可以正常的运行了,但是如果在build()建立两个账户,在log_in这个函数里输入第一个账户的账号和密码,假如第一次输入的密码是错误的,第二次输入密码时需要输入第二个账户的密码才是登录成功,这明显是错误的啊,我在log_in()加了几个辅助的输出语句也没有找到哪里出问题了。代码如下,如果能看一下十分感谢,忙的话不能看也要谢谢你啦。
  
图片附件: 游客没有浏览图片的权限,请 登录注册
程序代码:
#include <iostream>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<fstream>
#include<stdio.h>
using namespace std;


 typedef struct user
{
    string account;
    string password;
    string name;
    char gender;
    string address;
    string phone;
    string ID_num;
    double sum;
}USER;

USER users[30];

class ATM
{
public:
    void build();                            //建立账户
    int log_in();                             //登录账户
    void deposit();                       //存款
    void withdrawal();                  //取款
    void balance_enquiry();        //余额查询
    void interest();                         //利率计算
    ATM(string acc,string pas,string nam,char g,string addr,string pho,string id,double s):
   account(acc),password(pas),name(nam),gender(g),address(addr),phone(pho),ID_num(id),sum(s) {}
protected:
    string account;
    string password;
    string name;
    char gender;
    string address;
    string phone;
    string ID_num;
    double sum;
};

void ATM::build()                                   //新建账户
{
    int flag=1;
    char choice;
     ofstream outfile_account("d0.dat"),outfile_password("d1.dat");
     ofstream outfile_name("d2.dat"),outfile_gender("d3.dat");
     ofstream outfile_address("d4.dat"),outfile_phone("d5.dat");
     ofstream outfile_idnum("d6.dat"),outfile_sum("d7.dat");
      if(!outfile_account) {cerr<<"open d0.dat error"<<endl;
       exit(1);}
      if(!outfile_password) {cerr<<"open d1.dat error"<<endl;
      exit(1);}
      if(!outfile_name) {cerr<<"open d2.dat error"<<endl;
       exit(1);}
       if(!outfile_gender) {cerr<<"open d3.dat error"<<endl;
       exit(1);}
       if(!outfile_address) {cerr<<"open d4.dat error"<<endl;
       exit(1);}
       if(!outfile_phone) {cerr<<"open d5.dat error"<<endl;
       exit(1);}
       if(!outfile_idnum) {cerr<<"open d6.dat error"<<endl;
       exit(1);}
       if(!outfile_sum) {cerr<<"open d7.dat error"<<endl;
       exit(1);}

        for(int i=0;flag==1;i++)
        {cout<<"请输入要创建的银行卡账号:";
            cin>>users[i].account;   outfile_account<<users[i].account<<" ";
            cout<<"请输入要创建的银行卡密码:";
        cin>>users[i].password;  outfile_password<<users[i].password<<" ";
        cout<<"请输入您的姓名:";
        cin>>users[i].name;   outfile_name<<users[i].name<<" ";
        cout<<"请输入您的性别:";
        cin>>users[i].gender;   outfile_gender<<users[i].gender<<" ";
        cout<<"请输入您的现住址:";
        cin>>users[i].address;   outfile_address<<users[i].address<<" ";
        cout<<"请输入您的电话或手机号码:";
        cin>>users[i].phone;   outfile_phone<<users[i].phone<<" ";
        cout<<"请输入您的身份证号:";
        cin>>users[i].ID_num;  outfile_idnum<<users[i].ID_num<<" ";
        users[i].sum=0;
        outfile_sum<<users[i].sum<<" ";
          flag=0;
          cout<<"是否继续创建银行账户?(Y/N)\n";
          cin>>choice;
          if(choice=='y'||choice=='Y') flag++;
          else  continue;
}
}

int ATM::log_in()                                            //输入账号和密码对应后登录成功,最多尝试输入3次
{
    cout<<"下面请登录您的银行账户"<<endl;
    string account , password;
    ifstream infile_account ("d0.dat",ios::in),infile_password("d1.dat",ios::in);
    int n=3,flag=1,m;
    cout<<"请输入银行账户的账号:";
    cin>>account;
    for(int i=0;i<30 ;++i)                                     // !!!!!!!!!!!!!!!缺少条件
    {
        infile_account>>users[i].account;
        if(account==users[i].account)
        {
            m=i;
            cout<<"检测到此账号存在"<<endl;
            flag--;
            break;

        }
    }
    if(flag==1) cout<<"无此账号",exit(1);
    if(flag==0) cout<<"\n下一步,";

    for(; n>=0;)
    {
        cout<<"请输入密码:";
        cin>>password;
        infile_password>>users[m].password;
         cout<<"m="<<m<<"   此密码:"<<users[m].password<<endl;    //测试方便看密码奇怪的变化。。。
        if(password==users[m].password)
        {
            cout<<"登录成功"<<endl;
            break;
        }
        else
        {
            cout<<"密码错误,你还有"<<n<<"次尝试次数"<<endl;
            n--;
            if(n<0)
            {
                cout<<"次数用尽"<<endl;
                return 0;
            }
        }
    }
}

void ATM::deposit()                                                 //存款
{
    int add;
    cout<<"请输入要存入ATM机的钱数(仅支持人民币的面额为100元,即输入整百的数字):";
    cin>>add;
    sum=sum+add;
}

void ATM::withdrawal()                                              //取款
 {

int add;
    cout<<"请输入要取出ATM机的钱数(仅支持人民币的面额为100元,即输入整百的数字):";
    cin>>add;
    sum=sum-add;
}

void ATM::balance_enquiry()                                        //余额
{
    cout<<"您的余额为:"<<sum<<""<<endl;
}
void ATM::interest()                                                      //利息
{
    int a,income;
    double in,time;
    cout<<"0.返回"<<endl;
    cout<<"1.活期存款-0.35%"<<endl;
    cout<<"2.定期存款三个月-1.10%"<<endl;
    cout<<"3.定期存款半年-1.30%"<<endl;
    cout<<"4.定期存款一年-1.50%"<<endl;
    cout<<"5.定期存款二年-2.10%"<<endl;
    cout<<"6.定期存款三年-2.75%"<<"\n"<<endl;
    cout<<"请选择存款类型"<<endl;
    cin>>a;
    switch(a){
    case 0 :in=0;break;
    case 1: in=0.0035;break;
    case 2: in=0.011,time=0.25;break;
    case 3: in=0.013,time=0.5;break;
    case 4: in=0.015,time=1;break;
    case 5: in=0.021,time=2;break;
    case 6: in=0.0275,time=3;break;
}
cout<<"请输入存入钱数:";
cin>>income;
if(in==0)  ;
else cout<<"可获利息"<<income*in*time<<""<<endl;
}

int main()
{
    ATM a1("1","1","1",'1',"1","1","1",1);
    a1.build();
    a1.log_in();
}

2018-05-17 22:59
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
  infile_password>>users[m].password;
问题大概出在这句
你的账号和密码是分开保存的, 有两个流进行管理
那么账号走过了多少个资料, 你的密码也应该走过同样的数目的资料
我建议你放在一个文件里用 iostream 统一管理
2018-05-18 13:04
牧人马
Rank: 4
等 级:业余侠客
威 望:6
帖 子:49
专家分:229
注 册:2017-12-24
收藏
得分:0 
回复 4楼 Jonny0201
好的,谢谢你,我把这几个变量用一个结构体数组写在一个文件里吧
2018-05-18 15:11
快速回复:C++ ATM机大作业 文件写入报错
数据加载中...
 
   



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

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