Question

Files, Pointers and Dynamic Memory Allocation, and Structs Due date/time: Tuesday, Nov 26th, 11:00 PM. WRITE...

Files, Pointers and Dynamic Memory Allocation, and Structs

Due date/time: Tuesday, Nov 26th, 11:00 PM.

WRITE A C++ PROGRAM (USE DYNAMIC MEMORY ALLOCATION) THAT READS N CUSTOMER RECORDS FROM A TEXT FILE (CUSTOMERS.TXT) SUCH THAT THE NUMBER OF THE RECORDS IS STORED ON THE FIRST LINE IN THE FILE. EACH RECORD HAS 4 FIELDS (PIECES OF INFORMATION) AND STORED IN THE FILE AS SHOWN BELOW:

Account Number (integer)

Customer full name (string)

Customer email (string)

Account Balance (double)

The program is expected to print the records in the format shown below, sorted in decreasing order based on the account balance:

Account Number : 1201077

Name                     : Jonathan I. Maletic

Email                     : [email protected]

Balance                   : 10,000.17

-------------------------------------

Note: The records stored in the following format (4 lines for each account):

First Line is the account number

Second Line is full name

Third line is an email

Forth line is the available balance

Important Note: You must use 4 dynamic arrays (one for each field) to store all the records (information) read from the file. Then sort the arrays and then print it. The program should open the files and read the first token and use it size for the dynamic array.

Example (data saved in the text file)

Output:

3

1201077

Jonathan I. Maletic

[email protected]

10,000.17

1991999

Aziz . s. fuways

[email protected]

5,000.11

1333333

Bill Bultman

[email protected]

120,000.00

Account Number : 1333333

Name           : Bill Bultman

Email          : [email protected]

Balance        : 120,000.00

-------------------------------------

Account Number : 1201077

Name           : Jonathan I. Maletic

Email          : [email protected]

Balance        : 10,000.17

-------------------------------------

Account Number : 1991999

Name           : aziz . s. fuways

Email          : [email protected]

Balance        : 5,000.11

-------------------------------------

                                                                                                               

  • Make sure your programs adhere to proper programming style (e.g., good identifiers, comments, etc.). Post your questions on Piazza for a prompt response.
  • Please submit the two programs to the same dropbox on Canvas.
  • Please take a look at the example posted on Piazza and use it as a model.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code:

Code to copy:

// Include the necessary header files

#include <iostream>

#include <cstdlib>

#include <iomanip>

#include <fstream>

using namespace std;

// Define the main() function

int main()

{

// Declare the necessary dynamic arrays for each field

int *account_number;

string *full_name, *customer_email;

double *balance;

// Create an ifstream object

ifstream infile;

// Open the input file Customers.txt

infile.open("Customers.txt");

// Check if the file exists

if(!infile)

{

// Display the file does not exist

cout<<"The input file is not found!!"<<endl;

// Terminate the program

exit(0);

}

// Set precision for decimal value to two

cout << setprecision(2) << fixed;

// Declare an int variable to store the number of records

// in the file

int num_records = 0;

// Read the first line of the file, and store the

// value to the variable declared

infile>>num_records;

// Dynamically allocate memory for each array

account_number = new int[num_records];

full_name = new string[num_records];

customer_email = new string[num_records];

balance = new double[num_records];

// Declare an int variable, and initialize it to

// zero

int x = 0;

// Ignore '\n' character from file stream

infile.ignore();

// Loop till the end of the file

while(!infile.eof())

{

// Read the account number from the file, and store the

// value to the account_number array

infile>>account_number[x];

// Ignore '\n' character from file istream

infile.ignore();

// Read the full name from the file, and store the value

// to full_name array

getline(infile, full_name[x]);

// Read the email id from the file, and store the value

// to customer_email array

infile>>customer_email[x];

// Read the balance from the file, and store the value

// to balance array

infile>>balance[x];

// Increment the value of x

x++;

}

// Loop to sort the array in descending order, based on the

// values stored in the balance array

for(x = 0; x < num_records; x++)

{

for(int i = 0; i < num_records; i++)

{

// Check if the value at index x is greater than the

// value at index i

if(balance[x] > balance[i])

{

/* Sorting balance array */

// Declare a temporary double varaible, and assign the

// value at index x to the variable

double temp_bal = balance[x];

// Assign the value at index i to index x

balance[x] = balance[i];

// Assign the value in temp_bal variable to index i

balance[i] = temp_bal;

/* Sorting account number array */

// Declare a temporary string varaible, and assign the

// value at index x to the variable

int temp_num = account_number[x];

// Assign the value at index i to index x

account_number[x] = account_number[i];

// Assign the value in temp_num variable to index i

account_number[i] = temp_num;

/* Sorting name array */

// Declare a temporary int varaible, and assign the

// value at index x to the variable

string temp_name = full_name[x];

// Assign the value at index i to index x

full_name[x] = full_name[i];

// Assign the value in temp_name variable to index i

full_name[i] = temp_name;

/* Sorting email array */

// Declare a temporary string varaible, and assign the

// value at index x to the variable

string temp_email = customer_email[x];

// Assign the value at index i to index x

customer_email[x] = customer_email[i];

// Assign the value in temp_email variable to index i

customer_email[i] = temp_email;

}

}

}

// Loop to display the details of the customer, in

// descending order

for(x = 0; x < num_records; x++)

{

// Display the complete customer details

cout<<"Account Number: "<<account_number[x]<<endl;

cout<<"Name: "<<full_name[x]<<endl;

cout<<"Email: "<<customer_email[x]<<endl;

cout<<"Balance: "<<balance[x]<<endl;

cout<<"-------------------------"<<endl;

}

// Close the input file object

infile.close();

// Return zero

return 0;

}

Sample output:

Add a comment
Know the answer?
Add Answer to:
Files, Pointers and Dynamic Memory Allocation, and Structs Due date/time: Tuesday, Nov 26th, 11:00 PM. WRITE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Problem statement: You are tasked with writing a program which will write a monthly account summary...

    Problem statement: You are tasked with writing a program which will write a monthly account summary of customers' transactions at the bank at which you are employed.The monthly transactions are stored in a file called transactions.txt in the follow form: 123 d45.10 d50.45 d198.56 w45.67 The first entry is the account number; the remainder of the line, variable in length, contains all the transactions of the particular customer for the month: If the entry begins with a d, the number...

  • Project 3 Instructions 1. Due Date & Time: 09-26-2020 at 11:59 PM WHAT TO SUBMIT Submit...

    Project 3 Instructions 1. Due Date & Time: 09-26-2020 at 11:59 PM WHAT TO SUBMIT Submit 1 zip file containing 4 files below to iLearn by the deadline. [30 points] ● 3 JAVA Files: TableBmiPro.java, MyOwnIdea.java. DiceRoll.java [24 points] ● 1 File: Make a document that shows the screen captures of execution of your programs and learning points in Word or PDF. Please make sure you capture at least 2 executions for 1 program, so 6 screen captures and one...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • The purpose of this assignment is to develop solutions that perform File IO and objects. Problem specifications are shown below with my changes in blue. 1. File Previewer Write a program that asks...

    The purpose of this assignment is to develop solutions that perform File IO and objects. Problem specifications are shown below with my changes in blue. 1. File Previewer Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along with a message indicating the entire file has been...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT