This question already has answers here:
Reading file into array of struct c++
(3 answers)
C++ Reading text file with delimiter into struct array
(2 answers)
C++ Reading data from text file into array of structures
(2 answers)
How do I read data from a text file into an array of struct
(1 answer)
Reading data from a text file into an array of structs
(1 answer)
Closed 2 years ago.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class BasketballPlayer
{
public:
void printBasketballPlayer() const;
BasketballPlayer(); //Constructor with Parameters
private:
string name;
float MinutesPlayed;
float PlayerScoreEffRating;
float Steals;
float AST;
float PointPerGame;
float Rebounds;
float FieldGoal;
float FreeThrow;
float ThreePointers;
float BLK;
float TurnOver;
};
class BasketballTeam
{
public:
string name;
void printBasketballTeam() const;
bool findPlayer(string &);
void editPlayer(bool&);
BasketballTeam(); //Default Constructor
void getName()
{
getline(cin, name);
}
private:
//void BasketballPlayer();
string name;
float MinutesPlayed;
float PlayerScoreEffRating;
float Steals;
float AST;
float PointPerGame;
float Rebounds;
float FieldGoal;
float FreeThrow;
float ThreePointers;
float BLK;
float TurnOver;
string myArray[16][11];
string PlayerName[15];
float myArray[15][11];
void Team()
{
ifstream file("TextFileTeam.txt");
if (file.is_open())
{
string myArray[16];
for (int i = 0; i < 16; ++i)
{
file >> myArray[i];
}
}
}
};
int main()
{
BasketballPlayer Play;
string Search;
bool isPlayerThere;
char Change;
BasketballTeam Team;
cout << " Welcome to the Brooklyn Net's Stats and DataBase " << endl;
cout << " These are the stats of our new SuperStar " << endl;
Play.printBasketballPlayer();
cout << " < Would you Like to Update stats of the roster > --> Y or --> N " << endl;
cin >> Change;
cout << endl;
if (Change == 'Y' || 'y')
{
Team.findPlayer(Search);
Team.editPlayer(isPlayerThere);
}
else
{
Team.printBasketballTeam();
}
Team.printBasketballTeam();
return 0;
}//end main
void BasketballPlayer::printBasketballPlayer() const
{
cout << "|| The players name || " << this->name;
cout << endl;
cout << "|| Minutes Per Game || " << this->MinutesPlayed;
cout << endl;
cout << "|| Turnovers Per Game || " << this->TurnOver;
cout << endl;
cout << "|| Points Per Game || " << this->PointPerGame;
cout << endl;
cout << "|| Rebounds Per Game || " << this->Rebounds;
cout << endl;
cout << "|| Asists Per Game || " << this->AST;
cout << endl;
cout << "|| Blocks Per Game || " << this->BLK;
cout << endl;
cout << "|| 3 Point % || " << this->ThreePointers;
cout << endl;
cout << "|| Free-Throw % || " << this->FreeThrow;
cout << endl;
cout << "|| Field Goal %|| " << this->FieldGoal;
cout << endl;
cout << "|| Steals % || " << this->Steals;
cout << endl;
cout << "|| Player Scoreing Effiencey Rating || " << this->PlayerScoreEffRating;
cout << endl;
}
BasketballPlayer::BasketballPlayer() //Constructor with Parameters
{
name = "James Harden";
MinutesPlayed = 40.2f;
TurnOver = 4.2f;
PointPerGame = 24.1f;
Rebounds = 8.1f;
AST = 12.0f;
BLK = 0.6f;
ThreePointers = 37.9f;
FreeThrow = 89.2f;
FieldGoal = 48.2f;
Steals = 1.1f;
PlayerScoreEffRating = 1.561f;
}
void BasketballTeam::printBasketballTeam() const
{
ifstream file("TextFileTeam.txt");
if (file.is_open())
{
string myArray[16];
for (int i = 0; i < 16; ++i)
{
file >> myArray[i];
cout << myArray[i];
cout << endl;
}
}
}
bool BasketballTeam::findPlayer(string&)
{
bool Finder = false;
const char *name;
switch (*name)
{
case 1:
if(name == "Kevin_Durant")
Finder = true;
break;
case 2:
if (name == "Kyrie_Irving")
Finder = true;
break;
case 3:
if (name == "James_Harden")
Finder = true;
break;
case 4:
if (name == "Joe_Harris")
Finder = true;
break;
case 5:
if (name == "Jarrett_Allen")
Finder = true;
break;
case 6:
if (name == "Jeff_Green")
Finder = true;
break;
case 7:
if (name == "Taurean_Prince")
Finder = true;
break;
case 8:
if (name == "Timothe_Luwawu - Cabarrot")
Finder = true;
break;
case 9:
if (name == "Spencer_Dinwiddie")
Finder = true;
break;
case 10:
if (name == "DeAndre_Jordan")
Finder = true;
break;
case 11:
if (name == "Bruce_Brown_Jr.")
Finder = true;
break;
case 12:
if (name == "Landry_Shamet")
Finder = true;
break;
case 13:
if (name == "Chris_Chiozza")
Finder = true;
break;
case 14:
if (name == "Reggie_Perry")
Finder = true;
break;
case 15:
if (name == "Tyler_Johnson")
Finder = true;
break;
default:
cout << " Player not in Roster " << endl;
break;
}
return Finder;
}
void BasketballTeam::editPlayer(bool &)
{
int editValue;
BasketballTeam Team;
bool isThere;
ifstream file("JustData.txt");
if (file.is_open())
{
string myArray[15];
for (int i = 0; i < 15; ++i)
{
file >> myArray[i];
}
}
if (isThere == true)
{
cout << " What would you like to edit? " << endl;
cout << "[1] Mintues, [2] Turnover, [3] Points Per Game, [4] Rebounds Per Game, [5]
Steals Per
Game, [6] Free Throw %, [7] Assits Per Game, [8] Filed Goal %" << endl;
cout << "[7] Assits Per Game, [8] Filed Goal %[9] 3P%, [10] Player Scoring Eff, [11]
Blocks Per Game ---> ";
cin >> editValue;
switch (editValue)
{
case 1:
MinutesPlayed = editValue;
break;
case 2:
PlayerScoreEffRating = editValue;
break;
case 3:
PointPerGame = editValue;
break;
case 4:
Rebounds = editValue;
break;
case 5:
Steals = editValue;
break;
case 6:
FreeThrow = editValue;
break;
case 7:
AST = editValue;
break;
case 8:
FieldGoal = editValue;
break;
case 9:
ThreePointers = editValue;
break;
case 10:
PlayerScoreEffRating = editValue;
break;
case 11:
BLK = editValue;
break;
default:
break;
}
}
}
BasketballTeam::BasketballTeam() //Default Constructor
{
int j;
int row;
int col;
j = 0;
ifstream file("JustData.txt");
if (file.is_open())
{
float PlayerArray[15][11];
for (int row = 0; row < 15; row++)
{
for (int col = 0; col < 11; col++)
{
file >> PlayerArray[row][col];
MinutesPlayed = PlayerArray[j][col];
TurnOver = PlayerArray[j][col];
Rebounds = PlayerArray[j][col];
PointPerGame = PlayerArray[j][col];
AST = PlayerArray[j][col];
BLK = PlayerArray[j][col];
ThreePointers = PlayerArray[j][col];
FreeThrow = PlayerArray[j][col];
FieldGoal = PlayerArray[j][col];
Steals = PlayerArray[j][col];
PlayerScoreEffRating = PlayerArray[j][col];
j++;
}
}
}
ifstream file2("JustNames.txt");
if (file2.is_open())
{
string PlayerName[15];
for (int row = 0; row < 15; row++)
{
file >> PlayerName[row];
name = PlayerName[row];
}
}
};
JustData.txt
36.9,3.5,7.5,5.2,0.8,1.4,30.8,53.3,45.2,87.8,1.575
35.8,2.3,4.8,5.7,1,0.8,28.3,53.5,44.7,94.8,1.403
40.3,4.2,8.1,12,1.1,0.6,24.1,48.2,37.9,89.2,1.561
31.7,0.9,3.7,2.1,0.6,0.3,14.9,51.2,48.7,61.5,1.361
26.6,1.8,10.4,1.7,0.6,1.6,11.2,67.7,0,75.4,2.062
25.7,1,3.8,1.4,0.7,0.2,8.9,54.1,44.7,83.3,1.519
18.1,0.9,2.8,0.6,0.7,0.7,8.1,40.5,35.1,88.9,1.311
19,0.6,2.2,1.3,0.6,0.2,7.3,38.3,36.3,72.2,1.091
21.4,1.7,4.3,3,0.7,0.3,6.7,37.5,28.6,100,1.25
20.3,1.6,6.9,1.7,0.3,1.4,6.6,81.3,0,48.4,1.813
17.6,0.9,4.2,0.9,0.5,0.4,5.9,58.8,17.6,71.4,1.329
17.2,0.8,1.4,1.1,0.6,0.2,5.6,34.4,29.6,84,1.14
11.2,0.8,1,2.5,0.5,0.4,4,28.2,29.4,71.4,0.821
10.5,0.8,3.6,0.9,0.1,0.4,3.6,41.2,16.7,66.7,0.98
6.2,0.3,0.7,1.1,0.4,0,1.3,30,42.9,0,0.9
JustNames.txt
Kevin_Durant
Kyrie_Irving
James_Harden
Joe_Harris
Jarrett_Allen
Jeff_Green
Taurean_Prince
Timothe_Luwawu-Cabarrot
Spencer_Dinwiddie
DeAndre_Jordan
Bruce_Brown_Jr.
Landry_Shamet
Chris_Chiozza
Reggie_Perry
Tyler_Johnson
TeamNoTitle.txt
Kevin_Durant,36.9,3.5,7.5,5.2,0.8,1.4,30.8,53.3,45.2,87.8,1.575
Kyrie_Irving,35.8,2.3,4.8,5.7,1,0.8,28.3,53.5,44.7,94.8,1.403
James_Harden,40.3,4.2,8.1,12,1.1,0.6,24.1,48.2,37.9,89.2,1.561
Joe_Harris,31.7,0.9,3.7,2.1,0.6,0.3,14.9,51.2,48.7,61.5,1.361
Jarrett_Allen,26.6,1.8,10.4,1.7,0.6,1.6,11.2,67.7,0,75.4,2.062
Jeff_Green,25.7,1,3.8,1.4,0.7,0.2,8.9,54.1,44.7,83.3,1.519
Taurean_Prince,18.1,0.9,2.8,0.6,0.7,0.7,8.1,40.5,35.1,88.9,1.311
Timothe_Luwawu-Cabarrot,19,0.6,2.2,1.3,0.6,0.2,7.3,38.3,36.3,72.2,1.091
Spencer_Dinwiddie,21.4,1.7,4.3,3,0.7,0.3,6.7,37.5,28.6,100,1.25
DeAndre_Jordan,20.3,1.6,6.9,1.7,0.3,1.4,6.6,81.3,0,48.4,1.813
Bruce_Brown_Jr.,17.6,0.9,4.2,0.9,0.5,0.4,5.9,58.8,17.6,71.4,1.329
Landry_Shamet,17.2,0.8,1.4,1.1,0.6,0.2,5.6,34.4,29.6,84,1.14
Chris_Chiozza,11.2,0.8,1,2.5,0.5,0.4,4,28.2,29.4,71.4,0.821
Reggie_Perry,10.5,0.8,3.6,0.9,0.1,0.4,3.6,41.2,16.7,66.7,0.98
Tyler_Johnson,6.2,0.3,0.7,1.1,0.4,0,1.3,30,42.9,0,0.9
TeamFileNets.txt
Player,MPG,TOV,RPG,APG,SPG,BPG,PPG,FG%,3P%,FT%,SC-EFF
Kevin_Durant,36.9,3.5,7.5,5.2,0.8,1.4,30.8,53.3,45.2,87.8,1.575
Kyrie_Irving,35.8,2.3,4.8,5.7,1,0.8,28.3,53.5,44.7,94.8,1.403
James_Harden,40.3,4.2,8.1,12,1.1,0.6,24.1,48.2,37.9,89.2,1.561
Joe_Harris,31.7,0.9,3.7,2.1,0.6,0.3,14.9,51.2,48.7,61.5,1.361
Jarrett_Allen,26.6,1.8,10.4,1.7,0.6,1.6,11.2,67.7,0,75.4,2.062
Jeff_Green,25.7,1,3.8,1.4,0.7,0.2,8.9,54.1,44.7,83.3,1.519
Taurean_Prince,18.1,0.9,2.8,0.6,0.7,0.7,8.1,40.5,35.1,88.9,1.311
Timothe_Luwawu-Cabarrot,19,0.6,2.2,1.3,0.6,0.2,7.3,38.3,36.3,72.2,1.091
Spencer_Dinwiddie,21.4,1.7,4.3,3,0.7,0.3,6.7,37.5,28.6,100,1.25
DeAndre_Jordan,20.3,1.6,6.9,1.7,0.3,1.4,6.6,81.3,0,48.4,1.813
"Bruce_Brown_Jr.",17.6,0.9,4.2,0.9,0.5,0.4,5.9,58.8,17.6,71.4,1.329
Landry_Shamet,17.2,0.8,1.4,1.1,0.6,0.2,5.6,34.4,29.6,84,1.14
Chris_Chiozza,11.2,0.8,1,2.5,0.5,0.4,4,28.2,29.4,71.4,0.821
Reggie_Perry,10.5,0.8,3.6,0.9,0.1,0.4,3.6,41.2,16.7,66.7,0.98
Tyler_Johnson,6.2,0.3,0.7,1.1,0.4,0,1.3,30,42.9,0,0.9
I'm sure that I'm not understanding Classes. Trying to use fstream to hold data of players so that my classes and driver code can change those files then display data from stored data in classes. I have no syntax errors but code not running. Most likey because of me reading file into array wrong to get data into my classes from file and improper calling of classes in main function.
malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Or I get this when I try and print everything
Segmentation fault: 11
I'm doing some homework for an OOP class and I've been stuck for a good hour now. I'm getting this error once I've used keyboard input enough. I am not someone who gets frustrated at all, and here I am getting very frustrated with this. Here are the files:
This is the book class. I'm pretty sure this is very solid. But just for reference:
//--------------- BOOK.CPP ---------------
// The class definition for Book.
//
#include <iostream>
#include "book.h"
using namespace std;
Book::Book()
//
{
strcpy(title, " ");
strcpy(author, " ");
type = FICTION;
price = 0;
}
void Book::Set(const char* t, const char* a, Genre g, double p)
{
strcpy(title, t);
strcpy(author, a);
type = g;
price = p;
}
const char* Book::GetTitle() const
{
return title;
}
const char* Book::GetAuthor() const
{
return author;
}
double Book::GetPrice() const
{
return price;
}
Genre Book::GetGenre() const
{
return type;
}
void Book::Display() const
{
int i;
cout << GetTitle();
for (i = strlen(title) + 1; i < 32; i++)
cout << (' ');
cout << GetAuthor();
for (i = strlen(author) + 1; i < 22; i++)
cout << (' ');
switch (GetGenre())
{
case FICTION:
cout << "Fiction ";
break;
case MYSTERY:
cout << "Mystery ";
break;
case SCIFI:
cout << "SciFi ";
break;
case COMPUTER:
cout << "Computer ";
break;
}
cout << "$";
if (GetPrice() < 1000)
cout << " ";
if (GetPrice() < 100)
cout << " ";
if (GetPrice() < 10)
cout << " ";
/* printf("%.2f", GetPrice());*/
cout << '\n';
}
This is the store class that deals with the array and dynamic allocation. This was working well without input commands, but just using its functions it was working like a champ.
//--------------- STORE.CPP ---------------
// The class definition for Store.
//
#include <iostream>
#include <cstring> // for strcmp
#include "store.h"
using namespace std;
Store::Store()
{
maxSize = 5;
currentSize = 0;
bookList = new Book[maxSize];
}
Store::~Store()
// This destructor function for class Store
// deallocates the Store's list of Books
{
delete [] bookList;
}
void Store::Insert(const char* t, const char* a, Genre g, double p)
// Insert a new entry into the direrctory.
{
if (currentSize == maxSize)// If the directory is full, grow it.
Grow();
bookList[currentSize++].Set(t, a, g, p);
}
void Store::Sell(const char* t)
// Sell a book from the store.
{
char name[31];
strcpy(name, t);
int thisEntry = FindBook(name);// Locate the name in the directory.
if (thisEntry == -1)
cout << *name << " not found in directory";
else
{
cashRegister = cashRegister + bookList[thisEntry].GetPrice();
// Shift each succeding element "down" one position in the
// Entry array, thereby deleting the desired entry.
for (int j = thisEntry + 1; j < currentSize; j++)
bookList[j - 1] = bookList[j];
currentSize--;// Decrement the current number of entries.
cout << "Entry removed.\n";
if (currentSize < maxSize - 5)// If the directory is too big, shrink it.
Shrink();
}
}
void Store::Find(const char* x) const
// Display the Store's matches for a title or author.
{
// Prompt the user for a name to be looked up
char name[31];
strcpy(name, x);
int thisBook = FindBook(name);
if (thisBook != -1)
bookList[thisBook].Display();
int thisAuthor = FindAuthor(name, true);
if ((thisBook == -1) && (thisAuthor == -1))
cout << name << " not found in current directory\n";
}
void Store::DisplayGenre(const Genre g) const
{
double genrePrice = 0;
int genreCount = 0;
for (int i = 0; i < currentSize; i++)// Look at each entry.
{
if (bookList[i].GetGenre() == g)
{
bookList[i].Display();
genrePrice = genrePrice + bookList[i].GetPrice();
genreCount++;
}
}
cout << "Number of books in this genre: " << genreCount
<< " " << "Total: $";
if (genrePrice < 1000)
cout << " ";
if (genrePrice < 100)
cout << " ";
if (genrePrice < 10)
cout << " ";
printf("%.2f", genrePrice);
}
void Store::DisplayStore() const
{
if (currentSize >= 1)
{
cout << "**Title**\t\t"
<< "**Author**\t"
<< "**Genre**\t"
<< "**Price**\n\n";
for (int i = 0; i < currentSize; i++)
bookList[i].Display();
}
else
cout << "No books currently in inventory\n\n";
cout << "Total Books = " << currentSize
<< "\nMoney in the register = $";
if (cashRegister < 1000)
cout << " ";
if (cashRegister < 100)
cout << " ";
if (cashRegister < 10)
cout << " ";
printf("%.2f", cashRegister);
cout << '\n';
}
void Store::Sort(char type)
{
Book temp;
for(int i = 0; i <= currentSize; i++)
{
for (int j = i+1; j < currentSize; j++)
{
if (type == 'A')
{
if (strcmp(bookList[i].GetTitle(), bookList[j].GetTitle()) > 0)
{
temp = bookList[i];
bookList[i] = bookList[j];
bookList[j] = temp;
}
}
if (type == 'T')
{
if (strcmp(bookList[i].GetAuthor(), bookList[j].GetAuthor()) > 0)
{
temp = bookList[i];
bookList[i] = bookList[j];
bookList[j] = temp;
}
}
}
}
}
void Store::SetCashRegister(double x)
// Set value of cash register
{
cashRegister = x;
}
void Store::Grow()
// Double the size of the Store's bookList
// by creating a new, larger array of books
// and changing the store's pointer to refer to
// this new array.
{
maxSize = currentSize + 5;// Determine a new size.
cout << "** Array being resized to " << maxSize
<< " allocated slots" << '\n';
Book* newList = new Book[maxSize];// Allocate a new array.
for (int i = 0; i < currentSize; i++)// Copy each entry into
newList[i] = bookList[i];// the new array.
delete [] bookList;// Remove the old array
bookList = newList;// Point old name to new array.
}
void Store::Shrink()
// Divide the size of the Store's bookList in
// half by creating a new, smaller array of books
// and changing the store's pointer to refer to
// this new array.
{
maxSize = maxSize - 5;// Determine a new size.
cout << "** Array being resized to " << maxSize
<< " allocated slots" << '\n';
Book* newList = new Book[maxSize];// Allocate a new array.
for (int i = 0; i < currentSize; i++)// Copy each entry into
newList[i] = bookList[i];// the new array.
delete [] bookList;// Remove the old array
bookList = newList;// Point old name to new array.
}
int Store::FindBook(char* name) const
// Locate a name in the directory. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
for (int i = 0; i < currentSize; i++)// Look at each entry.
if (strcmp(bookList[i].GetTitle(), name) == 0)
return i;// If found, return position and exit.
return -1;// Return -1 if never found.
}
int Store::FindAuthor(char* name, const bool print) const
// Locate a name in the directory. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
int returnValue;
for (int i = 0; i < currentSize; i++)// Look at each entry.
if (strcmp(bookList[i].GetAuthor(), name) == 0)
{
if (print == true)
bookList[i].Display();
returnValue = i;// If found, return position and exit.
}
else
returnValue = -1;// Return -1 if never found.
return returnValue;
}
Now this is the guy who needs some work. There may be some stuff blank so ignore that. This one controls all the input, which is the problem I believe.
#include <iostream>
#include "store.h"
using namespace std;
void ShowMenu()
// Display the main program menu.
{
cout << "\n\t\t*** BOOKSTORE MENU ***";
cout << "\n\tA \tAdd a Book to Inventory";
cout << "\n\tF \tFind a book from Inventory";
cout << "\n\tS \tSell a book";
cout << "\n\tD \tDisplay the inventory list";
cout << "\n\tG \tGenre summary";
cout << "\n\tO \tSort inventory list";
cout << "\n\tM \tShow this Menu";
cout << "\n\tX \teXit Program";
}
char GetAChar(const char* promptString)
// Prompt the user and get a single character,
// discarding the Return character.
// Used in GetCommand.
{
char response;// the char to be returned
cout << promptString;// Prompt the user
cin >> response;// Get a char,
response = toupper(response);// and convert it to uppercase
cin.get();// Discard newline char from input.
return response;
}
char Legal(char c)
// Determine if a particular character, c, corresponds
// to a legal menu command. Returns 1 if legal, 0 if not.
// Used in GetCommand.
{
return((c == 'A') || (c == 'F') || (c == 'S') ||
(c == 'D') || (c == 'G') || (c == 'O') ||
(c == 'M') || (c == 'X'));
}
char GetCommand()
// Prompts the user for a menu command until a legal
// command character is entered. Return the command character.
// Calls GetAChar, Legal, ShowMenu.
{
char cmd = GetAChar("\n\n>");// Get a command character.
while (!Legal(cmd))// As long as it's not a legal command,
{// display menu and try again.
cout << "\nIllegal command, please try again . . .";
ShowMenu();
cmd = GetAChar("\n\n>");
}
return cmd;
}
void Add(Store s)
{
char aTitle[31];
char aAuthor[21];
Genre aGenre = FICTION;
double aPrice = 10.00;
cout << "Enter title: ";
cin.getline(aTitle, 30);
cout << "Enter author: ";
cin.getline(aAuthor, 20);
/*
cout << aTitle << " " << aAuthor << "\n";
cout << aGenre << " " << aPrice << '\n';
*/
s.Insert(aTitle, aAuthor, aGenre, aPrice);
}
void Find()
{
}
void Sell()
{
}
void ViewGenre(Store s)
{
char c;
Genre result;
do
c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
while ((c != 'F') && (c != 'M') && (c != 'S') && (c != 'C'));
switch (result)
{
case 'F': s.DisplayGenre(FICTION); break;
case 'M': s.DisplayGenre(MYSTERY); break;
case 'S': s.DisplayGenre(SCIFI); break;
case 'C': s.DisplayGenre(COMPUTER); break;
}
}
void Sort(Store s)
{
char c;
Genre result;
do
c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
while ((c != 'A') && (c != 'T'));
s.Sort(c);
}
void Intro(Store s)
{
double amount;
cout << "*** Welcome to Bookstore Inventory Manager ***\n"
<< "Please input the starting money in the cash register: ";
/* cin >> amount;
s.SetCashRegister(amount);*/
}
int main()
{
Store mainStore;// Create and initialize a Store.
Intro(mainStore);//Display intro & set Cash Regsiter
ShowMenu();// Display the menu.
/*mainStore.Insert("A Clockwork Orange", "Anthony Burgess", SCIFI, 30.25);
mainStore.Insert("X-Factor", "Anthony Burgess", SCIFI, 30.25);*/
char command;// menu command entered by user
do
{
command = GetCommand();// Retrieve a command.
switch (command)
{
case 'A': Add(mainStore); break;
case 'F': Find(); break;
case 'S': Sell(); break;
case 'D': mainStore.DisplayStore(); break;
case 'G': ViewGenre(mainStore); break;
case 'O': Sort(mainStore); break;
case 'M': ShowMenu(); break;
case 'X': break;
}
} while ((command != 'X'));
return 0;
}
Please, any and all help you can offer is amazing.
Thank you.
Welcome to the exciting world of C++!
Short answer: you're passing Store as a value. All your menu functions should take a Store& or Store* instead.
When you're passing Store as a value then an implicit copy is done (so the mainStore variable is never actually modified). When you return from the function the Store::~Store is called to clean up the copied data. This frees mainStore.bookList without changing the actual pointer value.
Further menu manipulation will corrupt memory and do many double frees.
HINT: If you're on linux you can run your program under valgrind and it will point out most simple memory errors.
Your Store contains dynamically-allocated data, but does not have an assignment operator. You have violated the Rule of Three.
I don't see the Store class being instantiated anywhere by a call to new Store() which means the booklist array has not been created but when the program exits and calls the destructor, it tries to remove the array that was never allocated and hence that's why i think you are getting this error. Either, modify the destructor to have a null check or instantiate the class by a call to the constructor. Your code shouldn't still be working anywhere you are trying to use a Store object.
Hope this helps