读书人

重载输入运算符gt;gt;引起的有关问题

发布时间: 2012-02-06 15:52:45 作者: rapoo

重载输入运算符>>引起的问题
在做一个ATM模拟程序时,遇到由于重载输入运算符> > 而引起cin异常执行的问题:
//account.h

#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_

/* class account */
class Account
{
public:
friend class Atm;

Account(const string &number, const string &name, double balance, const string &password)
:m_number(number), m_name(name), m_balance(balance), m_password(password)
{
}

Account(const Account &myAccount)
{
m_number = myAccount.m_number;
m_name = myAccount.m_name;
m_balance = myAccount.m_balance;
}

Account():m_number(string( " ")), m_name(string( " ")), m_balance(0.0)
{
}

string GetNumber() const
{
return m_number;
}

string GetName() const
{
return m_name;
}

double GetBalance() const
{
return m_balance;
}

string GetPassword() const
{
return m_password;
}

void ChangePassword(const string &newPassword)
{
m_password = newPassword;
}

friend istream& operator> > (istream&, Account&);

private:
string m_number;
string m_name;
double m_balance;
string m_password;
};


#endif


//atm.h

#ifndef _ATM_H_
#define _ATM_H_

#include <vector>

using namespace std;

#include "account.h "

/* class atm */
class Atm
{
public:
Atm(size_t cash, vector <Account> &accountVec)
:m_cashInAtm(cash), m_accountVec(accountVec)
{

}

Atm(const Atm &copyAtm)
{
m_cashInAtm = copyAtm.m_cashInAtm;
m_accountVec = copyAtm.m_accountVec;
}

Atm(){}

void Welcome();
bool CheckPassword(const string&);
bool CheckNumber(const string&);
void ChangePS(Account &account);
void FetchCash(Account &account);
void SaveMoney(Account &account);
void DispInformation(Account &account);
void Service(Account &account);
void PrintWarrant(Account &account);
void NeedOtherServ(Account &account);
void Lock();
void BackToMenu(Account &account);
void Exit();
void Wait();

private:
string m_passwordToUnlock;
size_t m_cashInAtm;
size_t m_index;
vector <Account> m_accountVec;
};


#endif


//account.cpp

#include <iostream>
#include <string>

using namespace std;

#include "account.h "


istream& operator> > (istream &inAccount, Account &MyAccount)
{
inAccount > > MyAccount.m_number > > MyAccount.m_name > > MyAccount.m_balance > > MyAccount.m_password;


if (!inAccount)
{
MyAccount.m_number = " ";
MyAccount.m_name = " ";
MyAccount.m_balance = 0.0;
MyAccount.m_password = " ";
}
return inAccount;
}

//atm.cpp

#include <iostream>
#include <ctime>
#include <string>

using namespace std;

#include "atm.h "

void Atm::Welcome()
{
string accountNumber;
string accountPassword;

cout < < "欢迎使用Zeejon ATM! " < < endl;
do
{
cout < < "请输入账号: ";
//fflush(stdin);//在此调用fflush或者rewind函数还是不能解决问题
cin > > accountNumber;//此处的cin异常,执行到此,却不显示光标,不能输入。
}while(!CheckNumber(accountNumber));


int count = 0;
do
{
if(++count > 3)
{
Lock();
count = 0;
break;
}

cout < < "请输入密码: ";
cin > > accountPassword;
} while(!CheckPassword(accountPassword));

Service(m_accountVec[m_index]);
}

bool Atm::CheckPassword(const string &password)
{
if (m_accountVec[m_index].GetPassword() != password)
{
cout < < "密码输入错误! " < < endl;
return false;
}
return true;
}

bool Atm::CheckNumber(const string &number)
{
vector <Account> ::iterator iter = m_accountVec.begin();

for (iter; iter != m_accountVec.end(); ++iter)
{
if (iter-> GetNumber() == number)
{
m_index = iter - m_accountVec.begin();
return true;
}
}

if (iter == m_accountVec.end())
{
cout < < "无此账号,请查证后再输入! " < < endl;
}

return false;
}

void Atm::ChangePS(Account &account)
{
do
{
cout < < "\n请输入六位新密码: ";
string newPS, newPS1;
cin > > newPS;
cout < < "请再次输入六位新密码: ";
cin > > newPS1;
if (newPS == newPS1)
{
account.ChangePassword(newPS);
cout < < "密码修改成功,请牢记您的密码! " < < endl;
break;
}
}while(1);

NeedOtherServ(account);
}

void Atm::FetchCash(Account &account)
{
int cash;
do
{
cout < < "\n您的账户余额为: " < < account.GetBalance() < < endl;
cout < < "您可以提取的金额为: " < < m_cashInAtm < < endl;
cout < < "请输入提取金额(以100元为单位): ";
cin > > cash;
}while((cash > account.GetBalance()) || (cash > m_cashInAtm));

account.m_balance -= cash;
Wait();
cout < < "请取卡! " < < endl;


cout < < "请取钱! " < < endl;
cout < < "需要打印凭条吗?是(y)/否(n): ";
char printSign;
cin > > printSign;
if (printSign == 'y ')
{
PrintWarrant(account);
}
else
{
Exit();
}
}

void Atm::SaveMoney(Account &account)
{
cout < < "\n请把纸币理顺后放入读币口! " < < endl;
Wait();
//存储怎么模拟? account.m_balance += ??
cout < < "存储完毕 " < < endl;
NeedOtherServ(account);
}

void Atm::DispInformation(Account &account)
{
cout < < "\n账号: " < < account.GetNumber() < < endl
< < "姓名: " < < account.GetName() < < endl
< < "结余: " < < account.GetBalance() < < endl;
NeedOtherServ(account);
}

void Atm::BackToMenu(Account &account)
{
Service(account);
}

void Atm::Service(Account &account)
{
int serviceNum = 4;
cout < < "\n此ATM选择以下服务: " < < endl;
cout < < "1. 提取现金\n "
< < "2. 现金存储\n "
< < "3. 修改密码\n "
< < "4. 查询结余\n "
< < "5. 退出 " < < endl;
do
{
cout < < "请输入服务编号: ";
cin > > serviceNum;
} while((serviceNum != 1) && (serviceNum != 2) && (serviceNum != 3)
&& (serviceNum != 4) && (serviceNum != 5));

switch(serviceNum)
{
case 1:
FetchCash(account);
break;
case 2:
SaveMoney(account);
break;
case 3:
ChangePS(account);
break;
case 4:
DispInformation(account);
break;
case 5:
Exit();
break;
default:
break;
}
}

void Atm::PrintWarrant(Account &account)
{
time_t lt;
lt =time(NULL);
printf(ctime(&lt));
}

void Atm::NeedOtherServ(Account &account)
{
cout < < "您还需要其他服务吗?是(y)/否(n): ";
char printSign;
cin > > printSign;
if (printSign == 'y ')
{
BackToMenu(account);
}
else
{
Exit();
}
}

void Atm::Exit()
{
cout < < "\n操作完成,谢谢使用! " < < endl;
}

void Atm::Lock()
{
cout < < "密码输入超过三次,你的卡将被吞掉! " < < endl;
}

void Atm::Wait()
{
cout < < "正在处理,请耐心等候………… " < < endl;


}


//main.cpp

#include <iostream>
#include <string>
#include <vector>

using namespace std;

#include "account.h "
#include "atm.h "

void main()
{
vector <Account> accountVec;
Account Zeejon( " ", " ", 0.0, " ");

while (cin > > Zeejon)
{
accountVec.push_back(Zeejon);
}

Atm ChineseAtm(5000, accountVec);

ChineseAtm.Welcome();
}


跟踪Welcome函数,发现在执行此while循环时,
cout < < "欢迎使用Zeejon ATM! " < < endl;
do
{
cout < < "请输入账号: ";
//fflush(stdin);//在此调用fflush或者rewind函数还是不能解决问题
cin > > accountNumber;//此处的cin异常,执行到此,却不显示光标,不能输入。
}while(!CheckNumber(accountNumber));

运行到cin处,继续跟踪,没有进入> > 重载函数,而是进入cin的系统定义中。但却没有正常执行。

请各位大虾多多指教。


[解决办法]
你在cin > > accountNumber;前面加个cin.clear();看看怎么样?或者也可以同时加个cin.sync(),,,,你一开始输入了那account,这里
while (cin > > Zeejon)
{
accountVec.push_back(Zeejon);
}
.....这里退出循环的条件是cin为假..所以在 cin下次输入前要 clear,就是清除错误标志

读书人网 >C++

热点推荐