My output is not printing out correctly and I think it has something to do with my deleteElement function, but I cannot figure out what the problem is. I have provided other functions that is also part of the Main.cpp file
From DArray.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
template <typename T>
DArray<T>::DArray()
{
capacity = CAP;
a = new T[capacity];
numOfElem = 0;
}
template <typename T>
DArray<T>::DArray(int newCapacity)
{
capacity = newCapacity;
a = new T[capacity];
numOfElem = 0;
}
template <typename T>
DArray<T>::DArray(const DArray<T>& otherArray)
{
capacity = otherArray.capacity;
a = new T[capacity];
numOfElem = otherArray.numOfElem;
for (int i = 0; i < numOfElem; ++i)
a[i] = otherArray.a[i];
}
template <typename T>
void DArray<T>::addElement(const T& element)
{
a[numOfElem] = element;
++numOfElem;
}
template <typename T>
void DArray<T>::deleteElement(const T& deleteElem)
{
bool found = false;
int pos = 0;
while (!found && pos != numOfElem)
{
if (a[pos] == deleteElem)
{
for (int i = pos; i < numOfElem; i++)
{
a[i] = a[++i];
}
found = true;
}
else
{
pos++;
}
}
--numOfElem;
}
|
And this is from the Movie.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include "Movie.h"
ostream& operator<<(ostream& out, const Movie& movie)
{
out << "\"" << movie.name << "\" (" << movie.year << ")" << endl;
return out;
}
Movie::Movie() : name(), year(0) {}
Movie::Movie(const string& diffName, int diffYear) :
name(diffName), year(diffYear) {}
string Movie::getName() const
{
return name;
}
int Movie::getYear() const
{
return year;
}
void Movie::setName(const string& newName)
{
name = newName;
}
void Movie::setYear(int newYear)
{
year = newYear;
}
bool Movie::operator==(const Movie& otherMovie) const
{
return (name == otherMovie.name && year == otherMovie.year);
}
Movie& Movie::operator=(const Movie& otherMovie)
{
name = otherMovie.name;
year = otherMovie.year;
return *this;
}
Movie::~Movie() {}
|
From Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include "DArray.h"
#include "DArray.cpp"
#include "Movie.h"
#include <iostream>
using namespace std;
int main()
{
DArray<int> a2(5);
cout << "\nTEST 3 ------------------------\n\n";
cout << "Re-populate..." << endl;
cout << "Array 2 is now: 21 53 64 19 43, with same capacity 5\n";
cout << "\nYour results:\n";
a2.addElement(21);
a2.addElement(53);
a2.addElement(64);
a2.addElement(19);
a2.addElement(43);
cout << "\tArray is: " << a2 << endl;
cout << "\tCapacity: " << a2.getCapacity() << endl;
cout << "\tNumber of elements: " << a2.getNumOfElem() << endl;
cout << "\tEmpty? " << (a2.isEmpty() ? "True." : "False") << endl;
cout << "\tFull? " << (a2.isFull() ? "True" : "False") << endl;
cout << "\tElement at last index: " << a2[a2.getNumOfElem() - 1] << endl;
cout << "\tDelete first element..." << endl;
a2.deleteElement(21);
cout << "\tAfter deletion: " << a2 << endl;
cout << "\tDelete last element..." << endl;
a2.deleteElement(43);
cout << "\tAfter deletion: " << a2 << endl;
cout << "\tNumber of elements: " << a2.getNumOfElem() << endl;
cout << "\tEmpty? " << (a2.isEmpty() ? "True" : "False") << endl;
cout << "\tFull? " << (a2.isFull() ? "True" : "False") << endl;
cout << "\tSearch for 53: ";
cout << ((a2.search(53)) ? "Found.\n" : "Not found.\n");
cout << "\tSearch for 19: ";
cout << ((a2.search(19)) ? "Found.\n" : "Not found.\n");
cout << "\tSearch for 100: ";
cout << ((a2.search(100)) ? "Found.\n" : "Not found.\n");
cout << "\tElement at last index: " << a2[a2.getNumOfElem() - 1] << endl;
cout << "\tDelete remaining elements..." << endl;
a2.deleteElement(64);
a2.deleteElement(53);
a2.deleteElement(19);
cout << "\tNumber of elements: " << a2.getNumOfElem() << endl;
cout << endl;
DArray<Movie> movies(4);
Movie m1("Fight Club", 1999);
Movie m2("Snatch", 2000);
Movie m3("Deadpool", 2016);
Movie m4("The Day After Tomorrow", 2004);
cout << "\nTEST 5 ------------------------\n\n";
cout << "\nRe-populate in reverse order..." << endl;
cout << "Array 3 is now: same capacity 4\n"
<< "\tThe Day After Tomorrow, 2004\n"
<< "\tDeadpool, 2016\n"
<< "\tSnatch, 2000\n"
<< "\tFight Club, 1999\n";
cout << "\nYour results:\n";
movies.addElement(m4);
movies.addElement(m3);
movies.addElement(m2);
movies.addElement(m1);
cout << "\tArray is:\n " << movies << endl;
cout << "\tCapacity: " << movies.getCapacity() << endl;
cout << "\tNumber of elements: " << movies.getNumOfElem() << endl;
cout << "\tEmpty? " << (movies.isEmpty() ? "True." : "False") << endl;
cout << "\tFull? " << (movies.isFull() ? "True" : "False") << endl;
cout << "\tElement at last index: " << movies[movies.getNumOfElem() - 1] << endl;
cout << "\tDelete first element..." << endl;
movies.deleteElement(m4);
cout << "\tAfter deletion:\n " << movies << endl;
cout << "\tDelete last element..." << endl;
movies.deleteElement(m1);
cout << "\tArray is:\n " << movies << endl;
cout << "\tNumber of elements: " << movies.getNumOfElem() << endl;
cout << "\tEmpty? " << (movies.isEmpty() ? "True" : "False") << endl;
cout << "\tFull? " << (movies.isFull() ? "True" : "False") << endl;
cout << "\tElement at last index: " << movies[movies.getNumOfElem() - 1] << endl;
cout << "\tDelete remaining elements..." << endl;
movies.deleteElement(m2);
movies.deleteElement(m3);
cout << "\tNumber of elements: " << movies.getNumOfElem() << endl;
cout << endl;
system("Pause");
return 0;
}
|
This is the output I am getting, which is not correct
<output>
TEST 3 ------------------------
Re-populate...
Array 2 is now: 21 53 64 19 43, with same capacity 5
Your results:
Array is: 21 53 64 19 43
Capacity: 5
Number of elements: 5
Empty? False
Full? True
Element at last index: 43
Delete first element...
After deletion: 21 53 64 19
Delete last element...
After deletion: 21 53 64
Number of elements: 3
Empty? False
Full? False
Search for 53: Found.
Search for 19: Not found.
Search for 100: Not found.
Element at last index: 64
Delete remaining elements...
Number of elements: 0
TEST 5 ------------------------
Re-populate in reverse order...
Array 3 is now: same capacity 4
The Day After Tomorrow, 2004
Deadpool, 2016
Snatch, 2000
Fight Club, 1999
Your results:
Array is:
"The Day After Tomorrow" (2004)
"Deadpool" (2016)
"Snatch" (2000)
"Fight Club" (1999)
Capacity: 4
Number of elements: 4
Empty? False
Full? True
Element at last index: "Fight Club" (1999)
Delete first element...
After deletion:
"The Day After Tomorrow" (2004)
"Deadpool" (2016)
"Snatch" (2000)
Delete last element...
Array is:
"The Day After Tomorrow" (2004)
"Deadpool" (2016)
Number of elements: 2
Empty? False
Full? False
Element at last index: "Deadpool" (2016)
Delete remaining elements...
Number of elements: 0
Press any key to continue . . .
</output>