File handling with C++ Programming

Learn File handling with the following sample code

You are to write a program that reads in lines of input using scanf or cin, and file redirection, process the data, and then output the results using printf or cout. This means you can use C or C++ style I/O as is your personal preference.

  1. Name your file lab1assignment.cpp. Failure to name the file correctly may lead to a grade of zero.
  2. The data file used in the example is available at: ~dmk0080/public/1040/labs/one.or in the Cavans assignment. Use the file named "transactiondata
  3. Use command line redirection to execute as in ./a.out angle bracket transactiondata"
  4. to compile use g++ labassignment1.cpp to create the executable file a.out

The Data:

  1. Each line of th named "transactione data file begins with an 'e', 'c', or 't'.
  2. Lines beginning with 'e' will have an integer employee ID and an employee name.
  3. Lines beginning with 'c' will have an integer customer ID, customer name, and a floating point account balance.
  4. Lines beginning with a 't' will have a customer ID, employee ID, a 'w' or 'd' representing withdrawal or deposit,and a floating point transaction amount.
  5. Lines beginning with an 'e' and 'c' may be intermingled, but all 'e' and 'c' lines will come before lines beginning with 't'.
  6. There will be at most 50 employees and 50 customers, and an unknown number of transactions. Names will be at most 15 characters long.

An example data file is shown below:

e 5 Elden

c 3 Felipe 55342.51415

e 3 Leonardo

e 1 Young

c 9 Alessandra 8114.541862

Above is a sample of data, more data information is available on this git link

What do we expect our program to do?

Your program should read in the employee and customer information and update the customer account balance depending on the transaction. For example, t 1 8 w 4924.86 means to update the balance for customer 1 by withdrawing 4924.86 from Devon's account balance of 56442.27875 . For each transaction, output customer name, the employee name, a plus sign for deposit or a minus sign for withdrawal, the transaction amount, and the new balance after the transaction with two decimal places (truncate values; do not worry about rounding). For example, the output for the example transaction above would appear as (include the line of digits):

Example of an output from the code we have to write

123456789012345678901234567890123456789012345678901234567890

Devon Kourtney -$4924.86 $ 51517.42

More information if the expected code

You should create structures to hold the data for each employee and for the customers, including their current balance. Then create arrays of these structures. You will need counters to keep track of the next available space in each array, as well as to use for loop control when searching for information. The arrays may be fixed size base on the limits provided above. When you receive a 't" command you can get the employee name and customer name and balance from the arrays. Be sure to update the customer balance to complete the transaction processing. You will output the iformation as described above.

When oyu have completed and tested your program on the CSE Linux Servers, you will need to download it to your local machine, and then upload it to the assignment box in Canvas for Lab1. Be sure to include all the files needed to compile, link and run your program if you created more than a single program file.

SOLUTION

I know most of you know the use of the first three lines of the code so I don't need to explain them

Line 7 and 8 declares two constant variables. Line 16 and 22 define employee and customer of type structure. Struct or structure is like an object in Object Oriented Programming section. From line 29 to 33, we have declared several functions which the program will use to read the file and process it as required from the instructions above.

Definition of the Function declared Earlier

The following screenshot shows the definition of the functions declared earlier. We haven't shown the Main function yet

The Main Funtion

Line 38 to 80 defines the main function. I have added comments to make it easy for you to understand. You can contact me through WhatsApp if you have any questions.

What happens when we run the code?

Above shows screenshot of the output when you run the code based on the instructions provided earlier

A Short Clip Showing how the program works

Code GitHub Link

Here is the link to download the code Download Here

You can reach me on WhatsApp if you have any qeuestions. Thank you

Comments

Popular posts from this blog

Hangman Game With Python

Build a Shopping List Application with ArrayList in Java