|
|
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; } | |
| |
| |
|
|
Subject:
Re: C++ PROGRAMMING
Answered By: maniac-ga on 03 Mar 2004 18:31 PST Rated: |
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; } | |
| |
| |
| |
| |
| |
|
integrated-ga
rated this answer:
and gave an additional tip of:
$70.00
excellent answers. |
|
There are no comments at this time. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |