Question: Re-write your program List of Chores using a unique_ptr object as your data member. You should store a dynamic array in the unique_ptr object. Use
Re-write your program "List of Chores" 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.
I am using visual studios to do this problem. I've been out with the flu for the past few days and our book does not cover unique_ptr. The assignment is due tm and I have no clue where to begin. Here is the code I have that works. This is without making any changes yet.
#include "stdafx.h"
#include
#include
#include
using namespace std;
string* reserve(string* arr, int& cNum)
{
string* newArr = new string[cNum + 1];
copy(arr, arr + cNum, newArr);
cNum = cNum + 1;
return newArr;
}
int main()
{
int cap = 1;
int numChores = 0;
string* chores = new string[cap];
ifstream inFile;
ofstream outFile;
inFile.open("input.bat");
if (!inFile)
{
cout << "File did not open correctly!!" << endl;
exit(1);
}
string inputLine;
while (getline(inFile, inputLine))
{
if (numChores == cap)
chores = reserve(chores, cap);
chores[numChores] = inputLine;
numChores++;
}
while (true)
{
cout << endl;
cout << "To add a chore,----------------------PRESS 1!!" << endl;
cout << "To display the number of chores,-----PRESS 2!!" << endl;
cout << "To print the chores list,------------PRESS 3!!" << endl;
cout << "To delete an chore,------------------PRESS 4!!" << endl;
cout << "To exit the program,-----------------PRESS 5!!" << endl;
int userInput;
cout << "Please enter your choice now!! " << endl;
cin >> userInput;
string curChore;
int cnt = 0;
switch (userInput)
{
case 1:
cout << "Enter the chore that needs to be executed!!" << endl;
getline(cin, curChore);
getline(cin, curChore);
if (numChores == cap)
chores = reserve(chores, cap);
chores[numChores++] = curChore;
break;
case 2:
cout << endl;
cout << "The number of chores in the list is " << numChores << "." << endl << endl;
break;
case 3:
cout << "******LIST OF CHORES****** " << endl;
for (int index = 0; index < numChores; index++)
{
cout << chores[index] << endl;
}
break;
case 4:
cout << endl;
cout << "Which chore would you like to delete? " << endl;
getline(cin, curChore);
getline(cin, curChore);
cnt = 0;
for (int index = 0; index < numChores; index++)
{
if (chores[index] == curChore)
{
chores[index] = chores[numChores - 1];
numChores--;
cnt++;
}
}
cout << "The number of chores deleted are " << cnt << endl;
break;
case 5:
cout << "Saving the chores list and exiting program!!" << endl;
outFile.open("input.bat");
for (int index = 0; index < numChores; index++)
{
outFile << chores[index] << endl;
}
cout << "Ending the program. " << endl;
exit(0);
break;
default:
cout << "There seems to be an error!! Run the program again.." << endl;
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
