问题描述
- C++从文件中输出类时重复输出的问题
- #ifndef CLASSDEF_H
#define CLASSDEF_Hclass MoneyData
{
public:
int year;
int month;
int day;
char remark[80];
char PI[40];
char BankAccount[20];
char form[15];
char direction[5];
double Change;
double Cash;
double Saving;
static double TotalCash;
static double TotalSavings;
static double TotalChange;
};#endif
#include
#include
#include
#include""ClassDef.h""
using namespace std;void Inquire(const char *filename)
{
char choice;
MoneyData money;
int m d;
char pi[40];
char bankaccount[20];
cout << "" ●财务信息管理系统●n"";
cout << ""●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●n"";
cout << ""● 账目查询 ●n"";
cout << ""●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●n"";
fstream fdat(filename ios::in);
if (!fdat)
{
cout << ""账目不存在,请进行初始化操作!"" << endl;
return;
}
while (1)
{
fdat.seekg(0 ios::beg);
cout << ""请选择需要查询的信息:"" << endl;
cout << ""1:按月查询t"" << ""2:按日查询t""<< ""3:按订单查询t""<<""4:按银行账号查询t""<<""5:按现金查询t""<<""6:查询总额t"" << ""Q:返回"" << endl;
cin >> choice;
cout << ""====================================================================="" << endl;
switch (choice)
{
case'1':
{
int work = 0;
fdat.seekg(0 ios::beg);
cout << ""请输入月份:"";
cin >> m;
cout << ""====================================================================="" << endl;
do
{
fdat.read((char *)&money sizeof(MoneyData));
if (money.month == m)
{
cout << money.year << ""."" << money.month << ""."" << money.day << ""nPI:""
<< money.PI << ""nForm:"" << money.form << ""nBankaccount:"" << money.BankAccount << ""nDirection:""
<< money.direction << ""nMoney:"" << money.Change << ""nRemark:"" << money.remark << endl;
work = 1;
}
} while (fdat.peek() != EOF); //这里是循环的判定
if (work=0)
{
cout << ""当月没有款项变动"" << endl;
continue;
}
break;
}
case'2':
{
int work = 0;
fdat.seekg(0 ios::beg);
cout << ""请输入月份和日期:"";
cin >> m >> d;
cout << ""====================================================================="" << endl;
do
{
fdat.read((char *)&money sizeof(MoneyData));
if (money.month == m&&money.day==d)
{
cout << money.year << ""."" << money.month << ""."" << money.day << ""nPI:""
<< money.PI << ""nForm:"" << money.form << ""nBankaccount:"" << money.BankAccount << ""nDirection:""
<< money.direction << ""nMoney:"" << money.Change << ""nRemark:"" << money.remark << endl;
work = 1;
}
} while (fdat.peek() != EOF);
if (work = 0)
{
cout << ""当日没有款项变动"" << endl;
continue;
}
break;
}
case'3':
{
int work = 0;
fdat.seekg(0 ios::beg);
cout << ""请输入订单号:"";
cin >> pi;
cout << ""====================================================================="" << endl;
do
{
fdat.read((char *)&money sizeof(MoneyData));
if (!strcmp(money.PI pi))
{
cout << money.year << ""."" << money.month << ""."" << money.day << ""nPI:""
<< money.PI << ""nForm:"" << money.form << ""nBankaccount:"" << money.BankAccount << ""nDirection:""
<< money.direction << ""nMoney:"" << money.Change << ""nRemark:"" << money.remark << endl;
work = 1;
}
} while (fdat.peek() != EOF);
if (work = 0)
{
cout << ""没有该订单相关信息"" << endl;
continue;
}
break;
}
case'4':
{
int work = 0;
fdat.seekg(0 ios::beg);
cout << ""请输入银行账号:"";
cin >> bankaccount;
cout << ""====================================================================="" << endl;
do
{
fdat.read((char *)&money sizeof(MoneyData));
if (!strcmp(money.BankAccount bankaccount))
{
cout << money.year << ""."" << money.month << ""."" << money.day << ""nPI:""
<< money.PI << ""nForm:"" << money.form << ""nBankaccount:"" << money.BankAccount << ""nDirection:""
<< money.direction << ""nMoney:"" << money.Change << ""nRemark:"" << money.remark << endl;
work = 1;
}
} while (fdat.peek() != EOF);
if (work = 0)
{
cout << ""没有该银行账号的存款变动信息"" << endl;
continue;
}
break;
}
case'5':
{
int work = 0;
fdat.seekg(0 ios::beg);
cout << ""现金变动信息如下:"";
do
{
fdat.read((char *)&money sizeof(MoneyData));
if (!strcmp(money.formCash""))
{
cout << money.year << ""."" << money.month << ""."" << money.day << ""nPI:""
<< money.PI << ""nForm:"" << money.form << ""nBankaccount:"" << money.BankAccount << ""nDirection:""
<< money.direction << ""nMoney:"" << money.Change << ""nRemark:"" << money.remark << endl;
work = 1;
}
} while (fdat.peek() != EOF);
if (work = 0)
{
cout << ""没有现金变动信息"" << endl;
continue;
}
break;
}
case'6':
{
fdat.seekg(0 ios::end);
fdat.read((char *)&money sizeof(MoneyData));
cout << ""总现金变动:"" << MoneyData::TotalCash << endl;
cout << ""总存款变动:"" << MoneyData::TotalSavings << endl;
cout << ""总款额变动:"" << MoneyData::TotalChange << endl;
break;
}
case'q':
case'Q':cout << ""返回主菜单"" << endl; return;
default:cout << ""没有该操作选项,请重新输入"" << endl;
}
}
}请各位大神相助!这里按我现在的判定语句是会重复输出的。我尝试过其他的方法判定,例如直接fdat>>...>>...但是这样不知道为什么连正常输出都不行。
请各位高手帮我修改一下,感激不尽!!!!
解决方案
你这样贴代码还不如贴图的方式,这样很难有人看得清楚,而且你也没有把打印的结果贴出来,
很难一下定位出问题的地方在哪里。
其实debug也是程序员的一种综合技能,学会了也能提高你的能力。
具体到你这个程序。你这里的问题是if (work = 0),这样判断无效,所以会重复。
这是对work进行赋值,应该改成if (work == 0)。
望采纳,希望能帮到你。
解决方案二:
debug,然后看判定条件是否正确。你这样直接贴代码,没人会看的