Question

Language c++ 9.10 Money from File This exercise will read 3 fields from a file for...

Language c++

9.10 Money from File

This exercise will read 3 fields from a file for an unknown number of rows. It mimics the listing of dollar bills of a particular denomination that belongs to a certain person. The following data read will be read in the following order:

wallet owner,number of bills,denomination of bill

The wallet owner will be a string, the number of bills and denomination will be ints. Further, the denominations will always be 0,1, 2, 5, 10, 20, 50 or 100.

A particular owner may appear multiple times in the file at any point, but must hold only 1 Wallet. A particular user and denomination will never repeat, however. As a sample, a file could be like the following:

Tom,3,20
Sally,1,1
Tom,1,5

Such a a file must result in only two Wallets, 1 for Tom and another for Sally. See the following classes:

class CashBills
{

public:
   CashBills(int d, int q):denom(d), quant(q){}

   int denom;

   int quant;

};
class Wallet
{

public:
   vector<CashBills*> money;
   string walletOwner;

};

You will fill the following map:

map<string, Wallet*> bankMap;

The key is the Wallet owner's name. The Wallet* contains the wallet owner and a list of CashBills.

Read the file and load the data into bankMap. Part of the challenge is to handle the fact that a name may appear multiple times in the file and to add CashBills properly. The code uses a function called stoi which basically changes the string representation of a number into an actual int value.

You will use the for_each algorithm to output a unique list of wallet owners and to find the total amount of money in their wallets. For instance, using the above example, the for_each() must result in the following:

Sally $1
Tom $65
Unique wallets:2

#include<iostream>
#include<vector>
#include<fstream>
#include<list>
#include<map>
#include<algorithm>

using namespace std;


//Do not change these classes
class CashBills
{

public:
//CashBills constructor
CashBills(int d, int q):denom(d), quant(q){}

int denom;
int quant;

};

class Wallet
{

public:
vector<CashBills*> money;
string walletOwner;

};

//Implement the showWallet function to output to monitor like:
//wallet_owner_name $xxx
//see how showWallet is used in the main()

int main()
{

//Use this code to open and process the file
ifstream inp("moneys.txt");

if(!inp.is_open())
{
cout << "File did not OPEN!\n";
return 1;
}

map<string, Wallet*> bankMap;
  
string own; //this will hold the wallet owner
int den, q; //holds denomination and quantity of bills

CashBills *cb;
Wallet *w;

char txt[100]; //a temporary spot to hold file input

while(!inp.eof())
{
//this code will read one row of data to own, den, and q
inp.getline(txt, 100, ',');
own=txt;
inp.getline(txt, 100, ',');
q = stoi(txt);
inp.getline(txt, 100, 10);
den = stoi(txt);
//by this point, data has been read into own, den, and q
  
  
//instantiates cash bills
cb = new CashBills(den, q);

//check if key exists in bankMap
//if so, assign w to existing value
//if not, instantiate wallet and add to bankMap
  
//By this point, w must point to a valid Wallet
//so, just add to money member
w->money.push_back(cb);
   

}

//close the file as all data should be loaded to bankMap
inp.close();

//The below line must work to output every bank member in order of wallet owner
for_each(bankMap.begin(), bankMap.end(), showWallet);

//output the following, where xx shows the exact number of unique wallets like:
//Unique wallets:xx


return 0;

}

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

#include<iostream>
#include<vector>
#include<fstream>
#include<list>
#include<map>
#include<algorithm>

using namespace std;
class CashBills
{

public:
CashBills(int d, int q):denom(d), quant(q){}

int denom;
int quant;

};

class Wallet
{

public:
vector<CashBills*> money;
string walletOwner;

};

//Implement the showWallet function to output to monitor like:
//wallet_owner_name $xxx
//see how showWallet is used in the main()

void showWallet(int d,int q)

{

cout<<d<<q;

}

int main()
{

ifstream inp("moneys.txt");

if(!inp.is_open())
{
cout << "File did not OPEN!\n";
return 1;
}

map<string, Wallet*> bankMap;
  
string own;
int den, q;

CashBills *cb;
Wallet *w;

char txt[100];

while(!inp.eof())
{
inp.getline(txt, 100, ',');
own=txt;
inp.getline(txt, 100, ',');
q = stoi(txt);
inp.getline(txt, 100, 10);
den = stoi(txt);
cb = new CashBills(den, q);

w->money.push_back(cb);
   

}


inp.close();

for_each(bankMap.begin(), bankMap.end(), showWallet);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Language c++ 9.10 Money from File This exercise will read 3 fields from a file for...
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
  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • in c++ please Page 1 of 3 (PRO) Project Assignment Instructions Last Charged: 6/1/2020 Read and...

    in c++ please Page 1 of 3 (PRO) Project Assignment Instructions Last Charged: 6/1/2020 Read and follow the directions below carefully and perform the steps in the order listed. You will be solving one program as instructed and turning in your work electronically via an uploaded file within Eagle Online/Canvas and copy & paste the program to the Text Entry box as well. Make sure and check your work prior to uploading the assignment (NOTE: For Steps 1 & 2...

  • IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5...

    IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5 OVERVIEW This homework builds on Hw4(given in bottom) We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

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