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

This assignment has all of the functionality of previous assignments, but will b

ID: 2246529 • Letter: T

Question

This assignment has all of the functionality of previous
assignments, but will be rewritten to use a dynamically allocated
linked list. Use Valgrind to check your code for leaks. Here are the two new requirements for this project:
• The exercises must be added to the list in alphabetical order according to the name of the exercise.
• In addition to the methods listed in project 3 and 4, you will create one more method and menu option to
delete an exercise from the list.
So, this assignment will have the same user-controlled loop as assignment 4 plus the delete option. The add()
and the loadData() methods will need to be rewritten to insert new exercises in alphabetical order. Just like
assignment 4, your program will write the data back to the same file as the input file at program termination,
using the writeData() method. Don’t forget to make a backup copy of assignment 4 before modifying the
source code for assignment 5.
Programming Requirements
• For assignment 4, we used an array of struct pointers, but for this version, we won’t be declaring an
array at all. We will be replacing the array with a pointer to the exerciseData struct called the head
pointer. Look at the class exerciseJournal. Notice that instead of the array
from project 4: exerciseData * exercises[arraySize]; we
now have: exerciseData * head;
Now look at the exerciseData struct to the right. At the bottom of the
struct declaration, there is a new member called next. The next pointer is
the link to the next struct in the list.
• Everything else with the struct will be the same as it was for assignment
4, namely, dynamically allocated c-strings. Use the same method to
allocate memory and strcpy() from a temporary string.
• I have also added a constructor prototype to the
exerciseJournal class. The constructor has become necessary
for this assignment, so that the head pointer can be initialized
to nullptr (or NULL).
• When you add new exerciseData structs to the linked list, it is
required that you add them alphabetically sorted according to
the name of the exercise.
• Notice that the delete method returns a bool, similar to
search(). If the user wishes to delete an exercise that doesn’t
exist in the list, return false, so that the calling function can
report an error. Otherwise return true, so that the calling
function can report successful deletion.
Strategies for using a linear linked list
• When you instantiate your exerciseJournal object, it is important to set the head pointer to nullptr (or
NULL if your compiler doesn’t support nullptr). Using nullptr is important because if a pointer is set to this
value, it means that it doesn’t point to anything, and is therefore the end of the list. Use your constructor
for exerciseJournal to set head to nullptr. By setting head = nullptr; you will be starting off with an empty
list.
class exerciseJournal {
exerciseData * head;
int countAndIndex;
char fileName[strSize];
public:
int loadData();
void writeData();
void add();
bool search();
void listAll();
void total();
bool delete();
exerciseJournal();
~exerciseJournal();
};
struct exerciseData {
char * name;
char * date;
char * note;
int time;
int calories;
int maxHeartRate;
exerciseData * next;
};
• The head pointer will point to the first exerciseData struct to be added to the linked list. When you load an
exercise from the file, you will know it is the first struct if
head is equal to nullptr. There are a couple of options to
add new structs to the list. The first method is to set the
head pointer to a new exerciseData struct, and then fill in
the data members. The second way is to set up a new
dynamic exerciseData using a temp pointer, fill in the data
there, and then set head equal to temp. Creating a
temporary pointer will pay dividends for you in the long
run. Look at the code in the textbox to the left to see an
example. This is only an example and you may have added
exercise data in other ways for assignment 4. You may
even want to delegate getting the data to a separate
method, and then have that method return the value of the temporary pointer.
• Adding additional exercises to the linked list is a bit trickier. Your next exercise may have a name that
places it at the beginning of the list, in which case head will point to the new exercise. If the name is
alphabetically at the end, then the new exercise will become the tail of the linked list. Otherwise, it will be
inserted between two other exercises. This is easier to describe with pictures, so
please see left and below. Suppose that the linked list consists of one exercise, called
Elliptical, as pictured to the left.
Elliptical is the last exercise in the list, because the next pointer is set to nullptr (or
NULL). This is signified with a slashing line in the next pointer field. Now let’s add Basketball.
Notice that Basketball comes before Elliptical alphabetically, so Basketball
becomes the head of the linked list. The head pointer is moved from
Elliptical to Basketball, and the next pointer for Basketball is made to point
at Elliptical. Elliptical-next is already set to nullptr, and the list is updated.
Now let’s add Cardio to the list.
Cardio comes after Basketball alphabetically, but before Elliptical.
So, the next pointer for Basketball is made to point at Cardio,
Cardio-next pointer is set to point at Elliptical, and the list is
updated.
The last possibility is to add an exercise to the end of the list. Strength comes after Elliptical alphabetically,
so it will be added to the end of the list. The next pointer for Elliptical will be set to point to Strength, the
next pointer for Strength will be set to nullptr, and the
list is updated.
Deleting an exercise struct is the opposite of adding one,
but is a little easier, because we already have the data,
and don’t need to allocate any memory. Suppose we
wanted to delete Strength. Then Elliptical-next pointer would be set to nullptr, and we could delete
Strength. Suppose we wanted to remove Basketball from the list. Since Basketball is the head, we need the
head pointer to point at Cardio:
exerciseData * temp; temp = head; head = head->next; delete temp; To delete
Cardio, we would set a temporary pointer to it, have Basketball-next point to Elliptical, and then delete
Cardio by deleting the temporary pointer. Suppose that we have searched for and found Cardio, so prev
points to Basketball and cur points to Cardio:
exerciseData *prev, *cur;
prev->next = cur->next; // Basketball now points to Elliptical.
delete cur;
void exerciseJournal::add() {
char input[STR_SIZE];
exerciseData * temp = new exerciseData;
cout << "What is the exercise name? ";
cin.getline(input, STR_SIZE);
temp->name = new char[strlen(input) + 1];
strcpy(temp->name, input);
// Add other data items here.
temp->next = nullptr;
if (head == nullptr)
head = temp;
// else code will go here, for adding
// other exerciseData structs.
return;
}

These are my code

exerciseHeader.hpp

#ifndef EXERCISE_HPP

#define EXERCISE_HPP

#include <iostream>

using namespace std;

const int strSIZE = 256;

const int Array = 128;

struct exerciseData {

char name[strSIZE];

char date[strSIZE];

char note[strSIZE];

int time;

int calories;

int maxHeartRate;

};

class exerciseJournal {

exerciseData practice[Array];

int countAndIndex;

char fileName[strSIZE];

public:

int loadData();

void writeData();

void add();

int search();

void listAll();

};

int loadData(exerciseData exerciseList[]);

int _search(exerciseData exerciseList[], int _count);

void _list(exerciseData exerciseList[], int _count);

#endif

main.cpp

#include "exerciseHeader.hpp"

#include "implementation.cpp"

using namespace std;

int main()

{

int time;

int calories;

int h_rates;

int o;

int countAndIndex;

char name[strSIZE];

int date[strSIZE];

char note[strSIZE];

exerciseJournal exerJournal;

cout << "Welcome to the SAMM exercise tracking program.";

int a = exerJournal.loadData();

cout << "Successfully loaded " << a << " activities ";

while (o != 'q')

{

cout << "What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? ";

cin.getline(0,1);

switch (o)

{

case 'l':

{

_list(exerciseList, a);

break;

}

case 's':

{

int u;

u = _search(exerciseList, a);

cout << " " << u << "activities found. ";

break;

}

case 'a':

{

cout << "Thank you for using the exercise tracking program.";

return 0;

}

implementation.cpp

#include "exerciseHeader.hpp"

#include <iostream>

#include <fstream>

#include <cstring>

int exerciseJournal::loadData()// pass the array of structs, return total exercises read.

{

//file stream and other varibles

//using char array instead of string

char file[256];

char ch;

exerciseData temp;

int i = 0;

cout << "Enter file to load ";

cin.getline(file, 256);

ifstream fin(file); //reading line by line

while (!fin.eof())

{

fin.getline(temp.name,strSIZE, ',');

fin.getline(temp.date,strSIZE, ',');

fin.getline(temp.note,strSIZE, ',');

fin >> temp.time;

fin.get(ch);

fin >> temp.calories;

fin.get(ch);

fin >> temp.maxHeartRate;

fin.get(ch);

i++;

}

return i - 1;

int _search(exerciseData exerciseList[], int _count);// pass the array and the number of items in the array.

char act[strSIZE];

cout << "What activity would you like to search for? ";

cin.getline(act, strSIZE);

for (int i = 0; i < countAndIndex; i++)

{

if (strcmp(practice[i].name, act) == 0)

{

countAndIndex;

//if (countAndIndex == 1)

{

cout << "Here are the activities matching " << act << endl;

cout << "Date Time Calories MaxHeartRate Note ";

}

cout << exerciseList[i].date << " " << exerciseList[i].time << " " << exerciseList[i].calories << " " << exerciseList[i].h_rates << " " << exerciseList[i].note << endl;

}

}

return 0;

}

void _list(exerciseData exerciseList[], int _count)// Print all data in the list.

{

int countAndIndex;

cout << "The exercises are as follows " << endl;

cout << "Name Date Time Calories MaxHeartRate Note ";

for (int i = 0; i < countAndIndex; i++)

{

cout << exerciseJournal[i].name << " " << exerciseList[i].date << " " << exerciseList[i].time << " " << exerciseList[i].calories << " " << exerciseList[i].h_rates << " " << exerciseList[i].note << endl;

}

}

void exerciseJournal::add()

{

char y[256], b[256], c[256];

int d, e, f;

char p;

cout << "What exercise activity did you do? ";

cin >> y;

cout << "What was the date (mm/dd/yy): ";

cin >> b;

cout << "How many minutes? ";

cin >> d;

cout << "How many calories did you burn? ";

cin >> e;

cout << "What was you max heart rate? ";

cin >> f;

cout << "Do you have any notes to add? ";

cin.getline(c, 50);

cout << "OK, " << y << " on " << b << ". Time: " << d << ", Calories: " << e << ", Max heartrate: " << f << ": Note: " << c << " Record the activity time and calories (y/n)? ";

cin >> p;

if (p == 'y')

{

strcpy(exerciseList[a].name, y);

strcpy(exerciseList[a].date, b);

strcpy(exerciseList[a].note, c);

exerciseList[a].time = d;

exerciseList[a].calories = e;

exerciseList[a].h_rate = f;

cout << "Your activity info has been recorded.";

countAndIndex;

}

else

{

cout << "Enter again";

goto there;

}

}

exercise text

Elliptical,06/10/17,great workout,40,400,135
Treadmill,06/12/17,doggin it,20,150,120
Stationary Bike,06/15/17,felt good,30,200,130
Elliptical,06/20/17,great-worked out with Mike,45,350,140

Explanation / Answer

Given below are the modified files. Removed some unwanted code outside of classes and implemented all methods. Output shown below. Please don't forget to rate the answer if it helped. Thank you.

exerciseHeader.hpp

#ifndef EXERCISE_HPP
#define EXERCISE_HPP
#include <iostream>
using namespace std;
const int strSIZE = 256;
const int Array = 128;
struct exerciseData {
char *name;
char *date;
char *note;
int time;
int calories;
int maxHeartRate;
struct exerciseData *next;
};
class exerciseJournal {
struct exerciseData * head;
int countAndIndex;
char fileName[strSIZE];
void add(exerciseData *e); //a private helper function to add an exerciseData object in sorted order
public:
exerciseJournal();
int loadData();
void writeData();
void add();
bool remove();
int search();
void listAll();
~exerciseJournal();
  
};
#endif

implementation.cpp


#include "exerciseHeader.hpp"
#include <iostream>
#include <fstream>
#include <cstring>
exerciseJournal::exerciseJournal()
{
head = NULL;
countAndIndex = 0;
}
int exerciseJournal::loadData()// pass the array of structs, return total exercises read.
{
//file stream and other varibles
//using char array instead of string
  
char ch;
char temp[strSIZE];
cout << "Enter file to load> ";
cin.getline(fileName, strSIZE);
ifstream fin(fileName); //reading line by line
while (true)
{
fin.getline(temp,strSIZE, ',');
if(fin.eof())
break;
exerciseData *e = new exerciseData;
e->name = new char[strlen(temp) + 1];
strcpy(e->name, temp);
  
fin.getline(temp,strSIZE, ',');
e->date = new char[strlen(temp) + 1];
strcpy(e->date, temp);
  
fin.getline(temp,strSIZE, ',');
e->note = new char[strlen(temp) + 1];
strcpy(e->note, temp);
  
fin >> e->time;
fin.get(ch);
fin >> e->calories;
fin.get(ch);
fin >> e->maxHeartRate;
fin.get(ch);
e->next = NULL;
add(e);
  
}
return countAndIndex;
}

void exerciseJournal::add(exerciseData *e)
{
if(head == NULL)
{
head = e;
}
else
{
exerciseData *curr = head;
exerciseData *prev = NULL;
//find out where to insert this new data
while(curr != NULL)
{
if(strcmp(e->name ,curr->name) < 0)
break;
prev = curr;
curr = curr->next;
}
//now we know where to insert, chck if inserting before head
  
if(prev == NULL)
head = e;
else
prev->next = e;
  
e->next = curr; //make the new node point to old
}
countAndIndex++;
}

int exerciseJournal::search()
{
char act[strSIZE];
cout << "What activity would you like to search for? ";
cin.getline(act, strSIZE);
int count = 0;
for (exerciseData *ed = head; ed != NULL; ed = ed->next)
{
if (strcmp(ed->name, act) == 0)
{
count++;
if (count == 1)
{
cout << "Here are the activities matching " << act << endl;
cout << "Date Time Calories MaxHeartRate Note ";
}
cout << ed->date << " " << ed->time << " " << ed->calories ;
cout << " " << ed->maxHeartRate << " " << ed->note << endl;
}
}
return count;
}

bool exerciseJournal::remove()
{
char act[strSIZE];
cout << "What activity would you like to delete? ";
cin.getline(act, strSIZE);
  
exerciseData *curr = head, *prev = NULL;
while(curr != NULL)
{
if (strcmp(curr->name, act) == 0)
break;
prev = curr;
curr = curr->next;
}
  
if(curr == NULL) //did not find matching exercise
return false;
else
{
if(prev == NULL) //deleing 1st node
head = curr->next;
else
prev->next = curr->next;
delete curr;
countAndIndex--;
return true;
}
  
}
void exerciseJournal::listAll()
{
  
cout << "The exercises are as follows " << endl;
cout << "Name Date Time Calories MaxHeartRate Note ";
for (exerciseData *ed = head; ed != NULL; ed = ed->next)
{
cout << ed->name << " " << ed->date << " " << ed->time << " " << ed->calories ;
cout << " " << ed->maxHeartRate << " " << ed->note << endl;
}
}
void exerciseJournal::add()
{
char act[strSIZE], dt[strSIZE], note[strSIZE];
int min, cal, heart;
char p;
cout << "What exercise activity did you do? ";
cin.getline(act, strSIZE);
cout << "What was the date (mm/dd/yy): ";
cin.getline(dt, strSIZE);
cout << "How many minutes? ";
cin >> min;
cout << "How many calories did you burn? ";
cin >> cal;
cout << "What was you max heart rate? ";
cin >> heart;
cin.ignore(); //ignore newline
cout << "Do you have any notes to add? ";
cin.getline(note, strSIZE);
cout << "OK, " << act << " on " << dt << ". Time: " << min << ", Calories: " << cal << ", Max heartrate: " << heart << ": Note: " << note << " Record the activity time and calories (y/n)? ";
cin >> p;
  
if (p == 'y')
{
exerciseData *e = new exerciseData;
e->name = new char[strlen(act) + 1];
strcpy(e->name, act);
  
e->date = new char[strlen(dt) + 1];
strcpy(e->date, dt);

e->note = new char[strlen(note) + 1];
strcpy(e->note, note);
  
e->time = min;
e->calories = cal;
e->maxHeartRate = heart;
e->next = NULL;
add(e);
cout << "Your activity info has been recorded. ";
  
}
}

void exerciseJournal::writeData()
{
ofstream outfile(fileName);
for(exerciseData *e = head; e != NULL; e = e->next)
{
outfile << e->name << "," << e->date << "," << e->note << ","
<< e->time << "," << e->calories << "," << e->maxHeartRate << " ";
}
outfile.close();
}

exerciseJournal::~exerciseJournal()
{
exerciseData *temp;
while(head != NULL)
{
temp = head->next;
delete head->name;
delete head->date;
delete head->note;
delete head;
head = temp;
}
}

main.cpp

#include "exerciseHeader.hpp"

using namespace std;
int main()
{
char ch = ' ';
exerciseJournal exerJournal;
cout << "Welcome to the SAMM exercise tracking program. ";
int a = exerJournal.loadData();
cout << "Successfully loaded " << a << " activities ";
while (ch != 'q')
{
cout << "What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit? ";
cin >> ch;
cin.ignore();//remove newline
switch (ch)
{
case 'l':
{
exerJournal.listAll();
break;
}
case 's':
{
int u;
u =exerJournal.search();
cout << " " << u << " activities found. ";
break;
}
case 'a':
{
exerJournal.add();
break;
}
case 'd':
{
if(exerJournal.remove())
cout << "exercise was removed. ";
else
cout << "exercise was not remove. ";
break;
}
case 'q':
{
exerJournal.writeData();
cout << "Thank you for using the exercise tracking program. ";
return 0;
}
}
}

}

input file: exercise.txt

Elliptical,06/10/17,great workout,40,400,135
Treadmill,06/12/17,doggin it,20,150,120
Stationary Bike,06/15/17,felt good,30,200,130
Elliptical,06/20/17,great-worked out with Mike,45,350,140

output

$ g++ implementation.cpp exercise2_main.cpp

$ ./a.out

Welcome to the SAMM exercise tracking program.
Enter file to load> exercise.txt
Successfully loaded 4 activities
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
l
The exercises are as follows
Name Date Time Calories MaxHeartRate Note
Elliptical 06/10/17 40 400 135 great workout
Elliptical 06/20/17 45 350 140 great-worked out with Mike
Stationary Bike 06/15/17 30 200 130 felt good
Treadmill 06/12/17 20 150 120 doggin it
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
s
What activity would you like to search for?
Elliptical
Here are the activities matching Elliptical
Date Time Calories MaxHeartRate Note
06/10/17 40 400 135 great workout
06/20/17 45 350 140 great-worked out with Mike
2 activities found.
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
s
What activity would you like to search for?
Treadmill
Here are the activities matching Treadmill
Date Time Calories MaxHeartRate Note
06/12/17 20 150 120 doggin it
1 activities found.
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
d
What activity would you like to delete?
Elliptical
exercise was removed.
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
l
The exercises are as follows
Name Date Time Calories MaxHeartRate Note
Elliptical 06/20/17 45 350 140 great-worked out with Mike
Stationary Bike 06/15/17 30 200 130 felt good
Treadmill 06/12/17 20 150 120 doggin it
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
a
What exercise activity did you do? Bike
What was the date (mm/dd/yy): 06/20/17
How many minutes? 15
How many calories did you burn? 180
What was you max heart rate? 100
Do you have any notes to add? good work out
OK, Bike on 06/20/17. Time: 15, Calories: 180, Max heartrate: 100: Note: good work out
Record the activity time and calories (y/n)? y
Your activity info has been recorded.
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
l
The exercises are as follows
Name Date Time Calories MaxHeartRate Note
Bike 06/20/17 15 180 100 good work out
Elliptical 06/20/17 45 350 140 great-worked out with Mike
Stationary Bike 06/15/17 30 200 130 felt good
Treadmill 06/12/17 20 150 120 doggin it
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, (d)elete an exercise or (q)uit?
q
Thank you for using the exercise tracking program

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote