I am trying to change the array in this program from a static array into a dynamic array. I'm totally lost on this one and could use a little help. There are a few other problems in the program, but the only thing I'm worried about right now is getting this from a static array to a dynamic array.
The program is supposed to take a given list of values and insert it into an array, delete any duplicates, and sort in ascending order. It will then read another list of values with an "a" or "d" in front of each value and add t he value if there is an "a" or delete the value if there is a "d." Finally the program outputs the updated list of values into a new file.
All I need to do here is switch the arrays from being statically defined to being dynamically defined. Probably not really that hard to do, but I'm new to this. I tried a few different things, but I keep getting memory leaks. Any help would be appreciated.
#include<iostream>
#include<fstream>
using namespace std;
#define MAX_SIZE 2000
int search(int value, int list[], int n);
void storeValue(int value, int list[], int& n);
void deleteValue(int value, int list[], int &n);
int main()
{
int list[MAX_SIZE];
int n = 0;
int value;
fstream inFile("invent.dat", ios::in);
while(inFile >> value)
{
if(search(value, list, n) == -1)
storeValue(value, list, n);
}
inFile.close();
cout << "Database has " << n << " ID numbers \n------------------------------------------\n";
char action;
fstream upFile("update.dat", ios::in);
while(upFile >> action >> value)
{
if(action == 'a' || action == 'A')
{
if(search(value, list, n) == -1);
storeValue(value, list, n);
}
if((action == 'D' || action == 'd') && search(value, list, n) != -1)
{
deleteValue(value, list, n);
}
}
upFile.close();
fstream outFile("final.dat", ios::out);
for(int i = 0; i < n; i++)
outFile << list[i] << '\n';
cout << "Updated Database \nDatabase has " << n << " ID numbers \n\n";
return 0;
}
int search(int value, int list[], int n)
{
for(int i = 0; i < n; i++)
{
if(list[i] == value)
return 0;
}
return -1;
}
void storeValue(int value, int list[], int& n)
{
int loc = 0;
for(loc = 0; loc < n; loc++)
{
if(value < list[loc])
break;
}
for(int i = n; i > loc; i--)
{
list[i] = list[i-1];
}
list[loc] = value;
n++;
}
void deleteValue(int value, int list[], int& n)
{
int loc;
for(int i = 0; i < n; i++)
{
if(list[i] == value)
loc = i;
}
for(int i = loc; i < n - 1; i++)
{
list[i] = list[i+1];
}
n--;
}