Write c++ program in which collection contains G.I Joe\'s, which are you action
ID: 3820658 • Letter: W
Question
Write c++ program in which collection contains G.I Joe's, which are you action figures, and the text file contains data about the collection of these toys. each line contains collection of one you with it's cost, year of purchase, condition and name of you. cost is floating point value and each data is separated by blanks.
1. Declare array with 75 components
2. print labeled listing of data in 4 columns
3.prompt user to enter year and then print listing of all data about all toys bought that year
4.prompt user for price
Explanation / Answer
Below is the code for the question. Since sample file is not given, tested with some test data on own assuming the file format to be with each of the like
name cost year condition
Also since space is being used as a delimiter, you would not be able to specify spaces in name or condition. Program would not work correctly if the field values have spaes. Also after prompting for price, question does not mention whether you want to list toys with same price or greater or lesser. So printing all greater than or equal to the specifid price.
In case there is any change in file format or query on price, please leave a comment with your requirements so that I can change the program accordingly. Please do rate the answer if it helped.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class toy
{
private :
int year;
float cost;
string name;
string condition;
public:
toy()
{
year=0;
cost=0;
name="";
condition="";
}
string getName()
{
return name;
}
void setName(string name1)
{
name=name1;
}
float getCost()
{
return cost;
}
void setCost(float c)
{
cost=c;
}
int getYear()
{
return year;
}
void setYear(int y)
{
year=y;
}
string getCondition()
{
return condition;
}
void setCondition(string c)
{
condition=c;
}
};
//print all toys
void print(toy collection[], int n)
{
cout<<" Name Year Cost Condition"<<endl;
cout<<"__________________________________________________"<<endl;
for(int i=0;i<n;i++)
{
//width is used to print in fixed width column and and left for left aligned , setprecision to specify number of decimal places
cout.width(15);
cout<<left<<collection[i].getName();
cout<<collection[i].getYear()<<" ";
cout<<setprecision(2)<<collection[i].getCost();
cout<<" "<<collection[i].getCondition()<<endl;
}
cout<<endl;
}
//prints toys bought in specfied year
void printToysInYear(toy collection[],int n,int year)
{
cout<<" Name Year Cost Condition"<<endl;
cout<<"__________________________________________________"<<endl;
for(int i=0;i<n;i++)
{
//if the current toy's year is not the year specified, continue in back into loop and
// dont process that record
if(collection[i].getYear() != year)
continue;
cout.width(15);
cout<<left<<collection[i].getName();
cout<<collection[i].getYear()<<" ";
cout<<setprecision(2)<<collection[i].getCost();
cout<<" "<<collection[i].getCondition()<<endl;
}
cout<<endl;
}
//prints toys greater than equal to price
void printToysGreater(toy collection[],int n,float price)
{
cout<<" Name Year Cost Condition"<<endl;
cout<<"__________________________________________________"<<endl;
for(int i=0;i<n;i++)
{
//if the current toy's year is not the year specified, continue in back into loop and
// dont process that record
if(collection[i].getCost() < price)
continue;
cout.width(15);
cout<<left<<collection[i].getName();
cout<<collection[i].getYear()<<" ";
cout<<setprecision(2)<<collection[i].getCost();
cout<<" "<<collection[i].getCondition()<<endl;
}
cout<<endl;
}
int main()
{
toy collection[75]; //declare an array to hold 75 toy objects
ifstream file;
string filename;
int year;
double price;
string name,condition;
int count,choice;
cout<<" Enter filename: ";
cin>>filename;
file.open(filename);
if(!file.is_open()) //check there was no error
{
cout<<"Could not open file : "<<filename<<endl;
return 1;
}
count=0;
while(file>>name)
{
//assuming the order of data in the file is name cost year condition ,
//if there is any change in the order , please change the order below statements
//for reading from file
file>>price;
file>>year;
file>>condition;
collection[count].setName(name);
collection[count].setCost(price);
collection[count].setCondition(condition);
collection[count].setYear(year);
count++;
}
file.close();
print(collection, count);
//keep displaying menu and ask for users choice
while(true)
{
cout<<"1. Get toys bought in a year"<<endl;
cout<<"2. Get toys greater than price"<<endl;
cout<<"3. Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
if(choice==1)
{
cout<<"Enter year: ";
cin>>year;
printToysInYear(collection,count,year);
}
else if(choice==2)
{
cout<<"Enter price: ";
cin>>price;
printToysGreater(collection,count,price);
}
else if(choice == 3)
{
exit(0);
}
else
{
cout<<" Invalid choice!"<<endl;
}
}
}
sample intput file toys.txt
Roadblock 10.95 2017 new
Duke 12 2015 used
Shipwreck 11 2015 broken
Barbecue 25 2016 good
Flint 15 2015 good
Jinx 10 2016 broken
Doc 18 2017 used
Dusty 12 2017 new
Hawk 15 2017 good
Countdown 12 2015 broken
sample output
Enter filename: toys.txt
Name Year Cost Condition
__________________________________________________
Roadblock 2017 11 new
Duke 2015 12 used
Shipwreck 2015 11 broken
Barbecue 2016 25 good
Flint 2015 15 good
Jinx 2016 10 broken
Doc 2017 18 used
Dusty 2017 12 new
Hawk 2017 15 good
Countdown 2015 12 broken
1. Get toys bought in a year
2. Get toys greater than price
3. Exit
Enter your choice: 1
Enter year: 2017
Name Year Cost Condition
__________________________________________________
Roadblock 2017 11 new
Doc 2017 18 used
Dusty 2017 12 new
Hawk 2017 15 good
1. Get toys bought in a year
2. Get toys greater than price
3. Exit
Enter your choice: 2
Enter price: 14
Name Year Cost Condition
__________________________________________________
Barbecue 2016 25 good
Flint 2015 15 good
Doc 2017 18 used
Hawk 2017 15 good
1. Get toys bought in a year
2. Get toys greater than price
3. Exit
Enter your choice: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.