c语言编的ATM取款机程序,有个问题请教大家

c语言编的ATM取款机程序,有个问题请教大家,第1张

因为你用的scanf("%ld",&m);所以会把40赋值给m,如果你要判断的话,可以利用

ltoa函数来先将数字转换成对应的字符串,如果不知道这个函数怎么用的自己上网查。

接着判断这个字符串是不是有小数点,可以用函数strchr(str,'');不清楚用法的话也可以上网查。

至于自动返回上层,因为你把所有的步骤都写到了一个函数中,所以这是没有很好的办法回到上一个,虽然有个goto的用法,不过不推荐用,你应该把每个可以分开的步骤都封装成一个函数,然后主函数中来调用这些函数,这样的程序比较整体,而且容易排错。

你可以加一个ini文件处理,就是每次开始程序是都到ini 或者 txr 文件中读取你那个密码 ,在更改时写入你更改的密码到ini 或者 txt 文件中 ,ini的编程很简单 你上网查一下就明白了

#include

#include

int main(void)

{

int i;

int c;

int a[4];

FILE fp;

fp = fopen("D:\\testtxt","r+");

fseek(fp,0,6789);

for (i = 0; i < 4; ++i)

a[i] = fgetc(fp);

for (i = 0; i < 4; ++i)

{

c = a[i];

a[i] = a[4-i];

c[4-i] = c;

fseek(fp,0,6789+i);

fputc(a[i],fp);

}

fclose(fp);

return 0;

}

试试

聪明的话提示下就能做,用switch--case完成界面的各选项,进入第一层界面之后又用switch--case完成第二层界面;到了要取款的时候case几个选项可以是100,200,300,等等····如果要求自己输入的话就添加个SCANF语句要用户自己输入取款金额

能传附件吗?不知道怎么传。我就贴给你吧。

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class account //atm账户类

{

private:

string CardNum,Psw; //卡号、密码

float Balance;

public:

friend class ATM; //atm为友元

/void get_cardnum();

void get_psw();

void get_balance();/

account(){}//<---------------------here

account(string num,string passwd,float balance); //构造函数

void Write();

};

class ATM //atm机类

{

private:

int times;

account ac;

public:

ATM(); void load( account act ); //装入账户信息

void welcome(); //初始欢迎信息

bool check_passwd( string pwd ); //验证当前账户信息的密码

void change_psw(); //修改密码

void get_money(); //取钱

float check_balance(); //查账

void tran(); //转账

void exit(); //退卡

void function_show(); //显示功能菜单

void lock(); //锁定账户

};

/

account::account( string num, string passwd, float balance )

account类构造函数

用途: 使用给定的账号密码等信息初始化一个account对象

参数: string num 账号

string passwd 密码

float balance

/

account::account( string num, string passwd, float balance )

{

CardNum = num;

Psw = passwd;

Balance = balance;

}

//atm类构造函数

/account::get_cardnum()

{

return CardNum;

}

account::get_psw()

{

return Psw;

}

account::get_balance()

{

return Balance;

}/

void account::Write()

{

ofstream outfile("atmtxt",ios::binary);//<-----------------here

outfilewrite((char )(this),sizeof(account));//<-------------here

outfileclose();

}

ATM::ATM()

{

}

/

void ATM::load( account act )

ATM类装入账户信息函数

用途: 载入指定的account对象,模拟atm插卡过程

参数: account act 要载入的account对象指针

/

void ATM::load( account act )

{

ac = act;

}

/

void ATM::welcome()

ATM类显示初始欢迎信息函数

用途: 显示欢迎信息,提示密码输入并验证

参数: 无

/

void ATM::welcome()

{

times = 0; //记录密码输入错误次数

cout << "Welcome to use the China Bank ATM!" << endl;

string pwd; //这一个语句应该上移的,一般来说数据的定义和初始化一块写在开头,下面才是各种 *** 作的语句

while( times < 3 )

{

cout << "Please enter the password: " << endl;

cin >> pwd;

if( !check_passwd( pwd ) )

{

cout << "The password you entered is wrong, please enter again" << endl;

times++;

}

else

{

function_show();

break;

}

}

if( times >= 3 )

lock(); //输入密码错误次数超过(等于)3次,锁定账户

}

bool ATM::check_passwd( string pwd )

{

if( pwd == ac->Psw )

return true;

else

return false;

}

void ATM::function_show()

{

int n;

cout << "(1) Change Password" << endl;

cout << "(2) Get Money" << endl;

cout << "(3) Check Balance" << endl;

cout << "(4) Transfer accounts" << endl;

cout << "(5) Exit" << endl;

cin >> n;

while(n != 1 && n != 2 && n != 3 && n != 4 && n != 5) //这样就可以完全限制用户的输入

{

cout << "Please enter the right number" << endl;

cin >> n;

}

switch( n )

{

case 1:

change_psw();

break;

case 2:

get_money();

break;

case 3:

cout << check_balance() << endl;

break;

case 4:

tran();

break;

case 5:

exit();

break;

}

}

void ATM::lock()

{

cout << "Sorry! Your card has been confiscated!" << endl;

exit();

}

void ATM::change_psw()

{

string old_psw, new_psw1, new_psw2;

int t = 0 ;

while( t < 3 )

{

cout << "Please enter the old password: ";

cin >> old_psw;

if( !check_passwd( old_psw ) )

{

cout << "The password you enter is wrong, please enter again" << endl;

t++;

}

else

{

cout << "Please enter the new password: ";

cin >> new_psw1;

cout << "Please enter the new password again: ";

cin >> new_psw2;

if( new_psw1 == new_psw2 )

{

ac -> Psw = new_psw2;

cout << "You have change your password successfully!" << endl;

break;

}

else

cout << "Sorry, entered passwords do not match! " << endl;

}

}

//}//<----------------------here

if( t >= 3 )

{

cout << "Sorry, you have inputed the wrong password for three times and more! " << endl;

}

}

void ATM::get_money()

{

float money;

cout << "Please enter the amount of money you want to get: " <<endl;

cin >> money;

while( money > ac -> Balance)

{

cout << "Your balance is not enough, please enter again" << endl;

cin >> money;

}

ac -> Balance = ac -> Balance - money;

}

float ATM::check_balance()

{

return ac -> Balance;

}

void ATM::tran()

{

account a[5];

string cn;

float m;

cout<<"please enter the cardnum of the account you want to transfer money to"<<endl;

cin>>cn;

ifstream infile("atmtxt",ios::binary);

infileseekg(0,ios::beg);

for(int i=0;i<5;i++)

{

infileread((char )(&a[i]),sizeof(a[i]));

}

for(int j=0;j<5;j++)

{

if(cn==a[i]CardNum)

{

cout<<"please enter the amount of money"<<endl;

cin>>m;

while(m>ac->Balance)

{

cout<<"there is no enough money in your account,please enter again"<<endl;

cin>>m;

}

ac->Balance=ac->Balance-m;

a[i]Balance=a[i]Balance+m;

ofstream outfile("atmtxt",ios::binary);

outfileseekp(isizeof(a[0]),ios::beg);

outfilewrite((char ) &a[i],sizeof(a[i]));

}

}

if(j>=5)

{

cout<<"the account doesn't exit"<<endl;

}

}

void ATM::exit()

{

cout << "Please take your card!" << endl;

}

int main()

{

account a[5]={account("10001","1111",50000f),account("10002","2222",100000f),account("10003","3333",150000f),

account("10004","4444",200000f),account("10005","5555",250000f)};

account temp( "10001", "1111", 50000f );

ATM atm;

atmload( &temp );

atmwelcome();

return 0;

}

以上就是关于c语言编的ATM取款机程序,有个问题请教大家全部的内容,包括:c语言编的ATM取款机程序,有个问题请教大家、怎么用C语言编写模拟ATM机、万能的百度大神可以用c++帮忙编个程序么,很紧急,谢谢 “模拟ATM机等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9316903.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存