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
  • 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="";       ...

  • 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...

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