Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and Implement a C++ Application. This assignment gives you an opportunity

ID: 3762975 • Letter: D

Question

Design and Implement a C++ Application.

This assignment gives you an opportunity to learn about files, arrays, and pointers.
A cost involves a Description, and an amount and An Item Number.

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

Explanation / Answer

#include <iostream>
amount.txt file

//Structure for the program
#ifndef COST_H
#define COST_H

struct Cost
{
   std::string descr;
   double amount;
   int itemNum;
};

#endif


sum.cpp file
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>

#include "cost.h"      
using namespace std;

  
Cost parse_account(ifstream &source);
double sum_accounts(vector <Cost>& data);

int main() {
     
   vector <Cost> allData;
   string desc;
   double amount, sum = 0.0;
   int n;
   Cost tempCost;

     
   ifstream costFile;
   costFile.open("costfile.txt", ios::in);
   //This loop will call the parse_account() function (until the EOF is reached) and push_back() the data into the main vector, allData.
   while (!costFile.eof() && costFile.is_open())
{
       allData.push_back(parse_account(costFile));
   }
   costFile.close();   
  
     
   sum = sum_accounts(allData);
   cout << "The sum of all items is: " << sum << endl << endl;

     
   ofstream outFile;
   outFile.open("outfile.dat", ios::out | ios::binary);
   for (int i = 0; i < allData.size(); i++)   
       outFile << setw(12) << allData[i].descr << setw(10) << allData[i].amount << setw(10) << allData[i].itemNum << endl;
   outFile.close();

   system("pause");
   return 0;
}

Cost parse_account(ifstream &source) {
   string desc;
   double amount;
   int n;
   Cost tempCost;

     
   source >> desc >> amount >> n;

     
   tempCost.descr = desc;
   tempCost.amount = amount;
   tempCost.itemNum = n;

   return tempCost;
}

double sum_accounts(vector <Cost>& data) {
   double sum = 0;
   for (int i = 0; i < data.size(); i++)
       sum += data[i].amount;
   return sum;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote