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

Please help. Everytime i try to write it it gives me an error saying that i cann

ID: 3823921 • Letter: P

Question

Please help. Everytime i try to write it it gives me an error saying that i cannot find the struct file. I have no idea why it's saying that.

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

In order to get the full point for the assignment, make sure you follow the coding guidelines provided:
- 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)

Explanation / Answer

Before implementing above requirement we need to understand the basic terms :-
Structure is a user defined data type that allows to combine data items of different kinds.It is used to represent a record.
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Implementation of above requirement:-

step 1:- cretae a costfile.txt file.In this put data as given below:

Pass_Go 200.00 135
Reading_RR 50.00 136
Connecticut 120.00 137
Chance 25.00 138

step 2:- create File cost.h

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

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

#endif

step 3:- create main logic implementation file

//This program will read/write data to file. The data will be structured, and manipulated
//by functions. A header file will be included with the struct information.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>

#include "cost.h"       //Structure File
using namespace std;

//Functions
//Cost parse_account(ifstream &source, vector <Cost>& data);
Cost parse_account(ifstream &source);
double calculateSumOfAccounts(vector <Cost>& data);

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

   //Importing data from file with verification.
   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(); //Closing "costfile.txt"
  
   //Message output of sum
   sum = calculateSumOfAccounts(allData);
   cout << "The sum of all items is: " << sum << endl << endl;

   //Output File
   ofstream outFile;
   outFile.open("outfile.dat", ios::out | ios::binary); //Saving as a binary file.
   for (int index = 0; index < allData.size(); index++) //Basic loop to write all data to the file.
       outFile << setw(12) << allData[index].descr << setw(10) << allData[index].amount << setw(10) << allData[index].itemNum << endl;
   outFile.close();

   system("pause");
   return 0;
}
//parse_account() will take a ifstream object as an argument, scan a line from the file
//and save the data to tempCost, of type Cost. tempCost is then returned.
Cost parse_account(ifstream &source) {
   string desc;
   double amount;
   int inum;
   Cost tempCost;

   //Scanning a line from the file
   source >> desc >> amount >> inum;

   //Assigning the data to the tempCost variable
   tempCost.descr = desc;
   tempCost.amount = amount;
   tempCost.itemNum = inum;

   return tempCost;
}
//calculateSumOfAccounts() will step through a vector of type Cost and add all the amounts, then
//return the sum as a double.
double calculateSumOfAccounts(vector <Cost>& data) {
   double sum = 0;
   for (int index = 0; index < data.size(); index++)
       sum += data[index].amount;
   return sum;
}