Member 12697792 Ответов: 1

Могу ли я получить помощь в отладке? Когда выбран вариант 5, он дает бесконечный список чисел. И я, кажется, не могу найти ошибку.. Улучшения приветствуются


#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include<conio.h>
#include<cstdlib>

using namespace std;

void Intro()
{
    cout<<"\n\n\n\t            WELCOME TO BANK SMART ";
    cout<<"\n\n\n\t ************* your no.1 bank...**************";
    cout<<"\n\n\n\t  www.banksmart.com     Toll free line: 002677241277  ";
    cout<<"\n\n\n\t Please Press enter to proceed";
	cin.get();
}

class account
{
	int accno;
	char name[50];
	int deposit;
	char type;
public:
	void create_account();
	void show_account() const;
	void modify();
	void dep(int);
	void draw(int);
	void report() const;
	int retacno() const;
	int retdeposit() const;
	char rettype() const;
};

void account::create_account()
{
	cout<<"Please Enter your account number. :"<<endl;
	cin>>accno;
	cout<<"Enter the Account Holders name : "<< endl;
	cin.ignore();
	cin.getline(name,50);
	cout<<"\nChoose account type (Current(C)/Savings(S) : " <<endl;
	cin>>type;
	type=toupper(type);
	cout<<"\nEnter initial deposit (>P250): "<<endl;
	cin>>deposit;
	cout<<"\n\nDear Client, you account has been successfully created";

}

void account::show_account() const
{
	cout<<"\nAccount No. : "<<accno;
	cout<<"\nAccount Holder Name : ";
	cout<<name;
	cout<<"\nType of Account : "<<type;
	cout<<"\nBalance amount : "<<deposit;
}


void account::modify()
{
	cout<<"\nAccount No. : "<<accno;
	cout<<"\nChange Account Holder's Name : ";
	cin.ignore();
	cin.getline(name,50);
	cout<<"\nChange Account type : ";
	cin>>type;
	type=toupper(type);
	cout<<"\nModify Balance amount : ";
	cin>>deposit;
}


void account::dep(int a)
{
	deposit+=a;
}

void account::draw(int a)
{
	deposit-=a;
}

void account::report() const
{
	cout<<accno<<setw(10)<<" "<<name<<setw(10)<<" "<<type<<setw(6)<<deposit<<endl;
}


int account::retacno() const
{
	return accno;
}

int account::retdeposit() const
{
	return deposit;
}

char account::rettype() const
{
	return type;
}

void write_acc();
void display_sp(int);
void modify_acc(int);
void delete_acc(int);
void show_all();
void deposit_withdraw(int, int);
void intro();

int main()
{
	char ch;
	int num;
	Intro();
	do
	{
        system("cls");
		cout<<"\n\n\n\tMAIN MENU";
		cout<<"\n\n\t1. CREATE A NEW ACCOUNT";
		cout<<"\n\n\t2. DEPOSITS";
		cout<<"\n\n\t3. WITHDRAWALS";
		cout<<"\n\n\t4. BALANCE ENQUIRY";
		cout<<"\n\n\t5. ALL ACCOUNT HOLDER LIST";
		cout<<"\n\n\t6. CLOSE ACCOUNT";
		cout<<"\n\n\t7. CHANGE ACCOUNT DETAILS";
		cout<<"\n\n\t8. EXIT";
		cout<<"\n\n\Please choose an option (1-8) "<<endl;
		cin>>ch;


		switch(ch)
		{
		case '1':
			write_acc();
			break;
		case '2':
			cout<<"\n\n\tPlease Enter The account No. : "; cin>>num;
			deposit_withdraw(num, 1);
			break;
		case '3':
			cout<<"\n\n\tPlease Enter The account No. : "; cin>>num;
			deposit_withdraw(num, 2);
			break;
		case '4':
			cout<<"\n\n\tPlease Enter The account No. : "; cin>>num;
			display_sp(num);
			break;
		case '5':
			show_all();
			break;
		case '6':
			cout<<"\n\n\tEnter The account No. : "; cin>>num;
			delete_acc(num);
			break;
		 case '7':
			cout<<"\n\n\tEnter The account No. : "; cin>>num;
			modify_acc(num);
			break;
		 case '8':
			cout<<"\n\n\tThank You For Choosing Bank Smart";
			break;
		 default :cout<<"\a";
		}
		cin.ignore();
		cin.get();
	}while(ch!='8');

     return 0;
}


void write_acc()
{
	account ac;
	ofstream outFile;
	outFile.open("account.dat",ios::binary|ios::app);
	ac.create_account();
	outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
	outFile.close();
}


void display_sp(int a)
{
	account ac;
	bool flag=false;
	ifstream inFile;
	inFile.open("account.dat",ios::binary);
	if(!inFile)
	{
		cout<<"File could not open !! Press any Key...";
		return;
	}
	cout<<"\nBALANCE DETAILS\n";

    	while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
	{
		if(ac.retacno()==a)
		{
			ac.show_account();
			flag=true;
		}
	}
	inFile.close();
	if(flag==false)
		cout<<"\n\nAccount number does not exist";
}


void modify_acc(int a)
{
	bool found=false;
	account ac;
	fstream File;
	File.open("account.dat",ios::binary|ios::in|ios::out);
	if(!File)
	{
		cout<<"File could not be open !! Press any Key...";
		return;
	}
	while(!File.eof() && found==false)
	{
		File.read(reinterpret_cast<char *> (&ac), sizeof(account));
		if(ac.retacno()==a)
		{
			ac.show_account();
			cout<<"\n\nEnter The New Details of account"<<endl;
			ac.modify();
			int pos=(-1)*static_cast<int>(sizeof(account));
			File.seekp(pos,ios::cur);
			File.write(reinterpret_cast<char *> (&ac), sizeof(account));
			cout<<"\n\n\t Record Updated";
			found=true;
		  }
	}
	File.close();
	if(found==false)
		cout<<"\n\n Account Does not Exist ";
}


void delete_acc(int a)
{
	account ac;
	ifstream inFile;
	ofstream outFile;
	inFile.open("account.dat",ios::binary);
	if(!inFile)
	{
		cout<<"An error was encountered, Please call our toll free line for assistance";
		return;
	}
	outFile.open("Temp.dat",ios::binary);
	inFile.seekg(0,ios::beg);
	while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
	{
		if(ac.retacno()!=a)
		{
			outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
		}
	}
	inFile.close();
	outFile.close();
	remove("account.dat");
	rename("Temp.dat","account.dat");
	cout<<"\n\n\tAccount successfully deleted";
}

void show_all()
{
	account ac;
	ifstream inFile;
	inFile.open("account.dat",ios::binary);
	if(!inFile)
	{
		cout<<"An error occurred, Please contact our toll free line for assistance";
		return;
	}
	cout<<"\n\n\t\tACCOUNT DETAILS\n\n";
	cout<<"----------------------------------------------------\n";
	cout<<"A/c no.      NAME           Type  Balance\n";
	cout<<"----------------------------------------------------\n";
	while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
	{
		ac.report();
	}
	inFile.close();
}

void deposit_withdraw(int a, int option)
{
	int amt;
	bool found=false;
	account ac;
	fstream File;
	File.open("account.dat", ios::binary|ios::in|ios::out);
	if(!File)
	{
		cout<<"Error !! Press any Key...";
		return;
	}
	while(!File.eof() && found==false)
	{
		File.read(reinterpret_cast<char *> (&ac), sizeof(account));
		if(ac.retacno()==a)
		{
			ac.show_account();
			if(option==1)
			{
				cout<<"\n\nEnter The Amount To Be Deposited";
				cin>>amt;
				ac.dep(amt);
			}
			if(option==2)
			{
				cout<<"\n\nEnter The Amount To Be Withdrawn";
				cin>>amt;
				int bal=ac.retdeposit()-amt;
				if((bal<250 && ac.rettype()=='S') || (bal<250 && ac.rettype()=='C'))
					cout<<"Insufficient balance";
				else
					ac.draw(amt);
			}
			int pos=(-1)*static_cast<int>(sizeof(ac));
			File.seekp(pos,ios::cur);
			File.write(reinterpret_cast<char *> (&ac), sizeof(account));
			cout<<"\n\n\t Transaction successful";
			found=true;
	       }
         }
	File.close();
	if(found==false)
		cout<<"\n\n Record Not Found ";


Что я уже пробовал:

Я попытался найти ошибку в исходном fns, но, похоже, не могу точно определить, в чем именно заключается проблема

1 Ответов

Рейтинг:
2

CPallini

После исправления тривиальной ошибки ("\P"->"\tP") и убрали устаревшие conio.h включение заголовка, я скомпилировал ваш код с помощью g++ 5.3.1 и успешно его запустил.
Выходной образец:

		ACCOUNT DETAILS

----------------------------------------------------
A/c no.      NAME           Type  Balance
----------------------------------------------------
12          foo          C   500


0x01AA

А 5.

CPallini

Спасибо.