在我的計劃,我得到「堆的腐敗」錯誤腐敗堆
「Windows已經引發了Project_Name.exe。 這可能是一個斷點,由於堆的腐敗,這表明在JPS的錯誤.exe或任何已加載的DLL 這可能也是由於用戶在Project_Name.exe具有焦點時按F12 輸出窗口可能包含更多診斷信息。
這裏是代碼:
Header.h
#pragma once
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <string>
#include <stdio.h>
#include <stack>
#include <vector>
#include <ctime>
#include <cassert>
#include <cmath>
using namespace std;
class ClientInfo
{
public:
int dayM;
string monthM;
int dayL;
string monthL;
string fName, lName;
double itmAmt;
ClientInfo* next;
};
class Record
{
private:
int numOfClients;
public:
int returnNumOfClients();
ClientInfo* head;
Record();
Record* InitializeRecord();
char* CalculateMaturity();
string ViewClient();
Record* FindName(string nn, ClientInfo *xxx);
void SaveRecord(ClientInfo* h);
double ComputeBalance(double amount, string mnthL, int dyL, string mnthM, int dyM);
ClientInfo* AddClient(string fn,string ln,string xMonthL, int xDayL, string
xMonthM, int xDayM ,double xItemAmt);
ClientInfo* SearchClient(string n);
void RemoveItem(ClientInfo* h, ClientInfo* rc, string remfname);
bool CheckExistence(ClientInfo* hx, string fnx, string lnx);
template <typename T>
string numToStr (T num)
{
stringstream ss;
ss << num;
return ss.str();
}
};
Record::Record():numOfClients(0),head(NULL)
{
}
Record* Record::FindName(string nn, ClientInfo * xxx)
{
ClientInfo* newci = new ClientInfo();
Record* newrec = new Record();
ClientInfo* node= xxx;
do{
if(node->lName==nn)
{
newrec->head=newrec->AddClient(node->fName,
node->lName,
node->monthL,
node->dayL,
node->monthM,
node->dayM,node->itmAmt);
}
node=node->next;
}while(node!=NULL);
return newrec;
}
int Record::returnNumOfClients()
{
return numOfClients;
}
void Record::SaveRecord(ClientInfo* h)
{
Record *rec = new Record();
ClientInfo* saveRec = h;
string str;
vector<string> v;
ofstream outF;
outF.open("Record.txt");
if(saveRec!=NULL)
{
do
{
str = saveRec->fName+'-'+saveRec->lName+'-'+
rec->numToStr(saveRec->monthL)+'-'+
rec->numToStr(saveRec->dayL)+'-'+
rec->numToStr(saveRec->monthM)+'-'+
rec->numToStr(saveRec->dayM)+'-'+
rec->numToStr(saveRec->itmAmt);
saveRec = saveRec->next;
v.push_back(str);
}
while(saveRec != NULL);
for(vector<string>::iterator it = v.begin(); it != v.end(); ++it)
{
if(it != v.begin())
outF<<'\n';
outF<< *it;
}
}
else
numOfClients=0;
outF.close();
}
char* Record::CalculateMaturity()
{
try
{
ClientInfo* saveRec = new ClientInfo();
Record* rec = new Record();
string str;
stack<string> month;
month.push("December");
month.push("November");
month.push("October");
month.push("September");
month.push("August");
month.push("July");
month.push("June");
month.push("May");
month.push("April");
month.push("March");
month.push("February");
month.push("January");
month.push("December");
month.push("November");
month.push("October");
month.push("September");
month.push("August");
month.push("July");
month.push("June");
month.push("May");
month.push("April");
month.push("March");
month.push("February");
month.push("January");
stack<string> mnth;
mnth.push("December");
mnth.push("November");
mnth.push("October");
mnth.push("September");
mnth.push("August");
mnth.push("July");
mnth.push("June");
mnth.push("May");
mnth.push("April");
mnth.push("March");
mnth.push("February");
mnth.push("January");
mnth.push("December");
mnth.push("November");
mnth.push("October");
mnth.push("September");
mnth.push("August");
mnth.push("July");
mnth.push("June");
mnth.push("May");
mnth.push("April");
mnth.push("March");
mnth.push("February");
mnth.push("January");
const char *delimeter="-";
char* date=new char;
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%m-%d", &tstruct);
date = buf;
char* token= new char;
vector<string> dt;
token=strtok(date,delimeter);
while(token!=NULL)
{
dt.push_back(token);
token=strtok(NULL,delimeter);
}
string monthL,monthM;
int dayL;
int monthnum;
istringstream(dt[0])>>monthnum;
for(int x=1;x!=monthnum;x++)
mnth.pop();
monthL=mnth.top();
istringstream (dt[1])>>dayL;
do{month.pop();}while(monthL!=month.top());
month.pop();
month.pop();
month.pop();
monthM=month.top();
str=monthL+'-'+
rec->numToStr(dayL)+'-'+
monthM+'-'+
rec->numToStr(dayL);
int strsize = str.size();
char* chr = new char;
for(int x=0;x<strsize;x++)
{
chr[x]=str[x];
}
return chr;
}
catch(char* ex){system("pause");}
}
ClientInfo* Record::AddClient(string fn, string ln,string xMonthL, int xDayL, string
xMonthM, int xDayM, double xItemAmt)
{
ClientInfo* newInfo = new ClientInfo();
Record* newRecord = new Record();
newInfo->fName = fn;
newInfo->lName = ln;
newInfo->dayM = xDayM;
newInfo->monthM = xMonthM;
newInfo->dayL = xDayL;
newInfo->monthL = xMonthL;
newInfo->itmAmt = xItemAmt;
if(newInfo->next!=NULL)
{
newInfo->next = head;
numOfClients++;
head = newInfo;
}
else
{
newInfo->next=NULL;
}
return head;
}
Record* Record::InitializeRecord()
{
Record* initRec = new Record();
ClientInfo* initClient = new ClientInfo();
ifstream inF;
inF.open("Record.txt");
char* rec;
char* token;
vector<string> v;
int dayL, dayM;
string monthL, monthM;
double itmAmt;
if(inF.good())
{
do
{
rec = new char;
token = new char;
inF>>rec;
token = strtok(rec,"-");
while(token!=NULL)
{
v.push_back(token);
token = strtok(NULL,"-");
}
istringstream(v[2])>>monthL;
istringstream(v[3])>>dayL;
istringstream(v[4])>>monthM;
istringstream(v[5])>>dayM;
istringstream(v[6])>>itmAmt;
initRec->AddClient(v[0],v[1],monthL,dayL,monthM,dayM,itmAmt);
v.erase(v.begin(),v.end());
}while(!inF.eof());
}
inF.close();
return initRec;
}
void Record::RemoveItem(ClientInfo* h, ClientInfo* rc, string remfname)
{
ClientInfo* head = h;
ClientInfo* temp = rc->next;
if(temp!=NULL)
{
rc->fName = rc->next->fName;
rc->lName = rc->next->lName;
rc->monthL = rc->next->monthL;
rc->dayL = rc->next->dayL;
rc->monthM = rc->next->monthM;
rc->dayM = rc->next->dayM;
rc->itmAmt = rc->next->itmAmt;
rc->next = temp->next;
numOfClients--;
free(temp);
}
else
{
head = NULL;
free(temp);
}
SaveRecord(head);
}
ClientInfo* Record::SearchClient(string n)
{
ClientInfo* current = head;
for(int x=numOfClients-1;current!=NULL;x--)
{
if(current->lName == n)
return current;
else
current = current->next;
}
return current;
}
string Record::ViewClient()
{
string s;
ClientInfo* itr;
itr = head;
if(numOfClients!=0)
{
for(int i = 0; i<returnNumOfClients();i++)
{
string empty = "";
int y = i+1;
s+="\n\n***************\nName: "+itr->fName+" "+itr->lName+"\n-----
----------\n"+
"Loan Origination Date: "+numToStr(itr->monthL)+"
"+numToStr(itr->dayL)+
"\n---------------\n"+"Date Of Maturity: "+numToStr(itr-
>monthM)+" "+numToStr(itr->dayM)+"\n---------------\n"+
"Balance: "+numToStr(itr->itmAmt);
itr = itr->next;
}
s.append("\n***************\n\n");
}
else
s ="There is no recorded client yet.\n\n";
return s;
}
double Record::ComputeBalance(double amount, string mnthL, int dyL, string mnthM, int
dyM)
{
try
{
double INTEREST = 0.04;
double PENALTY = 0.02;
stack<string> monthl;
monthl.push("December");
monthl.push("November");
monthl.push("October");
monthl.push("September");
monthl.push("August");
monthl.push("July");
monthl.push("June");
monthl.push("May");
monthl.push("April");
monthl.push("March");
monthl.push("February");
monthl.push("January");
monthl.push("December");
monthl.push("November");
monthl.push("October");
monthl.push("September");
monthl.push("August");
monthl.push("July");
monthl.push("June");
monthl.push("May");
monthl.push("April");
monthl.push("March");
monthl.push("February");
monthl.push("January");
stack<string> monthm;
monthm.push("December");
monthm.push("November");
monthm.push("October");
monthm.push("September");
monthm.push("August");
monthm.push("July");
monthm.push("June");
monthm.push("May");
monthm.push("April");
monthm.push("March");
monthm.push("February");
monthm.push("January");
monthm.push("December");
monthm.push("November");
monthm.push("October");
monthm.push("September");
monthm.push("August");
monthm.push("July");
monthm.push("June");
monthm.push("May");
monthm.push("April");
monthm.push("March");
monthm.push("February");
monthm.push("January");
int ml=0;
int mm=0;
do{
monthl.pop();
mm++;
}while(mnthL!=monthl.top());
do
{
monthm.pop();
ml++;
}while(mnthM!=monthm.top());
int diff=abs(mm-ml);
double balance=diff*(amount*(INTEREST+
(INTEREST/(pow(INTEREST+1,diff)-1))));
return balance;
}
catch(double ex){system("pause");}
}
bool Record::CheckExistence(ClientInfo* hx, string fnx, string lnx)
{
ClientInfo *hh;
hh = head;
int t=0;
for(int x=numOfClients; hh!=NULL && x!=0; x--)
{
if(hh->fName == fnx && hh->lName==lnx)
t++;
else
hh=hh->next;
}
if(t!=0)
return true;
else
return false;
}
Main.cpp的
#include "Header.h"
int main()
{
Record* rec = new Record();
ClientInfo* info;
Record* samelastrec = new Record();
double amt;
double balance;
int number;
int choose;
char ch;
double principal;
string iLName,iFName;
string nn;
int iDayL,iDayM;
string iMonthL,iMonthM;
string delfname;
rec=rec->InitializeRecord();
again:
system("cls");
cout<<"WELCOME TO JEWELRY PAWNING SYSTEM."<<endl;
cout<<"\n(1)Add Client\n(2)Remove Client\n(3)Search Client\n\n->";
cin>>choose;
Record *find = new Record();
ClientInfo* found;
system("cls");
int search;
char* datelm;
char* token;
vector<string> date;
string nnx;
switch(choose)
{
case 1:
system("cls");
if(rec->returnNumOfClients()!=0 || rec->returnNumOfClients()!=NULL)
{
cout<<"First Name: ";
cin>>iFName;
cout<<"Last name: ";
cin>>iLName;
if(!rec->CheckExistence(rec->head,iFName,iLName))
{
cout<<"Amount: ";
cin>>amt;
datelm=rec->CalculateMaturity();
token=strtok(datelm, "-");
while(token!=NULL)
{
date.push_back(token);
token=strtok(NULL, "-");
}
istringstream(date[0])>>iMonthL;
istringstream(date[1])>>iDayL;
istringstream(date[2])>>iMonthM;
istringstream(date[3])>>iDayM;
balance=rec->ComputeBalance(amt,iMonthL,iDayL,iMonthM,iDayM);
istringstream(rec->numToStr(balance));
rec->head=rec>AddClient(iFName,iLName,iMonthL,iDayL,iMonthM,iDayM,balance);
system("pause");
}
else
{
cout<<"Member already exist!"<<endl;
system("pause");
}
}
else
{
cout<<"First Name: ";
cin>>iFName;
cout<<"Last name: ";
cin>>iLName;
cout<<"Amount: ";
cin>>amt;
datelm=rec->CalculateMaturity();
token=strtok(datelm, "-");
while(token!=NULL)
{
date.push_back(token);
token=strtok(NULL, "-");
}
istringstream(date[0])>>iMonthL;
istringstream(date[1])>>iDayL;
istringstream(date[2])>>iMonthM;
istringstream(date[3])>>iDayM;
balance=rec->ComputeBalance(amt,iMonthL,iDayL,iMonthM,iDayM);
istringstream(rec->numToStr(balance));
rec->head=rec>AddClient(iFName,iLName,iMonthL,iDayL,iMonthM,iDayM,balance);
system("pause");
}
rec->SaveRecord(rec->head);
goto again;
break;
case 2:
system("cls");
find->head = rec->head;
cout<<"Enter Last Name: ";
cin>>iLName;
samelastrec=new Record();
samelastrec=samelastrec->FindName(iLName, rec->head);
if(find->SearchClient(iLName) == NULL)
{
cout<<"Client not found.\n";
system("pause");
}
else
{
found = find->SearchClient(iLName);
if(samelastrec->returnNumOfClients()>1)
{
cout<<samelastrec->ViewClient()<<endl<<endl;
cout<<"There are "<<samelastrec->numToStr(samelastrec->returnNumOfClients())
<<" Clients with "<<samelastrec->head->lName<<" last name";
cout<<endl<<"Enter the first name of the client you want to
remove: ";
cin>>delfname;
system("cls");
if(samelastrec->CheckExistence(samelastrec->head,delfname,
iLName))
{
cout<<endl<<"Do you want to remove "<<delfname<<"
<<samelastrec->head->lName<<" ?(Y/N)";
cin>>ch;
if(ch=='Y' || ch=='y')
{
rec->RemoveItem(rec->head,found,delfname);
cout<<"Client Removed."<<endl;
system("cls");
}
else if(ch=='N' || ch=='n')
goto again;
}
else
{
cout<<"The member with "<<delfname<<" does not
exist."<<endl;
system("pause");
goto again;
}
system("pause");
}
else
{
cout<<endl<<samelastrec->ViewClient()<<endl;
cout<<"Remove Client? [Y/N]: ";
cin>>ch;
if(ch=='Y' || ch=='y')
{
system("cls");
rec->RemoveItem(rec->head,found,found->fName);
cout<<"Client Removed."<<endl;
system("pause");
}
else if(ch=='N' || ch=='n')
goto again;
}
}
goto again;
break;
case 3:
system("cls");
cout<<"Enter Last Name : ";
cin>>nnx;
samelastrec=new Record();
samelastrec=samelastrec->FindName(nnx, rec->head);
cout<<samelastrec->ViewClient()<<endl;
system("pause");
break;
case 4:
system("cls");
cout<<rec->ViewClient();
system("pause");
goto again;
break;
case 5:
cout<<"Number of clients: "<<rec->numToStr(rec->returnNumOfClients())
<<endl;
system("pause");
break;
default:
if(choose!=0)
{
cout<<"\n\nInvalid Entry.\n\n";
system("pause");
goto again;
}
else
{
cout<<"\n\nTHANK YOU FOR USING THE JEWELRY PAWNING SYSTEM!\n\n";
system("pause");
exit(1);
}
}
goto again;
system("pause");
return 0;
}
我想不通爲什麼我得到這些錯誤。
請幫我調試這個錯誤。
謝謝!
您需要做的第一件事就是使用一個調試器並告訴我們*發生了什麼。 – chris
我注意到你也使用'goto'。在代碼中跳轉通常是不需要的,因爲它根本不是結構化的。閱讀,維護和調試使用while循環而不是goto的代碼更容易。 – chris
Record :: InitializeRecord()'有一些令人討厭的業務正在進行,包括一個永遠不會被返回或刪除的新客戶端。並且使用'[]'訪問一個矢量而不檢查矢量中有多少個元素。我猜你的問題在那裏。 – tmpearce