This assignment has all of the functionality of assignment 3, but
will be rewritten to use dynamic c-strings and dynamic structs.
Dynamic memory can leak, so use Valgrind to check your code for
leaks (see below). In addition to the methods listed in project 3, you will create one more method to calculate the total calories burned for all exercises in the database. So, this assignment will have the same user- controlled loop as assignment 3 plus the total option. The menu items are: search for an exercise by name, list all exercises stored in memory, add a new exercise, print the total calories burned for all exercises in memory, or quit. Just like assignment 3, 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 3 before modifying the source code for assignment 4.
——————————————————————————–
Here are Project 3 files below
—————————————————————————–
// common.cpp
#include “common.h”
// Return true if any char in cs satisfies condition.
// condition has type ‘int (*condition)(int)’ to match <cstring> convention.
bool any(const char cs[], int (*condition)(int)) {
for (int i = 0; cs[i]; i++)
if (condition(cs[i]))
return true;
return false;
}
// The q prefix indicates a function that queries the user.
// Ask the user a question. Dump the response into answer.
void qCString(const char question[], char answer[], const int ss) {
cout << question << ‘ ‘;
cin.getline(answer, ss);
}
// Bother the user until they enter a string containing graphical characters.
void qGCString(const char question[], char answer[], const int ss) {
qCString(question, answer, ss);
while (!any(answer, isgraph))
qCString(“Try again:”, answer, ss);
}
// Bother the user until they enter a valid integer. Return the integer.
int qInt(const char question[]) {
int resp;
bool fail;
cout << question << ‘ ‘;
while (true) {
cin >> resp;
fail = cin.fail();
cin.clear();
cin.ignore(strSize, ‘n’);
if (!fail)
break;
cout << “Try again: “;
}
return resp;
}
// Bother the user until they enter a positive integer. Return the integer.
int qPInt(const char question[]) {
int response = qInt(question);
while (response <= 0)
response = qInt(“Try again:”);
return response;
}
// Get a character from user. Consumes entire line.
char qChar(const char question[]) {
cout << question << ‘ ‘;
const char resp = cin.peek();
cin.ignore(strSize, ‘n’);
return resp;
}
// Return whether cs contains c.
bool contains(const char cs[], const char c) {
for (int i = 0; cs[i]; i++)
if (cs[i] == c)
return true;
return false;
}
// Bother user until they select an allowed character.
char qSel(const char question[], const char allowed[]) {
char resp = qChar(question);
while (!contains(allowed, resp))
resp = qChar(“Try again:”);
return resp;
}
// Bother the user until they enter y or n. Return true for y, false for n.
bool qYN(const char question[]) { return qSel(question, “yn”) == ‘y’; }
// Bother the user for a path to a real file. Return the open file.
void qFH(const char question[], ifstream& fh) {
char filename[strSize];
qCString(question, filename);
fh.open(filename);
while (!fh.is_open()) {
qCString(“Try again:”, filename);
fh.open(filename);
}
}
// Bother the user for a path to a real file. Set fn to filename.
void qFN(const char question[], char fn[], const int ss) {
ifstream fh;
qCString(question, fn);
fh.open(fn);
while (!fh.is_open()) {
qCString(“Try again:”, fn, ss);
fh.open(fn);
}
}
——————————————————————-
// common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int strSize = 256;
const int arraySize = 128;
// Return true if any char in cs satisfies condition.
// condition has type ‘int (*condition)(int)’ to match <cstring> convention.
bool any(const char cs[], int (*condition)(int));
// The q prefix indicates a function that queries the user.
// Ask the user a question. Dump the response into answer.
void qCString(const char question[], char answer[],
const int ss = strSize);
// Bother the user until they enter a string containing graphical characters.
void qGCString(const char question[], char answer[],
const int ss = strSize);
// Bother the user until they enter a valid integer. Return the integer.
int qInt(const char question[]);
// Bother the user until they enter a positive integer. Return the integer.
int qPInt(const char question[]);
// Get a character from user. Consumes entire line.
char qChar(const char question[]);
// Return whether cs contains c.
bool contains(const char cs[], const char c);
// Bother user until they select an allowed character.
char qSel(const char question[], const char allowed[]);
// Bother the user until they enter y or n. Return true for y, false for n.
bool qYN(const char question[]);
// Bother the user for a path to a real file. Return the open file.
void qFH(const char question[], ifstream& fh);
// Bother the user for a path to a real file. Set fn to filename.
void qFN(const char question[], char fn[], const int ss = strSize);
#endif
——————————————————————–
// exercise.cpp
Elliptical,06/10/17,40,400,135,great workout
Treadmill,06/12/17,20,150,120,doggin it
Stationary Bike,06/15/17,30,200,130,felt good
Elliptical,06/20/17,45,350,140,great-worked out with Mike
1,1,1,1,1,1
——————————————————————————
// exerciseJournal.cpp
#include “exerciseJournal.h”
// Populate an activity from a line in a csv.
bool parseActivity(exerciseData &ed, ifstream &fh) {
fh.getline(ed.name, strSize, ‘,’);
fh.getline(ed.date, strSize, ‘,’);
fh.getline(ed.note, strSize, ‘,’);
fh >> ed.time;
fh.ignore();
fh >> ed.calories;
fh.ignore();
fh >> ed.maxHeartRate;
fh.ignore();
}
template <class A, class B, class C, class D, class E, class F>
void showRow(A name, B date, C time, D calories, E maxHeartRate, F note) {
cout << left
<< setw(17) << name
<< setw(10) << date
<< setw(6) << time
<< setw(10) << calories
<< setw(15) << maxHeartRate
<< setw(12) << note
<< ‘n’;
}
// Populate an activity from user input.
void queryActivty(exerciseData &ed) {
qGCString(“What exercise activity did you do?”, ed.name);
qGCString(“What was the date (mm/dd/yy):”, ed.date);
ed.time = qPInt(“How many minutes?”);
ed.calories = qPInt(“How many calories did you burn?”);
ed.maxHeartRate = qPInt(“What was you max heart rate?”);
qGCString(“Do you have any notes to add?”, ed.note);
}
// Ask the user which file to load. Set fileName.
void exerciseJournal::queryFilename() {
qFN(“What is the name of the exercise data text file to load?”, fileName);
}
// Load all the data from a csv. Return number of loaded elements.
int exerciseJournal::loadData() {
countAndIndex = 0;
ifstream fh(fileName);
while (true) {
parseActivity(eds[countAndIndex], fh);
if (!fh)
break;
countAndIndex++;
if (countAndIndex >= arraySize)
break;
}
return countAndIndex;
}
void exerciseJournal::writeData() {
ofstream of(fileName);
if (of.is_open())
for (int i = 0; i < countAndIndex; i++)
of << eds[i].name << ‘,’
<< eds[i].date << ‘,’
<< eds[i].time << ‘,’
<< eds[i].calories << ‘,’
<< eds[i].maxHeartRate << ‘,’
<< eds[i].note << ‘n’;
else
cerr << “Could not open ” << fileName << ” for writing.n”;
of.close();
}
// Ask user to enter an exercise. Increment count if user chooses to save.
void exerciseJournal::add() {
if (countAndIndex >= arraySize) {
cout << “You need to stop exercising.n”;
} else {
queryActivty(eds[countAndIndex]);
if (qYN(“Record the activity time and calories (y/n)?”)) {
countAndIndex++;
cout << “Your activity info has been recorded.n”;
}
}
}
// Search for specific exercise name. Print all matches.
bool exerciseJournal::search() {
char name[strSize];
qGCString(“What activity would you like to search for?”, name);
cout << “Here are the activities matching ” << name << “:n”;
showRow(“Name”, “Date”, “Time”, “Calories”, “Max Heartrate”, “Note”);
for (int i = 0; i < countAndIndex; i++)
if (strcmp(name, eds[i].name) == 0)
showRow(eds[i].name, eds[i].date, eds[i].time,
eds[i].calories, eds[i].maxHeartRate, eds[i].note);
}
// Pretty print all the exercises.
void exerciseJournal::listAll() {
showRow(“Name”, “Date”, “Time”, “Calories”, “Max Heartrate”, “Note”);
for (int i = 0; i < countAndIndex; i++)
showRow(eds[i].name, eds[i].date, eds[i].time,
eds[i].calories, eds[i].maxHeartRate, eds[i].note);
}
——————————————————————–
//exerciseJournal.h
#ifndef _EXERCISE_JOURNAL_H_
#define _EXERCISE_JOURNAL_H_
#include “common.h”
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
struct exerciseData {
char name[strSize];
char date[strSize];
char note[strSize];
int time;
int calories;
int maxHeartRate;
};
class exerciseJournal {
exerciseData eds[arraySize];
int countAndIndex;
char fileName[strSize];
public:
void queryFilename();
int loadData();
void writeData();
void add();
bool search();
void listAll();
};
#endif
—————————————————————-
// main.cpp
#include “common.h”
#include “exerciseJournal.h”
#include <iostream>
using namespace std;
int main() {
exerciseJournal journal;
cout << “Welcome to the exercise tracking program.n”;
journal.queryFilename();
journal.loadData();
while (true) {
const char s = qSel(“What would you like to do: (l)ist all, (s)earch by”
” name, (a)dd an exercise, or (q)uit? :”, “lsaq”);
if (s == ‘l’) {
journal.listAll();
} else if (s == ‘s’) {
journal.search();
} else if (s == ‘a’) {
journal.add();
} else if (s == ‘q’) {
break;
}
}
journal.writeData();
cout << “Thank you for using the exercise tracking program.n”;
return 0;
}
Show transcribed image textProgramming Requirements · For assignment 3, our exercise Data struct used c-strings that were a constant size, as you can see from the version 3 struct to the left. But this kind of storage wastes a lot of memory. In order to hold an exercise that has a long name or note, strSize // Project 3 Version struct exerciseDataneeded to be fairly large. Let’s use an // Project 4 Version struct exerciseData char name [strSize]; char date[strSize]; char note[strSize]; int time int calories; int maxHeartRate example “Great Workout”, which might be a note for one of the exercises. This c-string only has 13 characters. Suppose you set up strSize to be 128. Then 115 bytes are wasted, because we set them aside but didn’t use them. So, for assignment 4, we Ji will be replacing the c-strings with char char* name; char date char * note; int time int calories; int maxHeartRate; pointers, as you can see in the project 4 version on the right. This will allow us to set aside just the right amount of space for each c-string item. » The main differences in the implementation file will be in loadData) and add(), because these are the two methods that store data in memory. So, you will need similar strategies for both methods we would like to set aside just the right amount of storage, but the problem is, we don’t know how long each c-string is until we load it from the file or ask the user for the information. So the simplest way to solve this problem is to create local temporary storage: char temp [strSize]; Now you are free to use istream.getline (temp, strSize); to load data, just like with assignment 3. Then you can find out how long the c-string is by calling strlen(): int len; len = strlen (temp): Now you can pass len along to the ‘new’ operator: exercises [i].name = new char [1en + 11: Notice that l used len + 1 inside of the square brackets. This is so there will be room for the null terminator. The last step is to copy the string from temp over to the brand new dynamically allocated c-string: strcpy (exercises[i].name, temp); Continue to place your implementation and main function code into separate source code files, and include a header file in both source code files. You must have at least 2 source code files, but you may have more if you wish · class exerciseJournal exerciseDataexercises [arraySize]; int countAndIndex; char fileName[strSize]; public: int loadDataO; void writeDataO; void addO bool search); void listAllO ~exerciseJournalO; » l;
Expert Answer
Given below are the modified files according to specifications in question. The program given in question did not run correctly initially and did not show the data from the file. Fixed all issues. Output shown below. Please do rate the answer if it helped. Thank you.
exerciseJournal.h
//exerciseJournal.h
#ifndef _EXERCISE_JOURNAL_H_
#define _EXERCISE_JOURNAL_H_
#include “common.h”
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
struct exerciseData {
char *name;
char *date;
char *note;
int time;
int calories;
int maxHeartRate;
//destructor in struct
~exerciseData()
{
delete []name;
delete []date;
delete []note;
}
};
class exerciseJournal {
exerciseData* eds[arraySize]; //array of pointers
int countAndIndex;
char fileName[strSize];
public:
void queryFilename();
int loadData();
void writeData();
void add();
bool search();
void listAll();
~exerciseJournal(); //destructor
};
#endif
exerciseJournal.cpp
// exerciseJournal.cpp
#include “exerciseJournal.h”
//a helper function to allocate needed memory based on contents of src and copy the value.
//dest passed by reference
void allocateAndCopy(char* &dest, const char* src){
dest = new char[strlen(src) + 1] ; //+1 for null terminator
strcpy(dest, src);
}
// Populate an activity from a line in a csv.
exerciseData* parseActivity(ifstream &fh) {
char temp[strSize];
exerciseData *edata = new exerciseData;
fh.getline(temp, strSize, ‘,’);
allocateAndCopy(edata->name, temp);
fh.getline(temp, strSize, ‘,’);
allocateAndCopy(edata->date, temp);
fh >> edata->time;
fh.ignore();
fh >> edata->calories;
fh.ignore();
fh >> edata->maxHeartRate;
fh.ignore();
fh.getline(temp, strSize);
allocateAndCopy(edata->note, temp);
return edata;
}
template <class A, class B, class C, class D, class E, class F>
void showRow(A name, B date, C time, D calories, E maxHeartRate, F note) {
cout << left
<< setw(17) << name
<< setw(10) << date
<< setw(6) << time
<< setw(10) << calories
<< setw(15) << maxHeartRate
<< setw(12) << note
<< ‘n’;
}
// Populate an activity from user input.
exerciseData* queryActivty() {
exerciseData* edata = new exerciseData;
char temp[strSize];
qGCString(“What exercise activity did you do?”, temp);
allocateAndCopy(edata->name, temp);
qGCString(“What was the date (mm/dd/yy):”, temp);
allocateAndCopy(edata->date, temp);
edata->time = qPInt(“How many minutes?”);
edata->calories = qPInt(“How many calories did you burn?”);
edata->maxHeartRate = qPInt(“What was you max heart rate?”);
qGCString(“Do you have any notes to add?”, temp);
allocateAndCopy(edata->note, temp);
return edata;
}
// Ask the user which file to load. Set fileName.
void exerciseJournal::queryFilename() {
qFN(“What is the name of the exercise data text file to load?”, fileName);
}
// Load all the data from a csv. Return number of loaded elements.
int exerciseJournal::loadData() {
countAndIndex = 0;
ifstream fh(fileName);
if(!fh.is_open())
{
cout << “Could not open input file ” << fileName << endl;
return 0;
}
while (true) {
eds[countAndIndex] = parseActivity(fh);
if (fh.eof())
break;
countAndIndex++;
if (countAndIndex >= arraySize)
break;
}
return countAndIndex;
}
void exerciseJournal::writeData() {
ofstream of(fileName);
if (of.is_open())
for (int i = 0; i < countAndIndex; i++)
of << eds[i]->name << ‘,’
<< eds[i]->date << ‘,’
<< eds[i]->time << ‘,’
<< eds[i]->calories << ‘,’
<< eds[i]->maxHeartRate << ‘,’
<< eds[i]->note << ‘n’;
else
cerr << “Could not open ” << fileName << ” for writing.n”;
of.close();
}
// Ask user to enter an exercise. Increment count if user chooses to save.
void exerciseJournal::add() {
if (countAndIndex >= arraySize) {
cout << “You need to stop exercising.n”;
} else {
eds[countAndIndex] = queryActivty();
if (qYN(“Record the activity time and calories (y/n)?”)) {
countAndIndex++;
cout << “Your activity info has been recorded.n”;
}
else
delete eds[countAndIndex]; //delete the dynamically allocated data if user does not want to record
}
}
// Search for specific exercise name. Print all matches.
bool exerciseJournal::search() {
char name[strSize];
bool found = false;
qGCString(“What activity would you like to search for?”, name);
cout << “Here are the activities matching ” << name << “:n”;
showRow(“Name”, “Date”, “Time”, “Calories”, “Max Heartrate”, “Note”);
for (int i = 0; i < countAndIndex; i++)
if (strcmp(name, eds[i]->name) == 0)
{
found = true;
showRow(eds[i]->name, eds[i]->date, eds[i]->time,
eds[i]->calories, eds[i]->maxHeartRate, eds[i]->note);
}
return found;
}
// Pretty print all the exercises.
void exerciseJournal::listAll() {
showRow(“Name”, “Date”, “Time”, “Calories”, “Max Heartrate”, “Note”);
for (int i = 0; i < countAndIndex; i++)
showRow(eds[i]->name, eds[i]->date, eds[i]->time,
eds[i]->calories, eds[i]->maxHeartRate, eds[i]->note);
}
exerciseJournal::~exerciseJournal()
{
for(int i = 0; i < countAndIndex; i++)
delete eds[i];
}
input file: exercise.txt
Elliptical,06/10/17,40,400,135,great workout
Treadmill,06/12/17,20,150,120,doggin it
Stationary Bike,06/15/17,30,200,130,felt good
Elliptical,06/20/17,45,350,140,great-worked out with Mike
output
Welcome to the exercise tracking program.
What is the name of the exercise data text file to load? exercise.txt
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : l
Name Date Time Calories Max Heartrate Note
Elliptical 06/10/17 40 400 135 great workout
Treadmill 06/12/17 20 150 120 doggin it
Stationary Bike 06/15/17 30 200 130 felt good
Elliptical 06/20/17 45 350 140 great-worked out with Mike
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : a
What exercise activity did you do? Cycle
What was the date (mm/dd/yy): 07/02/17
How many minutes? 15
How many calories did you burn? 100
What was you max heart rate? 120
Do you have any notes to add? good
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, or (q)uit? : l
Name Date Time Calories Max Heartrate Note
Elliptical 06/10/17 40 400 135 great workout
Treadmill 06/12/17 20 150 120 doggin it
Stationary Bike 06/15/17 30 200 130 felt good
Elliptical 06/20/17 45 350 140 great-worked out with Mike
Cycle 07/02/17 15 100 120 good
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : s
What activity would you like to search for? Elliptical
Here are the activities matching Elliptical:
Name Date Time Calories Max Heartrate Note
Elliptical 06/10/17 40 400 135 great workout
Elliptical 06/20/17 45 350 140 great-worked out with Mike
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : s
What activity would you like to search for? Cycle
Here are the activities matching Cycle:
Name Date Time Calories Max Heartrate Note
Cycle 07/02/17 15 100 120 good
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : a
What exercise activity did you do? Push up
What was the date (mm/dd/yy): 07/03/17
How many minutes? 10
How many calories did you burn? 120
What was you max heart rate? 100
Do you have any notes to add? tired
Record the activity time and calories (y/n)? n
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : l
Name Date Time Calories Max Heartrate Note
Elliptical 06/10/17 40 400 135 great workout
Treadmill 06/12/17 20 150 120 doggin it
Stationary Bike 06/15/17 30 200 130 felt good
Elliptical 06/20/17 45 350 140 great-worked out with Mike
Cycle 07/02/17 15 100 120 good
What would you like to do: (l)ist all, (s)earch by name, (a)dd an exercise, or (q)uit? : q
Thank you for using the exercise tracking program.