Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Re-write your program for HW3 \"List of Chores\" using a unique_ptr object as yo

ID: 3887383 • Letter: R

Question

Re-write your program for HW3 "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.

The book I own does not cover unique_ptr, can someone help me finish this code???

#include "stdafx.h"

#include<iostream>

#include<fstream>

#include<string>

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;

}

Explanation / Answer

Solution is just you need to create a unique pointer object for dynamic string array instead of string pointer.

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<memory>
#include<string>


using namespace std;


unique_ptr<string[]> reserve(unique_ptr<string[]> arr, int& cNum)
{

unique_ptr<string[]> newArr(new string[cNum+1]);
copy(arr, arr + cNum, newArr);
cNum = cNum + 1;
return newArr;
}


int main()
{
int cap = 1;
int numChores = 0;
std::unique_ptr<string[]> chores(new string[cap]); /*It simple,you just need to create an array with unique_ptr*/
/*and use for all operations as used string ptr before*/

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;
}