Google Answers Logo
View Question
 
Q: C++ PROGRAMMING ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: C++ PROGRAMMING
Category: Computers > Programming
Asked by: integrated-ga
List Price: $150.00
Posted: 26 Feb 2004 07:32 PST
Expires: 27 Mar 2004 07:32 PST
Question ID: 310995
(THIS QUESTION IS FOR MANIAC-GA)

HI MANIAC I HAVE SEND A PROGRAM WHICH COMPILES PERFECTLY IN MY BORLAND
3.1 COMPILER ALL THAT THIS PROGRAM NEEDS IS TO BE MODIFIED AND CHANGED
TO THE ACCOUNT PROGRAM. IN THIS CASE IT WILL BE ALOT MORE EASIER TO
COMPILE.ALL IT NEEDS IS FOR YOU TO ADD SOME NEW FUNCTIONS.

NOTE:

1.) MANIAC YOU HAVE USED SO MANY ADVANCED CODES IN YOUR LAST PROGRAM
THAT I COULDN'T UNDERSTAND THE MEANING OR THE PURPOSE OF SOME CODES,
AS IAM A BEGINNER IN C++ PLEASE USE SOME RECONISEABLE CODES, SO WRITE
THIS PROGRAM KEEPING IN MIND THAT A BEGINNER IS LEARNING FROM IT.

2.) PLEASE ALSO COMMENT ON EVERY LINE OF CODE.

3.) 

HERE IS THE PROGRAM.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <conio.h>

struct Details
{
	char name[30];
	char sex;
	int age;
};

//** Prototypes for User-Defined functions
void MainMenu (void);
void ReadDetails (Details&);
void AppenRec (Details&);
void DispSingleRec (Details&);
void ModifyRec (Details&);
void DeleteRec (Details&);

void MainMenu (void)
{
	clrscr();
	cout << "Choose one of the following options\n ";
	cout << "\t1. Append records\n";
	cout << "\t2. Display single record\n";
	cout << "\t3. Display all records\n";
	cout << "\t4. Modify records \n";
	cout << "\t5. Deleting records\n";
	cout << "\t6. Exit program\n";
	cout << "What is your choice? ";
	return;			// Return to main()
}

void AppendRec (void) //writing records to a Binary fikle
{
	FILE *testfile;
	Details det;

	testfile = fopen ("f:\L13Xmp.dat", "ab");

	if (!testfile)	// if file not found, error message
	{
		cout <<"Error opening file\n";
		exit (1);
	}
	ReadDetails(det);
	while(strcmp (det.name,"") )
	{
		fwrite(&det, sizeof (det),1,testfile);
		ReadDetails(det);	//Read more details from keyboard
	}
	fclose(testfile);		// close file
}

void ReadDetails (Details& p)
{
	clrscr();
	cout << "Enter name :";
	gets (p.name);
	if (!strcmp (p.name,"") )
		return;
	cout <<"Enter sex :";
	cin >>p.sex;
	cout <<"Enter age :";
	cin >>p.age;
}

void ExitProg()
{
	clrscr();
	cout <<"GOOD BYE ";
	getche();
}

void DispAllRecs()
{
	FILE *testfile;
	Details p;

	testfile = fopen("f:\L13Xmp.DAT", "rb");	//Open file
	if (!testfile)	// if file not found, error message
	{
		cout <<"Error opening file\n";
		exit (1);
	}
	clrscr();
	cout <<"THE FILE CONTAINS THE FOLLOWING DATA \n\n";
	while (fread (&p,sizeof (p), 1, testfile) ==1)
	{
		cout<<p.name <<' '<<p.sex<<' '<<p.age <<endl;
	}
	cout <<"End of file reached \n";
	fclose(testfile);
	getch ();
	MainMenu ();
}

void DispSingleRec()
{
	FILE *testfile;
	Details p;
	testfile =fopen ("F:\L13Xmp.DAT", "rb");
	if (!testfile)	// if file not found, error message
	{
		cout <<"Error opening file\n";
		exit (1);
	}

	clrscr ();
	cout <<"ENTER RECORD REQUIRED : ";
	int post;
	cin >> post;
	while (post!=-1)
	{
		fseek(testfile,sizeof(p) * (post -1), 0);
		if (fread (&p,sizeof(p),1,testfile)==1)
		{
		   cout <<p.name<<' '<<p.sex<<' ' <<p.age <<endl;
		   getch();
		}
		else
      {
		   cout <<"invalid record number, press any key\n";
		   getch();
		}
		cout <<"Enter record required : ";
		cin >>post;
	}
	cout <<"Program terminated\n";

	fclose (testfile);
	getch();
	MainMenu();
}
void ModifyRec()
{
	FILE *testfile;
	Details p;
	testfile =fopen ("f: \L13Xmp.DAT", "r+b");
	if (!testfile)	// if file not found, error message

	{
		cout <<"Error opening file\n";
		exit(1);
	}
	clrscr();

	// Calculate how many records are in the file
	fseek (testfile,0,2);
	int noofrecs=ftell (testfile)/ sizeof(p);
	cout <<"The first file contains "<<noofrecs<<" records\n";
	fseek(testfile,0,0);

	// Read record position
	cout <<"ENTER RECORD REQUIRED ( -1 TO STOP): ";
	int post;
	cin >>post;
	while (post!=-1)
	{
		fseek(testfile, sizeof(p) * (post -1) ,0);
		if (fread(&p,sizeof (p), 1, testfile)==1)
		{
			cout <<p.name <<' '<<p.sex<<' '<<p.age <<endl;
		   cout <<"Enter new name :";
		   gets(p.name);

			// Move file pointer to record just read
			fseek(testfile, (post-1) *sizeof(p),0);

			// Rewrite the record to the file
			fwrite (&p,sizeof(p), 1, testfile);
		}
		else	// Display error message
		{
			cout <<"invalod record number, press any key\n";
		   getch();
		}
		cout <<"Enter record required  ( -1 to stop) :";
		cin >>post;

	}
	cout <<"Program terminated\n";
	fclose (testfile);
	getch();
	MainMenu;
}
void DeleteRec ()
{
	clrscr();
	cout <<"Function not yet implemented\n ";
	cout <<"Press Enter key to proceed\n";
	getch();
	MainMenu();
}

main()
{
	int choice;
	do
	{
		clrscr();

		MainMenu ();
		cin >>choice;
	}
	while ((choice <1) || (choice >6));

	switch (choice)
	{

		case 1:
			AppendRec();	//Function all to append records
			break;
		case 2:
			DispSingleRec();
			break;
		case 3:
			DispAllRecs();
			break;
		case 4:
			ModifyRec();
			break;
		case 5:
			DeleteRec :
			break;
		case 6:
			ExitProg ();
         break;
	}
	return 0;
}

Request for Question Clarification by maniac-ga on 01 Mar 2004 19:44 PST
Hello Integrated,

I have taken a look at the program you provided but am unclear how to proceed.

[1] You use #include <conio.h> which is not defined on my system. I
have a library that appears to implement this but must test it before
I proceed further.

[2] I found a few compilation errors, most related to the file names.
A few others may be a cut / paste problem. I can summarize those if
needed.

[3] You refer to the "account program". Do you want the answer in
  http://answers.google.com/answers/threadview?id=304408
recoded to match this style / use of files or something else?

[4] Do you really want comments on every line or would a block of
comments leading every few lines be sufficient? Also - if I use the
same approach several times, would it be acceptable to describe the
approach once in detail and make a short comment in subsequent uses?

  --Maniac

Clarification of Question by integrated-ga on 02 Mar 2004 07:48 PST
hi maniac-

1.)yes that is fine with me test it and proceed.

2.) ok i understood 

3.) thats right maniac the main thing is to match the type of style
used in this code, thats what i been lookin for, and match the use of
files aswell if possible.

4.) it doesnt have to be every line, it came out wrong, once you have
commented on a line if it appears again then it doesnt need to be
commented on.

thanks integrated

Clarification of Question by integrated-ga on 03 Mar 2004 12:43 PST
hi maniac,
          i hope my clarification have made things more clarified let me know.


            thanks integrated
Answer  
Subject: Re: C++ PROGRAMMING
Answered By: maniac-ga on 03 Mar 2004 18:31 PST
Rated:5 out of 5 stars
 
Hello Integrated,

Sorry for the delay - I had to overcome problems building the conio
library I found. I ended up using just clrscr(); if you want to use
getch, there should just be one place to make the change. See note [3]
below for details.

The revised program is at the end. It follows the example you provided
for format and coding style. I also added a number of comments, at the
beginning of each function as well as inline.

Let me summarize some key design points on this version:

[1] Extensive comment included for the data declarations. All major
variables are now globals instead of local to the main program.

[2] All functions have prototypes near the top.

[3] Function Keypress takes the place of getch. It operates a little
differently because it reads and discards input until the user enters
a newline. Replacing
  cin.ignore(99,'\n');
with
  getch();
should have the same effect as you expect. There MAY be some
exceptions, I cannot test it on my system.

For an explanation of this version, cin.ignore(99, "\n") will read up
to the next newline (up to 99 characters).

[4] Each function that reads from cin now does directly into each
variable. This is does require the user to enter the values properly
(the previous program would handle invalid numbers much better).

[5] The formatting in Display uses the defaults for each type of data.
If you want formatted output (to match the headers), it is necessary
to replace the current cout statement with something like:
  cout.width(16);
  cout << customers[i].name << "  ";
  cout.width(12);
  cout << customers[i].accNo << "  ";
and so on. The use of cout.width forces the width of the next string
or numeric output to the value specified. At the end, use
cout.width(0) to go back to the default format (no limit on width).

[6] Both Deposit and Withdrawl check to see that the value "makes
sense" - negative values and excessive withdrawls are ignored.

[7] Both Calculate and the main program are very similar to that used
previously - mostly added comments to explain how they work.

Let me know if there are ANY problems compiling this file. You should
not have any problems based on the methods I used but I cannot test
for sure. I will be glad to make suggestions on fixes as needed. If
there is any other question related to how this works or how you can
improve it, le me know as well.

  --Maniac

// ktc2.cpp - written by Maniac on 2004/03/03
// A translation of Pascal source code provided by integrated-ga
// http://answers.google.com/answers/threadview?id=304408
// rewritten to match the style provided by integrated-ga
// http://answers.google.com/answers/threadview?id=310995

// header files

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <conio.h>

// accRec -- Account record definition:
//  name -- 16 character array with the person's name
//  accNo - 12 character array with the account number
//  accType - one character representing the type of account
//    (C = Checking, S = Savings)
//  amount - floating point value representing the amount of
//    money in the account. Double precision to allow large
//    values without loss of precision.
//  newBal - floating point value representing the new balance
//    in the account (after calculations).
//  NOTE: If you change the length of the name or account
//  number, make sure that dependent code is fixed.

struct accRec {
  char name[16];
  char accNo[12];
  char accType;
  double amount;
  double newBal;
};

// Global variables
//  customers - array of up to 20 customers
//  listEnd - last customer index in use, initial value
//   indicates no customers have been entered.

struct accRec customers[20];
int listEnd = -1;

// Prototypes for user defined functions:
//  MainMenu - prompt the user for main selection
//  

void Keypress (void);
void MainMenu (void);
void Input (void);
void Display (void);
void Deposit (void);
void Withdrawl (void);
void Calculate (void);

// ************************************************************
// Keypress - read to end of line
//  Used after inputs to purge input buffer. Used after
//  output to wait for user entry before continuing.
// ************************************************************

void Keypress() {
  cin.ignore(99,'\n');
}

// ************************************************************
//  MainMenu - prompt the user for main selection
// ************************************************************

void MainMenu (void)
{
  cout << "Choose from one of the following options:\n";
  cout << "\t1. Input customers details\n";
  cout << "\t2. Display accounts details\n";
  cout << "\t3. Customer deposit\n";
  cout << "\t4. Customer withdrawl\n";
  cout << "\t5. Calculate\n";
  cout << "\t6. EXIT\n";
  cout << "What is your choice? ";
  return;                 // Return to main()
}

// ************************************************************
//  Input - add records to active customer list
//   Loop prompts for customer information. Loop exits
//   if customer table is full or user chooses to stop
//   entering new customers.
// ************************************************************

void Input (void) {

  char choice; // Y or N to continue customer input

  do {
    if (listEnd+1 >= 20) {
      cout << "Customer table is full.\n";
      Keypress();
      break;  // exits the loop
    }

    listEnd++;  // move to next customer record
    clrscr();
    cout << "Enter Customer Details\n";
    cout << "======================\n";
    // enter string value for account name
    cout << "Account holder name ======> ";
    cin >> customers[listEnd].name;
    Keypress();
    // enter string value for account number
    cout << "Account number ===========> ";
    cin >> customers[listEnd].accNo;
    // enter character for account type, loop until
    // value is C (checking) or S (savings)
    do {
      cout << "Account type (C, S) ======> ";
      cin >> choice;
      Keypress();
    } while ((choice != 'C') && (choice != 'S'));
    customers[listEnd].accType = choice;
    // enter number as initial balance amount
    cout << "Amount ===================>£";
    cin >> customers[listEnd].amount;
    // copy amount to new balance
    customers[listEnd].newBal = customers[listEnd].amount;
    // Should we enter another customer?
    do {
      cout << "Enter another customer? (Y/N) ";
      cin >> choice;
    } while ((choice != 'Y') && (choice != 'N'));
  } while (choice != 'N');
}

// ************************************************************
//  Display - display all active accounts
//   Clear the screen.
//   If at least one account was entered, print heading
//   information. Then for each account, print the data.
//   If no accounts entered, say so.
// ************************************************************

void Display (void) {

  int i=0;

  clrscr();
  if (listEnd >= 0) {
    // Print heading information
    cout << "STATEMENT OF ACCOUNTS\n";
    cout << "=====================\n\n";
    cout << "Account name      Acct No.      Type  Old Balance  New Balance\n";
    cout << "================  ============  ====  ===========  ===========\n";
    // Print each account record
    do {
      cout << customers[i].name << "  " << customers[i].accNo << "  " <<
	customers[i].accType << "  " << customers[i].amount << "  " <<
	customers[i].newBal << "\n";
    } while (++i <= listEnd);

  } else {
    // No accounts, say so
    cout << "No accounts in system\n";
  }
  Keypress();
}

// ************************************************************
//  Deposit - allow customer to deposit funds
//   Asks for the account number and then searches for the
//   matching record. If not found, exit.
//   If found, ask for deposit amount. If amount is negative,
//   exit. If amount is positive, add to account balance.
//   Waits for user input at end.
// ************************************************************

void Deposit (void) {

  char accNo[12]; // account to deposit
  double amount=0.0;  // amount to deposit
  int i=0; // variable to loop through accounts

  clrscr();
  if (listEnd >= 0) {
    // ask for the account number
    cout << "Account number ===========> ";
    cin >> accNo;
    Keypress();
    // search the account list for the number
    do {
      if (strncmp(accNo, customers[i].accNo, 12)==0)
	break;
    } while (i++ <= listEnd);
    if (i > listEnd)
      // account number not found, ignore it
      cout << "Account '" << accNo << "' not found.\n";
    else {
      // ask for deposit amount
      cout << "Deposit Amount ===========>£";
      cin >> amount;
      Keypress();
      if (amount<0.0)
	// ignore negative deposits
	cout << "Amount £" << amount << " is negative. Ignored.";
      else {
	// adjust account balance by deposit amount
	customers[i].amount += amount;
	cout << "Deposit of £" << amount << " was made.";
      }
    }
  } else {
    // No accounts, say so
    cout << "No accounts in system\n";
  }
  Keypress();
}

// ************************************************************
//  Withdrawl - allow customer to withdraw funds
//   Asks for the account number and then searches for the
//   matching record. If not found, exit.
//   If found, ask for withdrawl amount. If amount is negative,
//   or more than balance then exit. Otherwise, withdraw that
//   amount from the account balance.
//   Waits for user input at end.
// ************************************************************

void Withdrawl (void) {

  char accNo[12]; // account to withdraw
  double amount=0.0;  // amount to withdraw
  int i=0; // variable to loop through accounts

  clrscr();
  if (listEnd >= 0) {
    // ask for the account number
    cout << "Account number ===========> ";
    cin >> accNo;
    Keypress();
    // search the account list for the number
    do {
      if (strncmp(accNo, customers[i].accNo, 12)==0)
	break;
    } while (i++ <= listEnd);
    if (i > listEnd)
      // account number not found, ignore it
      cout << "Account '" << accNo << "' not found.\n";
    else {
      // ask for withdrawl amount
      cout << "Withdrawl Amount =========>£";
      cin >> amount;
      Keypress();
      if (amount<0.0)
	// ignore negative withdrawls
	cout << "Amount £" << amount << " is negative. Ignored.";
      else if (amount > customers[i].amount)
	// ignore overdrafts
	cout << "Amount £" << amount << " exceeds balance. Ignored.";
      else {
	// adjust account balance by withdrawl amount
	customers[i].amount -= amount;
	cout << "Withdrawl of £" << amount << " was made.";
      }
    }
  } else {
    // No accounts, say so
    cout << "No accounts in system\n";
  }
  Keypress();
}

// ************************************************************
//  Calculate - compute new balance based on account type
//   Compute a "new balance" based on the account type
//   and amount in the account.
// ************************************************************

void Calculate (void) {

  int i=0; // index to loop through account list

  if (listEnd >= 0) {
    // loop through the accounts
    do {
      switch (customers[i].accType) {
	// process saving accounts
      case 'S':
	if (customers[i].amount<5000.0)
	  customers[i].newBal = customers[i].amount*(1.0+10.0/100.0);
	else
	  customers[i].newBal = customers[i].amount*(1.0+12.0/100.0);
	// process checking accounts
      case 'C':
	if (customers[i].amount<100.0)
	  customers[i].newBal = customers[i].amount*(1.0+5.0/100.0);
	else
	  customers[i].newBal = customers[i].amount*(1.0+7.0/100.0);
      }
    } while (++i<=listEnd);
    cout << "New balances computed.";
  }
  else {
    // no accounts found, say so
    cout << "No Accounts in the system.\n";
  }
  Keypress();
}

// ************************************************************
//  main - main program
//  Two nested loops
//   - main loop calls one of the sub functions and repeats
//     until user selects EXIT (#6)
//   - inner loop runs until user selects a valid menu option
// ************************************************************

int main()
{
  int choice;

  // main loop begins here
  do {
    // inner loop - get valid menu selection
    do {
      clrscr();
      MainMenu();
      cin >> choice;
      Keypress();
    } while ((choice <1) || (choice >6));
    // call the selected function
    switch (choice) {
    case 1: 
      Input();
      break;
    case 2:
      Display();
      break;
    case 3:
      Deposit();
      break;
    case 4:
      Withdrawl();
      break;
    case 5:
      Calculate();
      break;
    default:
      clrscr();
      cout << "\n\nThanks for using the KTC banking system\n";
      break;
    }
  } while (choice<6);
  return 0;
}

Request for Answer Clarification by integrated-ga on 04 Mar 2004 15:07 PST
Hi maniac, thanks the program is really satisfying and i appreciate
the effort thanks alot.

But I had little minor documentation that I have relating to this program.

1.)  Produce a ?structure diagram? ?or recognised alternative?  
showing  ?sequence, selection, and iteration?.

2.)Identify clearly which type of data structure you have used in this    
     program ?such as (array, numeric, Boolean, user defined)


3.) Create a data dictionary including:
     
     ·	Procedures 
     ·	Functions
     ·	Variables (names, data types, data scope)


4.)	Prepare a user guide for this particular program covering:
      
      ·	Normal use
      ·	Trouble shooting


thanks integrated

Clarification of Answer by maniac-ga on 04 Mar 2004 18:29 PST
Hello Integrated,

Can you handle a MS Word file or some other format where I can use
both graphics and text? I don't necessarily want to provide a PDF
since you won't be able to make changes.

Also - if you have a specific format to follow for those items, please
let me know. For example, if you look at:
  http://web.njit.edu/~jerry/sad/Notes/SAD-12.ppt
(a power point file) it shows a notation of structure charts that
would be usable [along with some other good notes on program
structure]. If you can, provide an on line example so I can match the
style.

  --Maniac

Clarification of Answer by maniac-ga on 06 Mar 2004 09:45 PST
Hello Integrated,

I put what you asked for on
  http://homepage.mac.com/mhjohnson/ktcdoc.pdf
and
  http://homepage.mac.com/mhjohnson/ktcdoc.doc
for the PDF and word formats respectively.

  --Maniac

Request for Answer Clarification by integrated-ga on 07 Mar 2004 12:13 PST
hi maniac- thanks for the excellent answer that you produced, sorry on
the late respond been busy there is one or two things i have to add to
the program.

1.) you see were it says display accounts details, i also need a
another one were it "displays only a single account detail" when you
choose to display a single account detail it should show a message
saying "please enter record number" and that should refer to the first
customer input.

2.) i also need all the records to be saved on a single " .dat file"
cause when i run the program enter the records after i close the
running program and run it again non of details have been saved.

3.) i need a pseudocode for the program.

    and after that make changes to the document according to what you
have added that be it thanks maniac.
  http://homepage.mac.com/mhjohnson/ktcdoc.pdf

Request for Answer Clarification by integrated-ga on 08 Mar 2004 16:44 PST
hi maniac- i have rated ur answer and gave an additional tip due to
the good work that has been produced once more thank you, but iam a
bit worried the fact that the question is stated as closed. does that
mean that you cant clarify on it?

Clarification of Answer by maniac-ga on 08 Mar 2004 18:56 PST
Hello Integrated,

I updated the program (see below) as well as the other files (both
Word and PDF versions) to match your request. I am a little unclear on
your need for "pseudo code"; I assume you mean something like the
result listed at the end of the answer clarification. It is intended
to capture the "essence" of the program flow.

  --Maniac

Updated application:

// ktc2.cpp - written by Maniac on 2004/03/03
// A translation of Pascal source code provided by integrated-ga
// http://answers.google.com/answers/threadview?id=304408
// rewritten to match the style provided by integrated-ga
// http://answers.google.com/answers/threadview?id=310995

// header files

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

// accRec -- Account record definition:
//  name -- 16 character array with the person's name
//  accNo - 12 character array with the account number
//  accType - one character representing the type of account
//    (C = Checking, S = Savings)
//  amount - floating point value representing the amount of
//    money in the account. Double precision to allow large
//    values without loss of precision.
//  newBal - floating point value representing the new balance
//    in the account (after calculations).
//  NOTE: If you change the length of the name or account
//  number, make sure that dependent code is fixed.

struct accRec {
  char name[16];
  char accNo[12];
  char accType;
  double amount;
  double newBal;
};

// Global variables
//  customers - array of up to 20 customers
//  listEnd - last customer index in use, initial value
//   indicates no customers have been entered.

struct accRec customers[20];
int listEnd = -1;

// Prototypes for user defined functions:
//  MainMenu - prompt the user for main selection
//  

void Import(void);
void Export(void);
void Keypress (void);
void MainMenu (void);
void Input (void);
void Display (void);
void Deposit (void);
void Withdrawl (void);
void Calculate (void);

// ************************************************************
// Import - import from data file
//  Used at start up to load customer data from the last run.
// ************************************************************

void Import() {
  // declare input file and attempt to open it
  ifstream ktcdata("ktc.dat");

  if (!ktcdata)
    return; // if not found, start w/o import
  // repeat loop until end of array (or end of file)
  do {
    // read each value in order listed
    ktcdata >> customers[listEnd+1].name;
    if (ktcdata.eof())
      return; // found end of file
    listEnd++; // have data, increase index into array
    ktcdata >> customers[listEnd].accNo;
    ktcdata >> customers[listEnd].accType;
    ktcdata >> customers[listEnd].amount;
    ktcdata >> customers[listEnd].newBal;
  } while (listEnd+1 < 20);
}

// ************************************************************
// Export - export to data file
//  Used at exit to save customer data for the next run.
// ************************************************************

void Export() {

  int i=0;
  // declare output file and attempt to open it
  ofstream ktcdata("ktc.dat");

  if (!ktcdata)
    return; // ignore output if we cannot create the file
  if (listEnd<0)
    return; // no accounts to output
  do {
    ktcdata << customers[i].name << " " << customers[i].accNo << " " <<
      customers[i].accType << " " << customers[i].amount << " " <<
      customers[i].newBal << "\n";
  } while (++i<=listEnd);
}


// ************************************************************
// Keypress - read to end of line
//  Used after inputs to purge input buffer. Used after
//  output to wait for user entry before continuing.
// ************************************************************

void Keypress() {
  cin.ignore(99,'\n');
}

// ************************************************************
//  MainMenu - prompt the user for main selection
// ************************************************************

void MainMenu (void)
{
  cout << "Choose from one of the following options:\n";
  cout << "\t1. Input customers details\n";
  cout << "\t2. Display accounts details\n";
  cout << "\t3. Customer deposit\n";
  cout << "\t4. Customer withdrawl\n";
  cout << "\t5. Calculate\n";
  cout << "\t6. EXIT\n";
  cout << "What is your choice? ";
  return;                 // Return to main()
}

// ************************************************************
//  Input - add records to active customer list
//   Loop prompts for customer information. Loop exits
//   if customer table is full or user chooses to stop
//   entering new customers.
// ************************************************************

void Input (void) {

  char choice; // Y or N to continue customer input

  do {
    if (listEnd+1 >= 20) {
      cout << "Customer table is full.\n";
      Keypress();
      break;  // exits the loop
    }

    listEnd++;  // move to next customer record
    clrscr();
    cout << "Enter Customer Details\n";
    cout << "======================\n";
    // enter string value for account name
    cout << "Account holder name ======> ";
    cin >> customers[listEnd].name;
    Keypress();
    // enter string value for account number
    cout << "Account number ===========> ";
    cin >> customers[listEnd].accNo;
    // enter character for account type, loop until
    // value is C (checking) or S (savings)
    do {
      cout << "Account type (C, S) ======> ";
      cin >> choice;
      Keypress();
    } while ((choice != 'C') && (choice != 'S'));
    customers[listEnd].accType = choice;
    // enter number as initial balance amount
    cout << "Amount ===================>£";
    cin >> customers[listEnd].amount;
    // copy amount to new balance
    customers[listEnd].newBal = customers[listEnd].amount;
    // Should we enter another customer?
    do {
      cout << "Enter another customer? (Y/N) ";
      cin >> choice;
    } while ((choice != 'Y') && (choice != 'N'));
  } while (choice != 'N');
}

// ************************************************************
//  Display - display all active accounts
//   Clear the screen.
//   If at least one account was entered, print heading
//   information. Then for each account, print the data.
//   If no accounts entered, say so.
// ************************************************************

void Display (void) {

  int i=0;
  char blank; // temporary used to test for blank entry
  char accNo[12]; // account to deposit

  clrscr();
  if (listEnd >= 0) {
    // ask for the account number
    cout << "Leave account number blank to list all accounts\n";
    cout << "Account number ===========> ";
    cin.get(blank); // see if we get a blank entry
    if (blank != '\n') {
      cin.putback(blank); // put the character back
      cin >> accNo; // read the account number
      Keypress();
    }
    // Print heading information
    cout << "STATEMENT OF ACCOUNTS\n";
    cout << "=====================\n\n";
    cout << "Account name      Acct No.      Type  Old Balance  New Balance\n";
    cout << "================  ============  ====  ===========  ===========\n";
    if (blank=='\n') {
      // Print each account record
      do {
	cout << customers[i].name << "  " << customers[i].accNo << "  " <<
	  customers[i].accType << "  " << customers[i].amount << "  " <<
	  customers[i].newBal << "\n";
      } while (++i <= listEnd);
    } else {
      // search the account list for the number
      do {
	if (strncmp(accNo, customers[i].accNo, 12)==0)
	  break;
      } while (i++ <= listEnd);
      if (i > listEnd)
	// account number not found, ignore it
	cout << "Account '" << accNo << "' not found.\n";
      else
	cout << customers[i].name << "  " << customers[i].accNo << "  " <<
	  customers[i].accType << "  " << customers[i].amount << "  " <<
	  customers[i].newBal << "\n";
    }
  } else {
    // No accounts, say so
    cout << "No accounts in system\n";
  }
  Keypress();
}

// ************************************************************
//  Deposit - allow customer to deposit funds
//   Asks for the account number and then searches for the
//   matching record. If not found, exit.
//   If found, ask for deposit amount. If amount is negative,
//   exit. If amount is positive, add to account balance.
//   Waits for user input at end.
// ************************************************************

void Deposit (void) {

  char accNo[12]; // account to deposit
  double amount=0.0;  // amount to deposit
  int i=0; // variable to loop through accounts

  clrscr();
  if (listEnd >= 0) {
    // ask for the account number
    cout << "Account number ===========> ";
    cin >> accNo;
    Keypress();
    // search the account list for the number
    do {
      if (strncmp(accNo, customers[i].accNo, 12)==0)
	break;
    } while (i++ <= listEnd);
    if (i > listEnd)
      // account number not found, ignore it
      cout << "Account '" << accNo << "' not found.\n";
    else {
      // ask for deposit amount
      cout << "Deposit Amount ===========>£";
      cin >> amount;
      Keypress();
      if (amount<0.0)
	// ignore negative deposits
	cout << "Amount £" << amount << " is negative. Ignored.";
      else {
	// adjust account balance by deposit amount
	customers[i].amount += amount;
	cout << "Deposit of £" << amount << " was made.";
      }
    }
  } else {
    // No accounts, say so
    cout << "No accounts in system\n";
  }
  Keypress();
}

// ************************************************************
//  Withdrawl - allow customer to withdraw funds
//   Asks for the account number and then searches for the
//   matching record. If not found, exit.
//   If found, ask for withdrawl amount. If amount is negative,
//   or more than balance then exit. Otherwise, withdraw that
//   amount from the account balance.
//   Waits for user input at end.
// ************************************************************

void Withdrawl (void) {

  char accNo[12]; // account to withdraw
  double amount=0.0;  // amount to withdraw
  int i=0; // variable to loop through accounts

  clrscr();
  if (listEnd >= 0) {
    // ask for the account number
    cout << "Account number ===========> ";
    cin >> accNo;
    Keypress();
    // search the account list for the number
    do {
      if (strncmp(accNo, customers[i].accNo, 12)==0)
	break;
    } while (i++ <= listEnd);
    if (i > listEnd)
      // account number not found, ignore it
      cout << "Account '" << accNo << "' not found.\n";
    else {
      // ask for withdrawl amount
      cout << "Withdrawl Amount =========>£";
      cin >> amount;
      Keypress();
      if (amount<0.0)
	// ignore negative withdrawls
	cout << "Amount £" << amount << " is negative. Ignored.";
      else if (amount > customers[i].amount)
	// ignore overdrafts
	cout << "Amount £" << amount << " exceeds balance. Ignored.";
      else {
	// adjust account balance by withdrawl amount
	customers[i].amount -= amount;
	cout << "Withdrawl of £" << amount << " was made.";
      }
    }
  } else {
    // No accounts, say so
    cout << "No accounts in system\n";
  }
  Keypress();
}

// ************************************************************
//  Calculate - compute new balance based on account type
//   Compute a "new balance" based on the account type
//   and amount in the account.
// ************************************************************

void Calculate (void) {

  int i=0; // index to loop through account list

  if (listEnd >= 0) {
    // loop through the accounts
    do {
      switch (customers[i].accType) {
	// process saving accounts
      case 'S':
	if (customers[i].amount<5000.0)
	  customers[i].newBal = customers[i].amount*(1.0+10.0/100.0);
	else
	  customers[i].newBal = customers[i].amount*(1.0+12.0/100.0);
	// process checking accounts
      case 'C':
	if (customers[i].amount<100.0)
	  customers[i].newBal = customers[i].amount*(1.0+5.0/100.0);
	else
	  customers[i].newBal = customers[i].amount*(1.0+7.0/100.0);
      }
    } while (++i<=listEnd);
    cout << "New balances computed.";
  }
  else {
    // no accounts found, say so
    cout << "No Accounts in the system.\n";
  }
  Keypress();
}

// ************************************************************
//  main - main program
//  Two nested loops
//   - main loop calls one of the sub functions and repeats
//     until user selects EXIT (#6)
//   - inner loop runs until user selects a valid menu option
// ************************************************************

int main()
{
  int choice;

  Import(); // get previous data (if any)
  // main loop begins here
  do {
    // inner loop - get valid menu selection
    do {
      clrscr();
      MainMenu();
      cin >> choice;
      Keypress();
    } while ((choice <1) || (choice >6));
    // call the selected function
    switch (choice) {
    case 1: 
      Input();
      break;
    case 2:
      Display();
      break;
    case 3:
      Deposit();
      break;
    case 4:
      Withdrawl();
      break;
    case 5:
      Calculate();
      break;
    default:
      clrscr();
      cout << "\n\nThanks for using the KTC banking system\n";
      break;
    }
  } while (choice<6);
  Export();
  return 0;
}

Pseudo Code

procedure Import

begin
  if "ktc.dat" is empty then
    return
  do
    if at end of file then
      return
    read record into next record in customer array
  while array is not full
end Import

procedure Export

begin
  if cannot write to "ktc.dat" then
    return
  if array is empty then
    return
  do
    write next record from customer array
  while array has more data
end Export

procedure Keypress

begin
  ignore input from cin until end of line reached
end Keypress

procedure MainMenu

begin
  display main menu
end MainMenu

procedure Input

begin
  do
    if table is full then
      exit loop
    prompt and read customer name
    prompt and read account number
    do
      prompt and read account type
    while account type is not C or S
    prompt and read balance
    do
      prompt and read choice to add more customers
    while choice is not Y or N
  while want to add more customers
end Input

procedure Display

begin
  if no accounts in array then
    tell user no accounts
  else
    prompt and read account number
    display header of account data
    if account number is blank then
      do
        print each account data on a separate line
      while more data to print
    else if account is present in table
      print account data on a single line
    else
      print account is not found
end Display

procedure Deposit

begin
  if accounts are present then
    prompt and read account number
    search for account in array
    if account is present then
      prompt and read deposit amount
      if amount less than zero then
        print deposit is ignored
      else
        add deposit to amount in account
    else
      print account is not found
  else
    print no accounts to process
end Deposit

procedure Withdrawl

begin
  if accounts are present then
    prompt and read account number
    search for account in array
    if account is present then
      prompt and read withdrawl amount
      if amount less than zero then
        print withdrawl is ignored
      else if amount is more than balance amount
        print withdrawl is ignored
      else
        subtract amount from balance amount in account
    else
      print account is not found
  else
    print no accounts to process
end Withdrawl

procedure Calculate

begin
  if accounts to process then
    for each account in array
      based on account type and balance amount, compute new balance
  else
    print no accounts present
end Calculate

procedure Main

begin
  call Import
  do
    do
      call MainMenu
      prompt and read choice
    while choice is less than 1 or greater than 6
    case choice
    1: call Input
    2: call Display
    3: call Deposit
    4: call Withdrawl
    5: call Calculate
    6: print thank you message
  while choice is not 6
  call Export
end Main
integrated-ga rated this answer:5 out of 5 stars and gave an additional tip of: $70.00
excellent answers.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy