Question

in C++ 1. Create the following “data.txt” file. Each record consists of customer's name and their...

in C++

1. Create the following “data.txt” file. Each record consists of customer's name and their balance:

Maria Brown 2100.90

Jeffrey Jackson 200.20

Bernard Smith 223.10

Matthew Davenport 1630.20

Kimberly Macias 19100.90

Amber Daniels 2400.40

The records in the file will be like the text above. You will store them in the struct provided below:

struct PERSON {

   char Name[20];

   float Balance; };

i. We don’t know exactly how many records are in the file. You will have to go through the file a first time and count the number of records (N).

ii . Once you have this information, you can create an array that is large enough to hold all of the records in the file PERSON P[N];

iii. Remember that c_strings or character arrays must be null terminated, so you will either need to set every element to ‘/0’ before writing the name, or make sure you put one after you have written the name.

iv. Display all records in array a. Use calling statement: Display(a, N);

Your output should look like the following:

                Name            Balance

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

Maria Brown 2100.90

Jeffrey Jackson 200.20

Bernard Smith 223.10

Matthew Davenport 1630.20

Kimberly Macias 19100.90

Amber Daniels 2400.40

v. Display the name of customer with maximum balance. Call function FindRichest(a, N);

   Output:

The customer with maximum balance is Kimberly Macias

vi. Allow a customer to deposit money in his/her account. Call function :

Deposit(CustName, a, N); where CustName is the name of the customer who wants to deposit money

    Output:

Enter your full name to deposit money: Maria Brown

                   Maria Brown, how much would you like to deposit? 100

Now your new balance is 2200.90

Vii. Copy array a in the same file. Use calling statement NewCopy(“data.txt”, a, N);

(turn in a copy of the file with the new updated records)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PFB functions created as required

Please note i haven't created a function to write data back to txt file as i run out of time.

It will be similar to reading

#include <iostream>

#include <fstream>

#include <sstream>

struct PERSON {

char Name[20];

float Balance;

PERSON(const char *Name, float Balance)

{

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

{

this->Name[i] = Name[i];

}

this->Balance = Balance;

}

};

int GetRecordsCount(const char* filePath)

{

int count = 0;

std::string line;

std::ifstream file(filePath);

while (std::getline(file, line))

count++;

return count;

}

void Display(PERSON* a, int N)

{

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

{

std::cout << a[i].Name << " " << a[i].Balance << std::endl;

}

}

PERSON FindRichest(PERSON* a, int N)

{

PERSON richest = a[0];

for (int i = 1; i < N; i++)

{

if (richest.Balance < a[i].Balance)

richest = a[i];

}

return richest;

}

void Deposit(char* CustName, PERSON* a, int N)

{

float depositAmount;

std::cout << CustName << ", how much would you like to deposit?";

std::cin >> depositAmount;

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

{

if (a[i].Name == CustName)

{

a[i].Balance += depositAmount;

std::cout << "Now your new balance is " << a[i].Balance;

}

}

}

int main()

{

const char* filePath = "F:\\Learning\\Chegg\\Data\\data.txt";

int N = GetRecordsCount(filePath);

struct PERSON* P = (PERSON*)malloc(N * sizeof(PERSON));

int index = 0;

std::string line;

std::ifstream file(filePath);

while (std::getline(file, line))

{

int pos1 = line.find(" ", 0);

int pos2 = line.find(" ", pos1 + 1);

std::string strName = line.substr(0, pos2 - 1);

std::string strBalance = line.substr(pos2 + 1);

char* Name = const_cast<char*>(strName.c_str());

float Balance = std::stof(strBalance);

PERSON tmp = PERSON(Name, Balance);

P[index++] = tmp;

}

Display(P, N);

PERSON richest = FindRichest(P, N);

std::cout << "The customer with maximum balance is " << richest.Name << std::endl;

char custName[20];

std::cout << "Enter your full name to deposit money:";

std::cin >> custName;

Deposit(custName, P, N);

std::cout << "Hello World!\n";

}

Add a comment
Know the answer?
Add Answer to:
in C++ 1. Create the following “data.txt” file. Each record consists of customer's name and their...
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
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