Re-write your program for HW3 (list of chores problem 5 in chapter 4) using a un
ID: 3800438 • Letter: R
Question
Re-write your program for HW3 (list of chores problem 5 in chapter 4) using a unique_ptr object as your data member. You should store a dynamic array in the unique_ptr object. Use this array for storing, retrieving, deleting and updating chores.
Here's the proigram i wrote. can i get help on adding whats above. its in C++
#include
#include
#include
#include
using namespace std;
int main()
{
std::vector chores; // Vector to hold our words we read in.
std::string str; // Temp string to
std::ifstream fin("chores.txt"); // Open it up!
while (fin >> str) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
chores.push_back(str);
}
fin.close(); // Close that file!
int c;
while(c!=5)
{
cout<<" 1:Add an item to list of chores 2:Ask how many chores are in list 3:Have the list of chore printed to Screen 4:Delete an item from the list 5:Exit Enter you choice:";
cin>>c;
if(c==1)
{string s;
cout<<"Enter item you want to Add:";cin>>s;//taking user input
chores.push_back(s);//adding item ..to list
}
else if(c==2)
{
cout<<"Total number of chores:"< }
else if(c==3)
{
cout<<"Items are: ";
for (int i = 0; i < chores.size(); ++i)
std::cout << chores.at(i) << std::endl; // printing items
}
else if(c==4)
{
string s;
cout<<"Enter item name you want to delete:";cin>>s;//taking user input.
for (int i = 0; i < chores.size(); ++i)
{
if(s.compare(chores.at(i))==0)//finding approriate item to delete
{
//erasing from list..
chores.erase(chores.begin()+i);cout<<"item removed ";break;
}
}
}
ofstream OFileObject; // Create Object of Ofstream
OFileObject.open ("chores.txt"); // Opening a File
for (int i = 0; i < chores.size(); ++i)//writing to files..
{OFileObject<<chores[i]<<" ";}
OFileObject.close(); // Closing the file
}
return 0;
}
Explanation / Answer
using namespace std;
int main()
{
std::vector chores; // Vector to hold our words we read in.
std::string str; // Temp string to
std::ifstream fin("chores.txt"); // Open it up!
while (fin >> str) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
chores.push_back(str);
}
fin.close(); // Close that file!
int c;
while(c!=5)
{
cout<<" 1:Add an item to list of chores 2:Ask how many chores are in list 3:Have the list of chore printed to Screen 4:Delete an item from the list 5:Exit Enter you choice:";
cin>>c;
if(c==1)
{string s;
cout<<"Enter item you want to Add:";cin>>s;//taking user input
chores.push_back(s);//adding item ..to list
}
else if(c==2)
{
cout<<"Total number of chores:"< }
else if(c==3)
{
cout<<"Items are: ";
for (int i = 0; i < chores.size(); ++i)
std::cout << chores.at(i) << std::endl; // printing items
}
else if(c==4)
{
string s;
cout<<"Enter item name you want to delete:";cin>>s;//taking user input.
for (int i = 0; i < chores.size(); ++i)
{
if(s.compare(chores.at(i))==0)//finding approriate item to delete
{
//erasing from list..
chores.erase(chores.begin()+i);cout<<"item removed ";break;
}
}
}
ofstream OFileObject; // Create Object of Ofstream
OFileObject.open ("chores.txt"); // Opening a File
for (int i = 0; i < chores.size(); ++i)//writing to files..
{OFileObject<<chores[i]<<" ";}
OFileObject.close(); // Closing the file
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.