Remember to use at least three MEANINGFUL functions; label all output; put name
ID: 3707030 • Letter: R
Question
Remember to use at least three MEANINGFUL functions; label all output; put name and
lecture section in appropriate places. You must implement an array of structs
in solving this problem.
You have been hired to write a program to process information about a toy collection. The collection
contains G.I. Joes’, which are toy action figures, and a text file contains data about the collection of
these toys. Each line contains information about one toy:
? the cost
? year of purchase
? condition
? Name of the toy action figure.
The cost is a floating point value. Data items are separated by blanks. Your solution should process files
of any size.
a) Declare an array with 75 components. Input the data into the array. Assume the file has at most 75
components. You may use dynamic variables if you choose.
b) Print out a labeled listing of the data in four columns and include a counter showing the
total number of items in the collection.
c) Sort the data in descending order by Year of purchase (most recent to least recent
purchase). Print out the sorted array to the screen.
d) Create an output file. It should contain your identifying information along with a labeled printout
of the sorted array. Name the output file using your last name.
Here is the .txt file: File name: input2.txt (That part is not actually in the file, just naming it for you)
87.21 1986 poor Sniper
77.81 1902 good Hunter
71.92 2009 used Captain
86.12 2003 poor SealOne
11.9 1986 poor Willard
21.34 1989 new Eagle
67.68 1991 used Boot
66.66 2015 poor Chief
16.53 1959 poor Skipper
74.22 1990 good Tanker
22.14 2011 worn Jane
54.23 1992 good Sarge
54.01 1967 poor Ozzball
45.33 1971 used Griffin
16.23 1958 new Billy
54.22 1991 old Tazer
22.14 2004 worn Tommy
86.13 2007 poor Skipper
79.81 2010 new Cookie
76.76 1965 new Junior
33.33 1982 used Howie
37.76 2017 poor Ziggy
56.7 1984 used Larry
80.98 1984 good Carla
41.23 1991 new Bilco
86.12 2003 poor SealTwo
Explanation / Answer
I wrote the code in the c++, please run it and check:
Please have input.txt and output.txt, in the right location, and copy the path and fill it in the program
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct Toy{
double cost;
int year_Of_Purchase;
string condition;
string name;
};
bool sort_fn(Toy a, Toy b)
{
return (a.year_Of_Purchase > b.year_Of_Purchase);
}
int main(int argc, const char * argv[]) {
ifstream fin;
string str;
Toy toys[75];
//Please find the file path of your text file from properties of the text file and put in the inFile.open("YOUR_FILE_LOCATION")
fin.open("/Users/saikat/Desktop/Chegg/Chegg/input.txt", ifstream::in);
if (!fin) {
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
int i = 0;
while(!fin.eof()) // To get you all the lines.
{
Toy toy = *new Toy();
fin>> toy.cost >> toy.year_Of_Purchase >> toy.condition >> toy.name;
toys[i] = toy;
++i;
}
fin.close();
sort(toys, toys+sizeof(toys)/sizeof(*toys), sort_fn);
ofstream myfile ("/Users/saikat/Desktop/Chegg/Chegg/output.txt");
if (myfile.is_open())
{
for (int i = 1; i<sizeof(toys)/sizeof(*toys); i++) {
if (toys[i].year_Of_Purchase == 0) {
break;
}
myfile<< i;
myfile<< " ";
myfile << toys[i].cost;
myfile<< " ";
myfile << toys[i].year_Of_Purchase;
myfile<< " ";
myfile << toys[i].condition;
myfile<< " ";
myfile << toys[i].name;
myfile<< " ";
myfile<< " ";
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}