1 Replies - 797 Views - Last Post: 31 July 2009 - 11:10 PM Rate Topic: -----

#1 cbebop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-July 09

How can dynamic memory be stored into a binary file to read and write

Post icon  Posted 31 July 2009 - 05:45 PM

If you look at my program as is, the majority of tasks need to involve reading and writing to files. This program dynamically creates 100 customer objects, and would like to know that content can be placed into a binary file so a customer can search for their name (myAccount pointer then points to the object in this file), and manipulate amount, account type, etc...

 #include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

//classes, functions, prototypes
const int SIZE = 100;
//return for each function?
enum AccountType {
unknown = -1, Checking, Savings, CreditCard, InstantAccess
};
class customerInfo
{
char name[SIZE];
float amount;
AccountType type;
//enum, functions other than main
//customerInfo();
//~customerInfo();
};
//customerInfo *myAccount = new customerInfo[SIZE]; declare customer array
//(myaccount pointing to first address)
// myAccount++ to next. INCLUDE delete [] myAccount;

void menuCust ()
{
char choice;
bool terminate = false;

while(!terminate)
{
cout << "Customer Services: Please enter an option\n"
<< "1) Balance Inquiry\n"
<< "2) Deposit Funds\n"
<< "3) Withdraw Funds\n"
<< "4) Transfer Funds to another account\n"
//<< "5) Anything else that you want to add\n"
<< "5) Return to main menu\n\n";

cin >> choice;

switch (choice)
{
case '1':

break;

case '2':

break;

case '3':

break;

case '4':

break;

case '5':

cout << "You have chosen to quit. We appreciate your business.\n\n";
terminate = true;

break;

default:

cout << "You have entered an invalid option, please try again\n";

break;
}
}
/*
** myAccount declared in 'main' scope not in this scope, just return
delete [] myAccount;
exit(1);
*/
return;
}
void menuBank ()
{
char choice;
bool terminate = false;

while(!terminate)
{
//<< "1) Everything in the Customer Interface, plus
	cout << "Banking Services: Please enter an option: \n"
<< "1) Add a new Customer to the Bank\n"
<< "2) Delete a Customer from the Bank\n"
<< "3) Search for Customer Record\n"
<< "4) Return to main menu\n\n";

cin >> choice;

switch (choice)
{
case '1':

break;

case '2':

break;

case '3':

break;

case '4':

cout << "You have chosen to quit. We appreciate your business.\n\n";
terminate = true;

break;

default:

cout << "You have entered an invalid option, please try again\n";

break;
}
}
/*
** myAccount declared in 'main' scope not in this scope, just return
delete [] myAccount;
exit(1);
*/
return;
}
void menuSuper ()
{
char choice;
bool terminate = false;

while(!terminate)
{
//a.Everything in Bank Teller Interface, plus
cout << "Supervising: Please enter an option\n"
<< "1) List Total Number of Customers in Bank\n"
<< "2) List Total Amount of Money in the Bank\n"
<< "3) List Total Deposits in a Day\n"
<< "4) List Total Withdrawals in a Day\n"
<< "5) View detailed log of all transactions\n"
<< "6) Return to main menu\n\n";

cin >> choice;

switch (choice)
{
case '1':

break;

case '2':

break;

case '3':

break;

case '4':

break;

case '5':

break;

case '6':

cout << "You have chosen to quit. We appreciate your business.\n\n";
terminate = true;

break;

default:

cout << "You have entered an invalid option, please try again\n";

break;
}
}
/*
** myAccount declared in 'main' scope not in this scope, just return
delete [] myAccount;
exit(1);
*/
return;
}

int main ()
{
	char choice;
	bool terminate = false;

	customerInfo *myAccount = new customerInfo[SIZE]; //declare customer array (myaccount pointing to first address)
	
	while (!terminate)
	{

				cout << "WELCOME TO SO-AND-SO BANK!\n\n" << "Please enter an option, are you a:\n\n"
				<< "A)Customer\n"
				<< "B)Bank Employee\n"
				<< "C)Bank Supervisor\n"
				<< "D)Quit the application\n\n";
				cin >> choice;

				switch(choice)
				{
				case 'a':
				case 'A':
					menuCust();
				
				break;

				case 'b':
				case 'B':
					menuBank();
				
				break;

				case 'c':
				case 'C':
					menuSuper();

				break;

				case 'd':
				case 'D':
				cout << "You have chosen to quit. We appreciate your business.\n\n";
				terminate = true;
				break;

				default:
				cout << "You have entered an invalid option, please try again\n";

				break;
				
				}
	}

	/*
	cout << "How many numbers would you like to type? ";
	cin >> i;
  
	p= new (nothrow) int[i];
	if (p == 0)
	cout << "Error: memory could not be allocated";
	else
	{
		for (n=0; n<i; n++)
		{
			cout << "Enter number: ";
			cin >> p[n];
		}
		cout << "You have entered: ";
		for (n=0; n<i; n++)
		cout << p[n] << ", ";
		delete[] p;
	}
	//above
	*/
	delete [] myAccount;
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How can dynamic memory be stored into a binary file to read and write

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How can dynamic memory be stored into a binary file to read and write

Posted 31 July 2009 - 11:10 PM

View Postcbebop, on 31 Jul, 2009 - 11:45 PM, said:

If you look at my program as is, the majority of tasks need to involve reading and writing to files. This program dynamically creates 100 customer objects, and would like to know that content can be placed into a binary file so a customer can search for their name (myAccount pointer then points to the object in this file), and manipulate amount, account type, etc...

have a look at using binary files in the following tutorial
http://www.cplusplus...tutorial/files/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1