Question

In: Computer Science

c++ Write a program that opens a file, reads records into a container of data structures,...

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)

Solutions

Expert Solution

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


Related Solutions

Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
1)  Write a python program that opens a file, reads all of the lines into a list...
1)  Write a python program that opens a file, reads all of the lines into a list of strings, and closes the file. Use the Readlines() method. Test your programing using the names.txt file provided. 2) Convert the program into a function called loadFile, that receives the file name as a parameter and returns a list of strings. 3) Write a main routine that calls loadFIle three times to load the three data files given into three lists. Then choose a...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT