upon trying to delete the pointer obs I get a crash (im suspecting garbage data) if I run this and enter 1 and fill out the questions, the array increments right, and is able to delete. but upon running this and entering 1 twice (both filling out data). I get a crash. the array is incremented, and holds both sets of data that I have entered. also I am unable to use vectors, due to this being a school project.
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "fileStuff.h"
bool menu();
bool menuOptions(int option);
void fileIO();
void listAll(interact * obs, int arryO);
interact addOne(interact * obs, int size);
interact subOne(interact * obs, int size);
int main()
{
bool isRunning = true;
while (isRunning)
{
isRunning = menu();
}
return 0;
}
interact addOne(interact * obs, int size)
{
interact *temp = new interact[size];
cout << temp[0].getBagId() << " from method." << endl;
for (int i = 0; i < size ; i++)
{
cout << temp[i].getBagId() << endl;
temp[i] = obs[i];
}
return *temp;
}
interact subOne(interact * obs, int size)
{
return *obs;
}
bool menu()
{
int option = 0;
cout << "1: add new backpack. " << endl
<< "2: delete a backpack "<< endl
<< "3: sort ascending by id " << endl
<< "4: sort descending by id " << endl
<< "5: list all backpacks " << endl
<< "6: quit" << endl;
cin >> option;
return menuOptions(option);
}
bool menuOptions(int option)
{
static int arrayO = 0;
cout << arrayO << endl;
static interact *obs = new interact[arrayO];
fileStuff test;
int tempBagId = 0, tempInvSpaces = 0, tempAmtOfItemsInInv = 0;
double tempInvMaxWeight = 0.0;
string tempBagType, tempBagCondish;
int t = 0 ;
int i = 0;
switch (option)
{
case 1:
cout << "bagId? ";
cin >> tempBagId;
cout << "How many inv spaces? ";
cin >> tempInvSpaces;
cout << "How much weight can the bag hold? ";
cin >> tempInvMaxWeight;
obs[arrayO].setBagId(tempBagId);
obs[arrayO].setInvSpaces(tempInvSpaces);
obs[arrayO].setInvMaxWeight(tempInvMaxWeight);
// test.writeToFile(obs, arrayO);
cout << "all stored" << endl;
arrayO++;
*obs = addOne(obs, arrayO);
cout << obs[0].getBagId() << "ERE" << endl;
break;
case 5:
//list all
listAll(obs, arrayO);
break;
case 6:
obs = NULL;
delete obs;
// exit(0);
return false;
break;
default:
break;
}
}
void listAll(interact * obs, int arryO)
{
int i = 0;
for (i; i <= arryO; i++)
{
cout << (obs + i)->getBagId() << endl;
cout << (obs + i)->getInvSpaces() << endl;
cout << (obs + i)->getInvMaxWeight() << endl;
}
}