In: Computer Science
c++
Write a program that opens a file, reads records into a
container of data structures, and prints out the sum in
order.
You can use Vectors to contain the data structures.
1. Create a data structure to represent the record ==>
struct Cost in cost.h
2. Write a function called ==> parse_account,
that parses one (string) record from a file, and
populates a data structure with that data.
3. Write a function called sum_accounts, that when
passed a container of structures, returns
a double representing the sum of the amounts in those
structures.
4. Create a main program
a) Create an appropriate container of data
structures
b) Open the accounts file
(costfile.txt)
c) While you can read a line from the file
without error
Call
parse_account, which returns a data structure.
Add the
returned data structure to the container (using, pushToV)
d) Call sum_accounts to determine the amount of
money represented
e) Print a message and the result.
Try this with some sample data, such as the following lines in
costfile.dat
You may add or create your own data file to test the program
with:
You can start with the data in a text file and then write the data
in a binary file, this
way you practice with both file types.
Description
Amount Item number
Pass_Go
200.00 135
Reading_RR 50.00
136
Connecticut 120.00
137
Chance
25.00 138
- Use input file to read the above Data
- Use header file for the data structure definition
- Use Multiple Source Files for function definitions and
Application, each in separate file (.cpp file)
Here is the program
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include "cost.h"
using namespace std;
// function declarations
struct cost parse_account(ifstream*);
void pushToV(vector<cost>&,struct cost);
double sum_accounts(vector<cost>& );
// pushes the data record into cotnainer
void pushToV(vector<cost> &records,struct cost
onerecord)
{
//add the record to the container
records.push_back(onerecord);
}
// Find sum of the account
double sum_accounts(vector<cost> &records)
{
double sum=0;
// loop through the vector
for(int i=0;i<records.size();i++)
{
sum=sum+(records[i].amount);
}
return sum;
}
// the driver program
int main()
{
vector <cost> records;
//ifstream object to read a text file
ifstream ifs;
struct cost record;
// open the cost file
ifs.open("costfile.txt");
if(!ifs.is_open())
{
cout<<"Unable to open file"<<endl;
return 0;
}
cout<<"----- The account Data -----"<<endl;
//loop through the file content
while(!ifs.eof())
{
//call parse_account and store returned
record=parse_account(&ifs);
//push the record into the container
pushToV(records,record);
}
//print the message and the sum
cout<<"---------------------------------"<<endl;
cout<<"The sum = $
"<<sum_accounts(records)<<endl;
//system("pause");
return 0;
}
// parses one record each time and populates the struct
record
struct cost parse_account(ifstream *ifs)
{
struct cost record;
stringstream stringRecord;
string str;
//read a record into string str
getline(*ifs,str);
//print the record
cout<<str<<endl;
//pass the string str into stringRecord
stringRecord<<str;
// fill the record structure
stringRecord>>record.description;
stringRecord>>record.amount;
stringRecord>>record.itemNum;
return record;
}
The cost.h file code
#include <iostream>
using namespace std;
struct cost
{
string description;
double amount;
int itemNum;
};
content of costfile.txt
Description Amount Item number
Pass_Go 200.00 135
Reading_RR 50.00 136
Connecticut 120.00 137
Chance 25.00 138
Here is the image of program output
